Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am developing a game in 2D using LWJGL library.

So far I have a rotating box. I have done basic Rectangle collision, but it doesn't work for rotated rectangles.

Does OpenGL have a function that returns the vertices of rotated rectangle? Or is there another way of doing this using trigonometry?

I had researched how to do this and everything I found was using some matrix that I don't understand so I am asking if there is another way of doing this.

For clarification, I am trying to find out the true (rotated) X,Y of each point of the rectangle. Let's say, the first point of a rectangle (top,left) has x=10 y=10.. Width and height is 100 pixels. When I rotate the rectangle using glRotatef() the x and y stay the same. The rotation is happening inside OpenGL. I need to extract the x,y of the rectangle so I can detect collisions properly.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

OpenGL just does the displaying, it doesn't do these kind of jobs for you.

One way to rotate points is to calculate the angle and distance to the point around which it should be rotated, applying the rotation to the angle and then converting the angle and distance back to a coordinate with sin and cos.

x and y are relative to the center of the object.

angle = atan2(y, x);
distance = sqrt(x*x + y*y);
angle += rotation;
rotatedX = sin(angle)*distance;
rotatedY = cos(angle)*distance;
share|improve this answer
is this x and y of the top-left corner of the rectangle? Because I translate it to x+w/2, y+h/2 then rotate followed by translate by -w/2, -h/2. So your x, y should be x+w/2 and y+h/2 to get the middle point is that right? –  matejkramny Oct 5 '12 at 22:58
no, x and y is the position of the current point relative to the center. You'll need to calculate the position of every point. This is not specific to rectangles, you can rotate any shape with that. –  API-Beast Oct 5 '12 at 23:28
Thank you very much.. Works perfect –  matejkramny Oct 6 '12 at 15:38

The hard answer is that it involves matrix multiplication. I sat here trying to think of a way to answer it otherwise, but I don't know of a way. This said, the good news is that basic vector mathematics is not hard at all. In fact, I have a great resource: 3D Math Primer for Graphics and Game Development

It is not only a good book on math, but a great book on game development in general. The foundation of all 2D and 3D games using objects involves basic vectors and angles, so it's very good to know.

Lecture aside (I know I hate when people do this to me!) this is what you have to do in broad strokes:

Moving objects have to be accounted for before they collide and before its viewable on the screen. There is something called the swap chain, during which images are rendered and then posted. What you'll have to do is create a bound sphere or some sort of collision detection mechanism that checks whether or not the rectangle is nearby. Unfortunately again, I cannot explain binding spheres or anything else about it without some math.

On a side note, it's part of the business. And it's not like it is in school! Check out the book, I think it will answer all your questions. And if it doesn't, the book Real-Time Collision Detection sure will.

share|improve this answer
Good books although first book because costs less $ than £.. Amazon--. They are looking very interesting thank you –  matejkramny Oct 5 '12 at 22:59

OpenGL has something called transform feedback, it allows you to capture output returned from the vertex/geometry shaders into a buffer.

Transform feedback is somewhat new, it has been in OpenGL since 3.0 so it might not be usable for you if you require compatibility with older systems (such as Mac OSX from a few versions ago).

Also you should note that glRotatef is now an outdated function, you are doing things in a deprecated inefficiant way.

I recommend you look at this post I made that goes into more detail about OpenGL 3.x+.

Having said that, this seems like a poor way to do collision detection. Might I suggest you look at embedding a proper physics engine, I recommend Bullet. You will get much more than collision detection out of it.

share|improve this answer
I am a beginner in OpenGL and using version 1.1 (yes, very old i know.. but I have to begin somewhere) –  matejkramny Oct 5 '12 at 15:53
I don't like to go into game engines until I know how to do everything myself. –  matejkramny Oct 5 '12 at 15:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.