Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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  
  Best way to rotate a polygon used for collision test?  (Read 3414 times)
0 Members and 1 Guest are viewing this topic.
Offline Axeman

Senior Member


Medals: 7



« Posted 2012-04-20 15:21:58 »

I´ve just gotten starten on a simple Asteroid-type game, just to get the hang of game programming. I´m using lwjgl in 2d-mode and so far I´ve only got a Pong clone under my belt. To keep it simple I´m using only polygons for the ship and asteroids for the time being, since I´m mostly interested in trying to implement some more "advanced" game physics. Smiley

Anyway, last night i realized a a problem I´d never thought of before: That is the fact that a glRotate() only affects what I´m rendering to screen but not the vertices I´m using to calculate collision. What´s the best way to solve this problem?  I did a little research that told me it´s possible to get the transformation made by glRotate and copy the matrix using a FrameBuffer... Is this the way to go or should I calculate the rotation myself and just render the new values with a simple glBegin?

Offline Tim Spekler
« Reply #1 - Posted 2012-04-20 18:13:41 »

Hi !
I would recommend you for rendering to keep your glBegin..glEnd as it is and to use glRotate() for the rotation (as you just did).
For collision, you should create a Bounding Box associated to all objects that are concerned by collision.
What you should do is trying to find a separating axis between 2 bounding boxes (the one from your chip and the one for an asteroid I presume). If you find one, that means there is a collision, if there is not, then there is a collision.
You can find a lot of documentation on the internet about that. Just search for Oriented Bounding Box (OBB).
Hope this helps  Grin
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #2 - Posted 2012-04-20 18:36:49 »

In addition to what Tim said, I'd like you to a thing I made here
http://www.java-gaming.org/topics/simplified-advanced-collision-detection-sat-v2-1-updated-8-4-2011/24585/msg/207464/view.html#msg207464
There are both simple and advanced examples there and all the source code is provided to use freely without question or footnotes.


If not that, I'd also look into AABB or Axis Aligned Bounding Boxes

Its quite a bit more complicated problem then simply rotating an image, as you most certainly have done.

Alternatively, you could use a combination of regular boxes and distance checks(circular distance) and not deal with the shapes themselves.


"Experience is what you get when you did not get what you wanted"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Axeman

Senior Member


Medals: 7



« Reply #3 - Posted 2012-04-20 19:24:47 »

Hi guys! Thanks for your replies. I'm sorry if i wasn't clear, but the collision isn't a problem. I've made a SAT collision for that. My problem is how to handle the rotation of the polygon I use to test with SAT. If I glRotate I'm not actually changing the polygon, just how it's displayed on screen. And therefor I'm thinking of the best way to deal with this problem. Smiley
Offline Tim Spekler
« Reply #4 - Posted 2012-04-20 19:38:35 »

Well, the more obvious way to deal with it is to calculate on each rotation all corner positions of your bounding element, so you can use your SAT on its new position. That's how I did for my projects.
But maybe you can use circular bounding area as namrog84 said (for asteroids for instance): this enables you to avoid calculating corners position, only center position is useful.  Tongue
Offline davedes
« Reply #5 - Posted 2012-04-20 19:48:23 »

So I'm guessing you already have some sort of Polygon object, which contains an array of points?

You can either rotate each point, or apply a rotation matrix to all the points.

Some code:
Look at the source of Slick's Transform class for an idea of matrix transformation.

Or check out Slick's drawEmbedded which simply rotates each point before sending it to glVertex (no matrix).

Some reading:
http://www.gamedev.net/topic/286454-simple-2d-point-rotation/
http://www.ia.hiof.no/~borres/cgraph/math/twod/p-twod.html
http://wally.cs.iupui.edu/n351/3D/matrix.html

From there you will have to determine how best to check for collision; i.e. determine whether the point is within the polygon, or determine whether it's within a bounding box/circle representing the ship, etc.

And since your points will already be rotated, you will no longer need glRotate when passing vertex data. Smiley

Offline Axeman

Senior Member


Medals: 7



« Reply #6 - Posted 2012-04-21 13:28:18 »

Thanks a bunch, all of you! I think I got a pretty good idea how to handle this now. Now I just have to put it down into code and move on to the next unanticipated problem. Smiley And thank you, Davedes for those links. They are great stuff! Smiley
Offline Axeman

Senior Member


Medals: 7



« Reply #7 - Posted 2012-04-21 15:24:05 »

I´ve got a spinning rect working now and I thought I should contribute to the forum with a code snippet, in case someone should be interested in the the same problem. Smiley

The method takes an "angle" in degrees. "AngleRad" gets the angle i radians. "numPoint" is number of vertices on the rectangle. As you can see the for loop iterates through the points of the rectangle and calculates the rotation for each vertex. Dx and dy only copies the coordinates from the rect[point] to make it a bit easier to read.

It should also be noted that I keep an "origin" variable outside of the method so I can translate the object to where I want it to rotate. Smiley

Disclaimer: I´m not saying it´s the best way or the most accurate way to rotate a polygon (a rectangle in this case). But it is a working spinning rectangle method and a code snippet might get someone on the right track. Smiley



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  
public void rotate(float angle) {
     
      double tempX;
      double tempY;
     
      float dx;
      float dy;
     
      for(int point = 0; point < numPoint; point++)
      {  
         
         double angleRad = Math.toRadians(angle);
         
         dx = rect[point].getX();
         dy = rect[point].getY();
         
         tempX = (dx * Math.cos(angleRad)) - (dy * Math.sin(angleRad));
         tempY = (dx * Math.sin(angleRad)) + (dy * Math.cos(angleRad));
         
     
         rect[point].setPoint((float)tempX, (float)tempY) ;
         
         
      }
   }


I hope it makes somebody happy. Smiley
Offline davedes
« Reply #8 - Posted 2012-04-23 02:19:27 »

Since cos/sin are somewhat heavy calculations (compared to simple addition/multiplication at least) you shouldn't be performing them more than necessary. Definitely not in each iteration of the loop!

Also look into a "FastMath" type alternative to Java's Math class (which can be a little slow). There are some utilities in the forums here for faster math as well as a FastMath class in Slick.

Offline Roquen
« Reply #9 - Posted 2012-04-23 06:37:06 »

Also: complex numbers are your friend.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Axeman

Senior Member


Medals: 7



« Reply #10 - Posted 2012-04-23 10:49:09 »

Davedes: My first priority is just to get things working and understand the concept of why it´s working, that´s why I´m using Math.sin/Math.cos: It´s just easy. Smiley But I´d be happy to learn more about how to exchange those into something else. One thought I had is to simply make a array for sin[360] and cos[360], and just use integer angles. Any ideas..?

Roquen: I´ve noticed that you seem to like to use complex numbers. I promise that I´ll look into it when I feel ready. I´m just not there yet... Smiley
Offline Roquen
« Reply #11 - Posted 2012-04-23 10:57:49 »

2D or 3D, regardless of what techniques you use, you're going to want to have the current orientation (rotation) of an entity stored in some manner.  You should never be recreating information (when it's hard) that you should just know.  Within the context of space transforms (translations, rotation, etc.)   you also want to be always working from "raw" untransformed data.
Offline Axeman

Senior Member


Medals: 7



« Reply #12 - Posted 2012-04-23 11:19:39 »

Roquen: I´m not sure I understand..? In my case I use a rectangle, I rotate the rectangle and then I overwrite the old position with the new, rotated, position. Is that what you mean when you say "store"?

And could you elaborate on what you mean by "raw untransformed data"?
Offline Roquen
« Reply #13 - Posted 2012-04-23 11:36:33 »

If you rectangle is store as four vertices and you transform and overwrite them each time, after awhile your rectangle will not longer be a rectangle.  Specifically after every transform the lengths of the edges and angles between them will change as floating point computations introduce error.  So you need to store "raw" untransformed info about the rectangle so that it doesn't change and use that to get the current information.

Personally I'd use storing the center (its translation), the positive distance to the upper right corner when unrotated and some rotational information (angle or complex number pair).
Offline Axeman

Senior Member


Medals: 7



« Reply #14 - Posted 2012-04-23 11:45:21 »

Ah, I see! Thanks, that´s fantastic advice. Cheesy I will rewrite my code now... Smiley

Ooh, just one more thing before I get started... How much is "Some rotational information"? Is it just a few key angles or the full 360? Smiley
Offline Roquen
« Reply #15 - Posted 2012-04-24 12:06:04 »

By "some" I simply meant: by "some" means...an angle, a complex number..whatever.  If using an angle IHMO it's worth getting used to using radians instead of degrees for runtime data.  Think about angles however you want.
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.

xsi3rr4x (31 views)
2014-04-15 18:08:23

BurntPizza (28 views)
2014-04-15 03:46:01

UprightPath (43 views)
2014-04-14 17:39:50

UprightPath (26 views)
2014-04-14 17:35:47

Porlus (43 views)
2014-04-14 15:48:38

tom_mai78101 (64 views)
2014-04-10 04:04:31

BurntPizza (124 views)
2014-04-08 23:06:04

tom_mai78101 (224 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!