Skip to main content
GameDev.net gamedev.net
🔒 Locked

rotate using sin and cos

Started by phil67rpg Nov 3, 2019 at 10:13 PM 13 replies 14.6k views
Original Post
phil67rpg
phil67rpg

I am attempting to get a small figure to rotate using sin and cos. here is a code snippet.


void drawShip()
{
	glPushMatrix();
	glColor3f(1.0f, 0.0f, 0.0f);
	glTranslatef(50.0f, 0.0f, 0.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(0.0f+(cos(shipAngle)), 0.0f+(sin(shipAngle)) , 0.0f);
	glVertex3f(-5.0f+(cos(shipAngle)), -5.0f+(sin(shipAngle)), 0.0f);
	glVertex3f(0.0f+(cos(shipAngle)), 10.0f+(sin(shipAngle)), 0.0f);
	glVertex3f(5.0f+(cos(shipAngle)), -5.0f+(sin(shipAngle)), 0.0f);
	glEnd();
	glPopMatrix();
}


Wyrframe
Wyrframe

Good news, Phil; I can see what isn't working.

Bad news, Phil; you didn't ask any questions.


I think you would really benefit from learning a cognitive aid called "Rubber Duck Debugging". When you encounter a problem, you bring out the duck, sit it on the desk, and patiently explain your problem to it, step by step.

Eventually, in the course of explaining it to the duck, you are likely to notice something that contradicts your approach to the problem, and thus can start re-thinking it... or you are likely to realize that you don't understand the solution, and can re-examine how you got to the point you did.


One edge case of the practice, however: if you successfully explain the entire problem and the unexpected results to the duck, and it acknowledges to you that it understands... see a shrink.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
phil67rpg
phil67rpg

that is a cute exercise, my question is how do I get a drawing to rotate around the z-axis using only the sin and cos functions. also I will get a rubber duck. I have tried multiplying the sin and cos by the figure points and it draws itself inside out.

PeterStock
PeterStock

I think you may want this:



	float offsets[4][2] = {{0.0f, 0.0f}, {-5.0f, -5.0f}, {0.0f, 10.0f}, {5.0f, -5.0f}};
	float cosA = cos(shipAngle);
	float sinA = sin(shipAngle);
	for (i=0; i<4; i++)
	    glVertex3f(offsets[i][0]*cosA - offsets[i][1]*sinA, offsets[i][0]*sinA + offsets[i][1]*cosA, 0.0f);
	


Or you could use glRotate, like you already do with glTranslate.

phil67rpg
phil67rpg

well this rotation code works all I want to do is to get my ship to move up and down following its nose.

phil67rpg
phil67rpg

can I get some help on getting my ship to move up and down following its nose.


void drawShip()
{
	glPushMatrix();
	glColor3f(1.0f, 0.0f, 0.0f);
	glTranslatef(50.0f, 0.0f, 0.0f);
	glTranslatef(cos(shipAngle)*thrust, sin(shipAngle)*thrust, 0.0f);
	glBegin(GL_LINE_LOOP);
	float offsets[4][2] = { {0.0f, 0.0f}, {-5.0f, -5.0f}, {0.0f, 10.0f}, {5.0f, -5.0f} };
	float cosA = cos(shipAngle);
	float sinA = sin(shipAngle);
	for (int i = 0; i < 4; i++)
		glVertex3f(offsets[i][0] * cosA - offsets[i][1] * sinA, offsets[i][0] * sinA + offsets[i][1] * cosA, 0.0f);
	glEnd();
	glPopMatrix();
}


phil67rpg
phil67rpg

I finally solved my problem you can close this thread.

Rutin
Rutin
2 hours ago, phil67rpg said:

I finally solved my problem you can close this thread.

How did you solve your problem? You should post your solution and bookmark it for future.

Programmer and 3D Artist
phil67rpg
phil67rpg

well I used the following code.


void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		shipAngle+=PI/8;
		break;
	case GLUT_KEY_RIGHT:
		shipAngle-=PI/8;
		break;
	case GLUT_KEY_UP:
		dx += -2.0f*sin(shipAngle);
		dy += 2.0f*cos(shipAngle);
		break;
	case GLUT_KEY_DOWN:
		dx += 2.0f*sin(shipAngle);
		dy += -2.0f*cos(shipAngle);
		break;
	}
	glutPostRedisplay();
}


phil67rpg
phil67rpg

you can close this thread

phil67rpg
phil67rpg

you can close this thread.

Lactose
Lactose
1 minute ago, phil67rpg said:

you can close this thread.

Read rule 7 of this forum, which you've posted in for years and years.

Hello to all my stalkers.
phil67rpg
phil67rpg

sorry lactose

Bozemoto
Bozemoto

Just in case someone trips over this post in the future, some useful math when thinking about this kind of stuff.
sin and cos give you a x or y offset on the unit circle, so if you use both then you have a 2D vector.


vec2 dir = { cos(ang), sin(ang) };

Now if you want a vector that is orthogonal to this one you swap the X and Y round and put a negative on one of them. Which one will decide it it's rotated 90 degrees to the left or right.


vec2 orth = { dir.y, -dir.x };

Once you have this you can use these you can use them to make a rectangle like so


vec2 pos[4] = { {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f} }
vec2 rotated_pos[4] = {};
for(int i=0; i<4; ++i)
{
	rotated_pos[i] = (pos[i].x * orth) + (pos[i].y * dir);
}

Think this is what you're doing but wanted to try and provide some clarity to the topic.

Video Game Programmer.
5 years in industry.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.