Jump to content
  • Advertisement
Sign in to follow this  
nataku92

Unity [java] 2D collision response

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!