4

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.stack.imgur.com/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 :)

B.Friedrichs
  • 158
  • 7
  • No, because it wouldn't make sense. Maybe I should have added that. This is just for collision on top. So the white rectangle would just fall through. – B.Friedrichs Nov 05 '12 at 13:42

2 Answers2

3

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

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • I'm using them. I made the class Entity which extends Rectangle. The problem isn't the detection, but the resetting of the position. – B.Friedrichs Nov 05 '12 at 13:44
  • 1
    Then just use `if (Entity1.intersects(Entity2)) { ... }` – tckmn Nov 05 '12 at 13:45
  • 1
    If they intersect, set box 1's location to be directly on top of box 2. Something like `Box1.setXLocation(Box2.getXLocation - Box1.height);` – tckmn Nov 05 '12 at 13:47
  • I agree. Being greater than is not the same as intersecting. Two points intersect when they are the same. I think it's a good question. Picklish, you should vote up. :D – Wolfpack'08 Nov 05 '12 at 13:48
  • 1
    derp. I'm using `org.newdawn.slick.Graphics`, isn't it the same? Because `intersects` of `awt.Rectangle` will not reset the position.. – B.Friedrichs Nov 05 '12 at 13:49
  • Well yes. I already tried that, but if I want to add collision from every side, how am I going to decide from which side the rectangles intersect?Like the 2. in this picture? [link](http://i.imgur.com/rFYnC.png) Because the red rectangle clips through, it could be from top or from the left.. – B.Friedrichs Nov 05 '12 at 13:54
  • Like this? http://i.imgur.com/8IVTk.png Oh wait, I misread your comment. You can check if it intersects, and if it does, don't let it move in that direction. Or you could move it back to its previous location. – tckmn Nov 05 '12 at 13:55
  • You're checking collision every time BEFORE you move not after? Am I right with this. Because that could be the answer. I will test right now! – B.Friedrichs Nov 05 '12 at 14:02
  • @Wolfpack'08 well... of course it does. There's no `main` method, no `class`, or anything, it's just pseudocode. Have you even looked at the errors? – tckmn Nov 07 '12 at 13:58
1

The best way to do rectangular collision is to use the Rectangle class to detect collision using the .intersects(Rectangle) method, and then calculate a new variable called displacementX and displacementY.

displacementX = Math.abs(entitiy1.getX() - entity2.getX());

displacementY = Math.abs(entitiy1.getY() - entity2.getY());

So what we have currently is the amount of pixels entity1 is intruding on entity2 (or vice versa due to the absolute value). Then, run some comparisons and move entity1 (or entity2) by the value of the lesser displacement, which should yield perfect-looking collision.

This is at least how I do it. The correct method for rectangular collision is to:

1) Determine if they collide

2) Correct it

3) Render

Simply preventing movement after detecting collision will look horrible (especially on low frame rates).

Hope I helped!

~Izman

Izman
  • 309
  • 3
  • 13