Search code examples
javalwjglslick2d

Proper Collision


So basically im trying to figure out how to make a proper collision between two Rectangles. Detecting isn't the problem, but the rectangles begin to clip. I wanted to reset the position, but how do I do that. I'm trying to use dx and dy to reset, but it won't reset to the right coordinates.

https://i.sstatic.net/IU6sK.png (Sorry I can't use images yet)

System.out.println(this.y + this.h + " " +  e.getY());
if(this.y + this.h >= e.getY())
{
    if(this.dy > 0)
    {
        this.y -= delta * this.dy + 0.1; 
        this.dy = 0;
    }
    else 
    {
        this.y += delta * this.dy;
        this.dy = 0;
        this.inAir = false;
    }
}

This code is just an example to show how I am trying it for the top. (this = the white Rectangle and e = the orange one) I used my class Entity, which extends Rectangle.

I'm checking intersection before I call this. This is a function in the "white" Entity and the intersection is checked in the update function of the main loop.

If I use this, there is like 1px between the rectangles. Any ideas? Thanks for any help :)


Solution

  • http://docs.oracle.com/javase/6/docs/api/java/awt/Rectangle.html

    Use the Rectangle class.

    Here, some code

    http://pastebin.com/raw.php?i=TzkST3Hm