openGL, rotation, collision

Started by
6 comments, last by L. Spiro 9 years, 10 months ago

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(){
		glLoadIdentity();
		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

Advertisement

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.

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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

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(){
		glLoadIdentity();
		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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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(){
		glLoadIdentity();
		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.

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

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement