LibGDX - Rectangle collision detection in 2d?

Hi,

I made two cars with sprites like in the picture.

http://img802.imageshack.us/img802/2934/rectangles2.png

Two cars with (width: 32px and height: 20px).
Everything works great.

There is no collision detection now. I want to do a collision detection without the sprites.
I want it so, because I want to add 3d Cars at these positions and with his rotations.
I mean, Collision detection without sprites in 2d.

In the end game, there will be no sprites. Only 3d Objects. But the collision detection should be in 2d.

Anybody has some codes for that? What are the keywords to search for that?
I searched and came to Polygon and Intersector. But I don’t found some examples or
tutorials.

I want to make it without Box2D.

Thank You for any help.

If the collision boxes are going to just be rectangles, I recommend looking into OBB (Oriented Bounding box) collisions and Separated Axis Theorem.

Yes, they will be only rectangles. Have you some Links for that please?

Yeah, this is the website I used for learning it. http://www.wildbunny.co.uk/blog/2011/04/20/collision-detection-for-dummies/
Basically it involves rotating each rectangle by the negative rotation of one and checking for if the vertices are inside the other rectangle.

Here’s a link describing Separating Axis Theorem (not Java though, but the principle is the same)

http://rocketmandevelopment.com/2010/05/19/separation-of-axis-theorem-for-collision-detection/

i found it pretty useful.

I got it.

With the help of this:

This is my solution:


..
	Rectangle bounds;
	Polygon polygon;
	
	Rectangle bounds2;
	Polygon polygon2;
...
	@Override
	public void create() {
...
		bounds = new Rectangle(0, 0, 32, 20);
		polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height});
		polygon.setOrigin(bounds.width/2, bounds.height/2);
		
		bounds2 = new Rectangle(0, 0, 32, 20);
		polygon2 = new Polygon(new float[]{0,0,bounds2.width,0,bounds2.width,bounds2.height,0,bounds2.height});
		polygon2.setOrigin(bounds2.width/2, bounds2.height/2);

...
}
	@Override
	public void render() {
...


		polygon.setPosition(car1.x, car1.y);
		polygon.setRotation(car1.rotation);
		polygon2.setPosition(car2.x, car2.y);
		polygon2.setRotation(car2.rotation);

...
        if(Intersector.overlapConvexPolygons(polygon, polygon2)){
            //COLLISION DON'T HAPPEN!!!
        }
...
}


Sorry for the bad names -> bounds, bounds2…
I wrote the code fast.

And thank you for the GOOD Links.

Gdx has polygon collision methods? Well, you learn something every day. :slight_smile:

I wrote an article about SAT myself (includes MTV response but in basic)

http://back2basic.phatcode.net/?Issue_%231:2D_Convex_Polygon_Collision_using_SAT

Huh doesn’t libgdx has rectangle class for this?

Yes, there is a rectangle class. But with Polygon class you can set rotate or set position etc.

See http://www.level1gamer.com/2012/10/24/top-down-car-using-libgdx-and-box2d/ for a more versatile way of doing this…