Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (687)
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 4598 times)
0 Members and 1 Guest are viewing this topic.
Offline Axeman

Senior Devvie

Medals: 7

« Posted 2012-04-20 13: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

« JGO Spiffy Duke »

Medals: 58
Projects: 2
Exp: 4 years

« Reply #1 - Posted 2012-04-20 16: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 16:36:49 »

In addition to what Tim said, I'd like you to a thing I made here
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 Devvie

Medals: 7

« Reply #3 - Posted 2012-04-20 17: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

« JGO Spiffy Duke »

Medals: 58
Projects: 2
Exp: 4 years

« Reply #4 - Posted 2012-04-20 17: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 17: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:

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 Devvie

Medals: 7

« Reply #6 - Posted 2012-04-21 11: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 Devvie

Medals: 7

« Reply #7 - Posted 2012-04-21 13: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

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 00: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.

Online Roquen
« Reply #9 - Posted 2012-04-23 04: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 Devvie

Medals: 7

« Reply #10 - Posted 2012-04-23 08: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
Online Roquen
« Reply #11 - Posted 2012-04-23 08: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 Devvie

Medals: 7

« Reply #12 - Posted 2012-04-23 09: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"?
Online Roquen
« Reply #13 - Posted 2012-04-23 09: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 Devvie

Medals: 7

« Reply #14 - Posted 2012-04-23 09: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
Online Roquen
« Reply #15 - Posted 2012-04-24 10:06:04 »

By "some" I simply meant: by "some" 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.

Dwinin (73 views)
2015-11-07 13:29:08

Rems19 (81 views)
2015-10-31 01:36:56

Rems19 (78 views)
2015-10-31 01:32:37

williamwoles (107 views)
2015-10-23 10:42:59

williamwoles (93 views)
2015-10-23 10:42:45

Jervac_ (111 views)
2015-10-18 23:29:12

DarkCart (135 views)
2015-10-16 00:58:11

KaiHH (117 views)
2015-10-11 14:10:14

KaiHH (157 views)
2015-10-11 13:26:18

BurntPizza (172 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 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‑
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!