• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# openGL, rotation, collision

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1too_many_stars  Members

Posted 03 June 2014 - 02:19 PM

Hi Guys,

Have a bit a canudrum. I have lines made up of a vector called 'start' and 'end'

These lines are in an array which form a rectangle, which is then rotated about the center of the screen thus...

void showLine(){
glTranslatef(SCREEN_WIDTH/2,SCREEN_WIDTH/2,0.0f);
glRotatef(rot,0.0f,0.0f,1.0f);
glTranslatef(-SCREEN_WIDTH/2,-SCREEN_WIDTH/2,0.0f);
glBegin(GL_LINES);
glColor3f(1.0f,0.5f,0.3f);
glVertex2f(start.x,start.y);
glVertex2f(end.x,end.y);

glEnd();

}//this is only one line segment


I have a collision function (line segment vs circle) all figured out, but as I loop through the simulation, i am not sure how to update my two line vectors, start.x, start.y, end.x, end.y with GL's rotation algorithm. I need to find some kind of relationship.

If you know how to solve this problem please let me know.

Thanks,

Mike

### #2ilreh  Members

Posted 03 June 2014 - 02:46 PM

You need to multiply the line points with the rotation matrix. You can get the current matrix with glGetFloatv and store it into an array. I'm not quite sure how to get only the rotation matrix but you could try storing the current matrix and calling glLoadIdentity before the rotation (resets the matrix). If you know the angle you could easily do this using basic math, without gl.

Edited by ilreh, 03 June 2014 - 02:47 PM.

### #3L. Spiro  Members

Posted 03 June 2014 - 06:45 PM

Your ordering of translations and rotations is not only wrong, but extremely strange.
Why are you doing the second translate? It serves only the purpose of ensuring most of the time you won’t see anything (which seems to be your problem).

Load identity and then stop messing with translations and rotations (leave it as identity).
Put the line points where you actually want them in screen coordinates.

Ensure you have prepared a projection matrix of the orthogonal kind.

L. Spiro

### #4too_many_stars  Members

Posted 03 June 2014 - 07:07 PM

Hi Spiro,

For some arbitrary center of rotation, with arbitrary number of lines in an object doing the rotating, I don't know how to figure out where the points are supposed to be, unless I use cosx, siny for a unit circle type of scenario.

Let's say I have an asteroid with 50 points, that's a lot of resources it seems to figure out for each iteration where 49 lines are. My question is can I use glRotation (since it is already crunching all the numbers and knows where every pixel is located) to figure out my: start.x,start.y,end.x,end.y for all lines in an object.

You will have to pardon me if the above code example is wrong, I have looked on line and that's what appears to be the common method. I am also very new to openGL 2.x

Thanks,

Mike

### #5L. Spiro  Members

Posted 03 June 2014 - 08:26 PM

If all the points are around the origin if the object and you are drawing something like Asteroids, rotate first, then translate. Once.

	void showLine(){
glRotatef(0.0f,0.0f,rot);				// glRotatef() has 3 parameters.  Rotate around Z.
glTranslatef(SCREEN_WIDTH/2,SCREEN_WIDTH/2,0.0f);	// Then move to the center of the screen.

// Or move to the position of the object relative to the center of the screen.
//glTranslatef(SCREEN_WIDTH/2+objPos.x,SCREEN_WIDTH/2+objPos.x,0.0f);

glBegin(GL_LINES);
glColor3f(1.0f,0.5f,0.3f);
glVertex2f(start.x,start.y);
glVertex2f(end.x,end.y);

glEnd();

}//this is only one line segment


I have looked on line and that's what appears to be the common method.

It was common perhaps 20 years ago. It’s outdated and deprecated, meaning no longer supported and not the common way anymore.

L. Spiro

Edited by L. Spiro, 03 June 2014 - 08:30 PM.

### #6RobTheBloke  Members

Posted 04 June 2014 - 08:46 AM

If all the points are around the origin if the object and you are drawing something like Asteroids, rotate first, then translate. Once.

	void showLine(){
glRotatef(0.0f,0.0f,rot);				// glRotatef() has 3 parameters.  Rotate around Z.
glTranslatef(SCREEN_WIDTH/2,SCREEN_WIDTH/2,0.0f);	// Then move to the center of the screen.

// Or move to the position of the object relative to the center of the screen.
//glTranslatef(SCREEN_WIDTH/2+objPos.x,SCREEN_WIDTH/2+objPos.x,0.0f);

glBegin(GL_LINES);
glColor3f(1.0f,0.5f,0.3f);
glVertex2f(start.x,start.y);
glVertex2f(end.x,end.y);

glEnd();

}//this is only one line segment


I have looked on line and that's what appears to be the common method.

It was common perhaps 20 years ago. It’s outdated and deprecated, meaning no longer supported and not the common way anymore.

L. Spiro

Spiro, the original code was correct. You might not like what he's doing, but he is simply changing a pivot. That is a standard approach, the runtime cost is negligible, and whilst it might not be something I'd recommend, it isn't actually wrong at all. The original code is also using glRotatef correctly (it does not take 3 parameters - it takes 4 - The angle, and the axis. Any rotate function that takes 3 args, is a gimbal-locking function best avoided!).

Let's say I have an asteroid with 50 points, that's a lot of resources it seems to figure out for each iteration where 49 lines are. My question is can I use glRotation (since it is already crunching all the numbers and knows where every pixel is located) to figure out my: start.x,start.y,end.x,end.y for all lines in an object.

You won't be able to use glRotate, but you can use a matrix to transform the points instead. (it might be worth computing your own transforms, and glMultMatrix instead of glRotatef/glTranslatef). CPU's are faster than you think, so there should be no significant performance problem in doing that. Just be aware that if you want to port you code to GL3+ at any point, you'll need to do your own matrix calculations anyway. You might as well start now - it'll be less hassle in the future.

### #7too_many_stars  Members

Posted 04 June 2014 - 12:06 PM

Thanks for the advice guys, I have revamped my code to make it cleaner and working on the point transforms right now.

Before I started working with openGL 2.x I knew that it's outdated. However, a number of people recommended to start with 2.x (as it's easier than 3+) to get used to the 3d co-ordinates so I chose that as my immersion.

Thanks,

Mike

### #8L. Spiro  Members

Posted 04 June 2014 - 02:55 PM

Spiro, the original code was correct. You might not like what he's doing, but he is simply changing a pivot.

He is changing the pivot to the center of the screen. It’s not correct. If the object’s pivot was not [0, 0], yes, translate by negative pivot (which is another problem in his original code), but the negative center of the screen (the pivot point derived from his positive half-screen translations) is very very unlikely to be the pivot point.
This is what I meant when I said it ensures most of the time he won’t see anything. Object starts at lower-left, moves to the center of the screen, spins around the [0, 0] origin (making a wide circular sweep), and the moves back to the [0, 0] origin, making it 75%-of-the-time off the screen to the left, bottom, or bottom-left.

The original code is also using glRotatef correctly

I misread the prototype. So on this point his original code is correct.

L. Spiro

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.