Jump to content
  • Advertisement
phil67rpg

rotate using sin and cos

Recommended Posts

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();
}

 

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.
 

Share this post


Link to post
Share on other sites

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

Edited by phil67rpg

Share this post


Link to post
Share on other sites

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();
}

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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();
}

 

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!