Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX - Rectangle collision detection in 2d?  (Read 7399 times)
0 Members and 1 Guest are viewing this topic.
Offline ddfh

Junior Member


Medals: 1



« Posted 2013-03-06 21:18:40 »

Hi,

I made two cars with sprites like in the picture.

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.
Offline cubemaster21
« Reply #1 - Posted 2013-03-06 21:38:23 »

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

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ddfh

Junior Member


Medals: 1



« Reply #2 - Posted 2013-03-06 21:43:42 »

Yes, they will be only rectangles. Have you some Links for that please?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cubemaster21
« Reply #3 - Posted 2013-03-06 21:48:55 »

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.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Axeman

Senior Member


Medals: 7



« Reply #4 - Posted 2013-03-06 21:54:19 »

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.
Offline ddfh

Junior Member


Medals: 1



« Reply #5 - Posted 2013-03-06 22:30:23 »

I got it.

With the help of this:
http://stackoverflow.com/questions/14359852/libgdx-properly-using-polygon-class

This is my solution:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
..
   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.
Offline Axeman

Senior Member


Medals: 7



« Reply #6 - Posted 2013-03-06 23:36:24 »

Gdx has polygon collision methods? Well, you learn something every day. Smiley
Offline relminator
« Reply #7 - Posted 2013-03-07 00:40:51 »

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
Offline ReBirth
« Reply #8 - Posted 2013-03-07 02:40:08 »

Huh doesn't libgdx has rectangle class for this?

Offline ddfh

Junior Member


Medals: 1



« Reply #9 - Posted 2013-03-07 07:23:31 »

Yes, there is a rectangle class. But with Polygon class you can set rotate or set position etc.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #10 - Posted 2013-03-07 14:10:53 »

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

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (15 views)
2014-07-29 03:07:13

E.R. Fleming (5 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!