opengl move space ship

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1057 Views - Last Post: 06 January 2018 - 04:25 PM Rate Topic: -----

#1 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

opengl move space ship

Posted 18 November 2017 - 06:46 PM

well I have worked on this problem for a long time. I have read the opengl superbible 4th ed. It took me 3 weeks to read this text. I learned a lot from this book. I am also working on a small graphical project. In my project have got a drawing of a space ship. I am able to rotate my ship using the left/right keys. I want to move the ship up/down using the up/down keys. I want to move the ship like in asteroids with the movement to follow the nose/tail of the ship. I have also done a lot of research on this problem. here is a snippet of the code I am developing.
void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective
	glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
	glTranslatef(0.0f, 0.0f, -20.0f); //Move forward 20 units
	glColor3f(1.0f, 0.0f, 0.0f);
	glPushMatrix(); //Save the transformations performed thus far
	glTranslatef(8.0f, 0.0f, 0.0f);//move space ship 
	glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis
	
	glBegin(GL_LINE_LOOP);
	
	//Space Ship
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(-0.25f, -0.5f, 0.0f);
	glVertex3f(-0.75f, -0.5f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.75f, -0.5f, 0.0f);
	glVertex3f(0.25f, -0.5f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	glEnd();
	
	glPopMatrix(); //Undo the move to the center of the ship
	
	glutSwapBuffers();
}


I hope this is a valid question. let me know if you need more code.

Is This A Good Question/Topic? 0
  • +

Replies To: opengl move space ship

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 06:47 PM

You didn't actually ask a question. So what's up? What do you want to have happen? What is happening? Etc.
Was This Post Helpful? 0
  • +
  • -

#3 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 18 November 2017 - 06:56 PM

well my question is how do I move a space ship like in asteroids. I want to move up and down following the nose and tail of the ship. I can draw and rotate the ship using the arrow keys. I have tried using the glrotatef and gltranslate commands in various places in my code but still cannot get the ship to move up and down properly. this is probably a very simple solution but I still cannot see it. that is why another pair of eyes might help.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 07:06 PM

I would imagine you just increment the x and y and use the translatef.

glTranslatef(x, y, 0f);

then push it.

Ex: https://www.youtube....h?v=JE09UNZapKk
Was This Post Helpful? 0
  • +
  • -

#5 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 18 November 2017 - 07:09 PM

where in my code would I put the translate command. I also have 2 translate commands already in my code.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 07:12 PM

For me it would be after tracking the key input.. knowing which was triggered by setting a boolean.. and then the update would have any new x, y, roation, etc.. then have one translate and one rotate before a push.

You'll have to figure out that flow with what ever code you have there and/or not shown.
Was This Post Helpful? 0
  • +
  • -

#7 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 18 November 2017 - 08:09 PM

well I have almost solved my problem. I got my ship to move up and down while following the nose/tail. I have also got it to rotate around the z-axis. but when I move up or down it rotate around the origin and I want it to rotate around the center of the ship. here is my code
//Draws the 3D scene
void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective
	glTranslatef(0.0f, 0.0f, -20.0f); //Move forward 20 units
	glRotatef(_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
	glColor3f(1.0f, 0.0f, 0.0f);
	glPushMatrix(); //Save the transformations performed thus far
	glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis
	glTranslatef(0.0f, y, 0.0f);//move space ship 

	glBegin(GL_LINE_LOOP);
	
	//Space Ship
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(-0.25f, -0.5f, 0.0f);
	glVertex3f(-0.75f, -0.5f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.75f, -0.5f, 0.0f);
	glVertex3f(0.25f, -0.5f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);

	glEnd();
	
	glPopMatrix(); //Undo the move to the center of the ship	
	glutSwapBuffers();
}

void handleSpecialKeypress(int key, int , int)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		_angle += 1.0f;
		break;
	case GLUT_KEY_RIGHT:
		_angle -= 1.0f;
		break;
	case GLUT_KEY_UP:
		y += 0.5f;
		break;
	case GLUT_KEY_DOWN:
		y -= 0.5f;
		break;
	}
	glutPostRedisplay();
}


I just need a little help, I am very close to solving my problem.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 08:22 PM

Okay.. since I can't run that code please explain to me *HOW* it is rotating right now.
Was This Post Helpful? 0
  • +
  • -

#9 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 18 November 2017 - 08:26 PM

OK it is rotating around the origin but not around the center of the ship after it is moved.it rotates like it is on a swing arm.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 08:28 PM

If it were me I would look at doing the math to calculate the 'center point' for the x and y - from the height and width... bonus points if they are the same value - and use that in the rotation function as well.
Was This Post Helpful? 0
  • +
  • -

#11 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 18 November 2017 - 08:31 PM

are you referring to the distance formula sqrt(x*x + y*y) or the cos and sin functions?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 08:36 PM

Ah.. no. Start simple. If you have a sqare that is M height and M width, how do you find the center point. M/2, M/2.
Was This Post Helpful? 0
  • +
  • -

#13 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 18 November 2017 - 09:13 PM

well I have done some research and found that centerX = x + 1/2*width and center+Y = y + 1/2*height.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,542
  • Joined: 12-June 08

Re: opengl move space ship

Posted 18 November 2017 - 10:29 PM

Coolio. Do whatcha think you need to do.
Was This Post Helpful? 0
  • +
  • -

#15 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 308
  • Joined: 22-October 13

Re: opengl move space ship

Posted 19 November 2017 - 04:04 PM

what do I do with the formula that finds the center of a square?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2