Advertisement

rotate using sin and cos

Started by November 03, 2019 10:13 PM
13 comments, last by Bozemoto 5 years, 1 month ago

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

 

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.
Advertisement

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.

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.
 

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

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

 

Advertisement

I finally solved my problem you can close this thread.

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

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

 

you can close this thread

This topic is closed to new replies.

Advertisement