[java] 2D collision response

Started by
6 comments, last by nataku92 18 years, 7 months ago
I'm having a problem identical to the one in this thread. I have to move the player to the edge of the object it collided with, but I don't know how to find out which side it collided with, since the player can be moving in different x and y velocities at the same time. I'm using rectangles as bounding boxes and checking for collisions with the intersects() method. I've gotten it so that it works fine if you collide diagonally on the left or right side of an object, but I don't know how to correct the collision response for the top and bottom sides. Here's what I've come up with so far:

    private void updatePlayer(long elapsedTime)
    {
        float dx = player.getVelocityX();
        if (player.getBounds().intersects(player2.getBounds())) {
	    if (dx > 0) {
                player.setX(player2.getX() - player.getWidth());
	    }
	    else if (dx < 0) {
	       player.setX(player2.getX() + player2.getWidth());
	    }
        }
        player.updateBounds();
        float dy = player.getVelocityY();
        if (player.getBounds().intersects(player2.getBounds())) {
            if (dy > 0) {
                player.setY(player2.getY() - player.getHeight());
            }
            else if (dy < 0) {
                player.setY(player2.getY() + player2.getHeight());
            }
        }
    }




Thanks [Edited by - nataku92 on August 27, 2005 2:43:34 PM]
Advertisement
Well in order to collide it has to collide in both axis so there will always be 2 sides that are colliding(I am assuming you are using rectangles). So the question is which axis should you move the box in. I think in order to move an object out of another you should move in the axis that has the smallest distance between colliding sides. I wasn't sure if that made sense so I drew you a picture .

This should work and look pretty good in most cases, but is likely to be defective if the rectangles are very small or move very fast. Those errors could be fixed by moving the objects back when they collide and then moving back into the collision in smaller increments. Something like this:
If (boxa.collides(boxb)) {   boxa.position-= boxa.velocity   boxb.position-= boxb.velocity    boxa.velocity*=0.1   boxn.velocity*=0.1   for(int k=0;k<10;k++){      boxa.position+= boxa.velocity      boxb.position+= boxb.velocity       If (boxa.collides(boxb)) {         fix collision errors in position         break loop      }   }   boxa.velocity*=10   boxn.velocity*=10}
Thanks for the help Alrecenk. Works fine most of the time, even without moving the player by increments and checking for collisions. However, I found 2 situations in which this method wouldn't work:


  1. If the player collides on the y-axis but just hits the edge of the object, creating an intersection box with the width less than the height. (Same on the x-axis).


  2. If the intersection box has the same width and height. Then it wouldn't be possible to find if the player collided on the x-axis, y-axis, or directly on the corner.



Oh, and I might switch to using the area class to more accurately describe the player, so I need a different method to handle collisions.

Thanks

[Edited by - nataku92 on August 28, 2005 1:09:39 AM]
this is what I've been doing

public void CollisionDetection(IMoveableObject o){   while(o.dx < 0 && (!isEmpty(o.left + o.dx, o.top) || !isEmpty(o.left + dx, o.bottom)){      o.dx++;   }   while(o.dx > 0 && (!isEmpty(o.right + o.dx, o.top) || !isEmpty(o.right + dx, o.bottom)){      o.dx--;   }   while(o.dy < 0 && (!isEmpty(o.left, o.top + o.dy) || !isEmpty(o.right, o.top + o.dy)){      o.dy++;   }   while(o.dy > 0 && (!isEmpty(o.left, o.bottom + o.dy) || !isEmpty(o.right, o.bottom + o.dy)){      o.dy--;   }}/*** returns true if the specified point on this object is empty. This example is for a tile map*/public bool isEmpty(int x, int y){   int tx = x / tileWidth;   int ty = y / tileHeight;   return (map[y][x] == WALKABLE); //obviously this isn't the exact test, it just conveys the meaning}

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

The easiest way I find to do 2D collisions in Java is to add 4 additional rectangles. First check that whatever the player controls is in someway intersecting the other object. If it is then it needs to check to see which part of the player is colliding with the other object. In otherwords there is a rectangle for the top, the left side, the right side, and the bottom of the player. Find which one of those is intersecting the object and then make the additional adjustments you need to. I am working on a game where you rotate the level around the player, and all the collisions seem to work this way perfectly.
Thanks for the ideas. I'll try them out as soon as I can. I still need a method for collision detection with the area class though.

Thanks
In my opinion, you should use the center of the object and its velocity to form a vector. The lenght of that vector depends on its velocity. Use that vector as a line to check for intersection with 4 edges of the other object. You need to make the vector from one rectangly object only and check with other objects (rectangle). If collision happened, send a message to the other object.

Of course there's more to do to make it a perfect 2D collision detection. If you like my idea, respond and I'll post the rest, hope it helps :)
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Skeleton_V@T, thanks for the reply. I'd like to see how to implement your method

Thanks

This topic is closed to new replies.

Advertisement