🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

asteroids ship movement

Started by
8 comments, last by Green_Baron 4 years, 9 months ago

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

 

Advertisement

You have claimed you solved this over a year ago.

Hello to all my stalkers.

 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.

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.

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.

Hello to all my stalkers.

yes I am still stuck on the movement problem.

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

well I am doing some research on google and my old threads.

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.

This topic is closed to new replies.

Advertisement