Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

asteroids ship movement

Recommended Posts

well I am attempting to move a ship like in asteroids. my question is how do I move a figure ship that follows its nose. it now moves strictly up and down. here is my code.

#include<freeglut.h>
#include<iostream>
#include<math.h>

using namespace std;

float shipenemyAngle = 0.0f;
float shipAngle = 0.0f;
float thrust = 0.0f;

const float PI = 3.1415926;

void drawShip()
{
	glPushMatrix();
	glColor3f(1.0f, 0.0f, 0.0f);
	glTranslatef(50.0f, 0.0f, 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();
}

void drawEnemyShip()
{
	glPushMatrix();
	glColor3f(0.0f, 1.0f, 0.0f);
	glLoadIdentity();
	glTranslatef(-50.0f, 0.0f, 0.0f);
	glRotatef(shipenemyAngle, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(-5.0f, -5.0f, 0.0f);
	glVertex3f(0.0f, 10.0f, 0.0f);
	glVertex3f(5.0f, -5.0f, 0.0f);
	glEnd();
	glPopMatrix();
}

void renderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	drawShip();
	drawEnemyShip();
	glPopMatrix();
	glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat aspectRatio;
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if (w <= h)
		glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 'a':
		shipenemyAngle+=PI/8;
		break;
	case 'd':
		shipenemyAngle-=PI/8;
		break;
	case 'w':

		break;
	case 'x':

		break;
	}
	glutPostRedisplay();
}

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:
		glTranslatef(0.0f, 1.0f, 0.0f);
		break;
	case GLUT_KEY_DOWN:
		glTranslatef(0.0f, -1.0f, 0.0f);
		break;
	}
	glutPostRedisplay();
}

int main(int argc, char**argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE);
	glutInitWindowPosition(600, 400);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Space Wars");
	glutDisplayFunc(renderScene);
	glutKeyboardFunc(handleKeypress);
	glutSpecialFunc(handleSpecialKeypress);
	glutReshapeFunc(ChangeSize);
	glutMainLoop();
}

 

Edited by phil67rpg

Share this post


Link to post
Share on other sites
Advertisement

 well I looked through my old threads and have solved my bullet shooting problem but not my  movement problem. if I am wrong send me the link.

Share this post


Link to post
Share on other sites

You should look through your recent threads instead Phil, because apparently you haven't solved the root problem yet. I'm starting to think you are just taking us for a walk.

Share this post


Link to post
Share on other sites
11 minutes ago, phil67rpg said:

 well I looked through my old threads and have solved my bullet shooting problem but not my  movement problem. if I am wrong send me the link.

For once, try doing some research on your own.

Share this post


Link to post
Share on other sites

How long shall it go on like this ? You post the same lines of codes and the same questions over and again with no sign of actual understanding. You must learn to solve your problems by yourself and not demand help every time you stumble upon a step like a boolean won't flip or a comparison goes wrong. There are more than enough tutorials on opengl 1 and 2, with extensive examples and pages after pages of explanations of how these things work.

We can't do better than these. Well, i can't ...

Edited by Green_Baron
typo

Share this post


Link to post
Share on other sites

I believe this keeps you running in circles. You will not find anything new in your old threads.

I did a brief search and found this that appears to me as being close enough to your setup (it is one of a series that explains the "historic" opengl and uses glut). Don't necessarily program things, just read and understand.

And you must be able to do the abstraction from a toturial that shows you how a specific thing works to actually implementing it in your use case. That is an ability no tutorial or online help can convey and comes after understanding the technique. First understand how it works (rotation martix, axis rotation and all that), then how to apply it to your ship.

And when you're thorugh with these, then the real problems show up. But that must yet be seen.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • 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!