Skip to content Skip to sidebar Skip to footer

Collision Detection In Java/android

I want to check collision detection in a game (the code is below). I have a game class in which I am drawing bitmaps, making touch events and make an array list for moving a vehicl

Solution 1:

I know what it is like about dealing with collision detection. If you want cheap advice here it is: This will work with 2d games. Set each object a Rectangle for its boundries. Set rectangle x and y as the top left positon and then the width the width of your object set the height as the height of your object. Do the same thing with the other objects.

In the rectangle class there is a method called intersect or something like that. Do rect1.isIntersecting(rect2); in the update method. Hope this helps

Solution 2:

To check if two shapes are colliding, check axis one by one (X and Y axis in your case). You should make some research on this topic: "separate axis theorem". Here is a tutorial about it: http://www.metanetsoftware.com/technique/tutorialA.html (Section 1).

Solution 3:

You could use a rectangle collision check. If (frogLeftvehicleLeft and frogTopvehicleTop) then you know they must be overlapping. You could add methods for the top, right, left, bottom attributes to make the code pretty.

EX:

intright()
{
    returnthis.x+this.width;
}

Or you could make your objects inherit from java's rectangle2D.double object and use the built in intersection function: http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Rectangle2D.html#intersects(double, double, double, double) .

Also, I have a tutorial on 2D collision detection written in Java posted here: http://tylergriffin.me/collision_detection/

Solution 4:

if you are dealing with rectangles then it may help you

/**
 * Check if two rectangles collide
 * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle
 * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle
 */boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2){
  return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}

Post a Comment for "Collision Detection In Java/android"