Jump to content

  • Log In with Google      Sign In   
  • Create Account


Object Movement


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 AltarofScience   Members   -  Reputation: 933

Like
0Likes
Like

Posted 07 March 2012 - 06:47 PM

So I am writing a program as part of a larger game to do ships traveling and fighting.
I want the ship to move and be able to turn and move in the new direction.
I got the ship so that if I click a key to move it it rotates properly about its own y axis. Also I can set it so that it moves at pretty much any angle.
The problem is that when I change the angle it should rotate around its own axis so the front always points in the direction of travel. But instead of rotating around its own axis it rotates around the axis defined by its initial location plus any change to the location with a key press.
In the code the variable changed by key press is mx or mz. my never changes. Although it may later due to waves or something.

I want to know how to make it so that when I change the heading it rotates always around its current center point as the axis and it doesn't also change the position of the boat.
The boat should move every |double const ztm| increase and travel a distance based on the speed following the proper vector.

Here is my code:



#include <windows.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <GL/glut.h>
#include <stdlib.h>

using namespace std;

int WINDOW_WIDTH = 640, WINDOW_HEIGHT = 480;

/* GLUT callback Handlers */

void SetOrthoForFont()
{
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
	glScalef(1, -1, 1);
	glTranslatef(0, -WINDOW_HEIGHT, 0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void ResetPerspectiveProjection()
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}

void RenderSpacedBitmapString(
							  int x,
							  int y,
							  int spacing,
							  void *font,
							  char *string)
{
	char *c;
	int x1=x;
	for (c=string; *c != '\0'; c++) {
		glRasterPos2i(x1,y);
		glutBitmapCharacter(font, *c);
		x1 = x1 + glutBitmapWidth(font,*c) + spacing;
	}
}

void DrawAxes()
{
	//To prevent the view from disturbed on repaint
	//this push matrix call stores the current matrix state
	//and restores it once we are done with the arrow rendering
	glPushMatrix();
		glColor3f(0,0,1);
		glPushMatrix();
			glTranslatef(0,0, 0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			//Draw label
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Z");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);

		glColor3f(1,0,0);
		glRotatef(90,0,1,0);
		glPushMatrix();
			glTranslatef(0,0,0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			//Draw label
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "X");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);

		glColor3f(0,1,0);
		glRotatef(90,-1,0,0);
		glPushMatrix();
			glTranslatef(0,0, 0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			//Draw label
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Y");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);
	glPopMatrix();
}
void DrawGrid(int GRID_SIZE)
{
	glBegin(GL_LINES);
	glColor3f(0.75f, 0.75f, 0.75f);
	for(int i=-GRID_SIZE;i<=GRID_SIZE;i++)
	{
		glVertex3f((float)i,0,(float)-GRID_SIZE);
		glVertex3f((float)i,0,(float)GRID_SIZE);

		glVertex3f((float)-GRID_SIZE,0,(float)i);
		glVertex3f((float)GRID_SIZE,0,(float)i);
	}
	glEnd();
}

void OnReshape(int nw, int nh) {
	glViewport(0,0,nw, nh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60, (GLfloat)nw / (GLfloat)nh, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
}

int oldX = 0, oldY = 0, rX = 15, rY = 0, state = 0, dist = -10;

float mx = 0;
float my = 0;
float mz = 0;
float direction = 0;
float speed = 0;

static void display(void)
{

    const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double ztm = t*+0.1;
    float radians = direction * 3.141592653 / 180;
    float xPos = sin(radians) * speed;
    float zPos = cos(radians) * speed;
    float dmx = xPos*ztm;
    float dmz = zPos*ztm;
    float nmx = dmx+xPos;
    float nmz = dmz+zPos;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(-dmx,(-5+dmz)/4,dist-dmz);
	glRotatef(rX,1,0,0);
	glRotatef(rY,0,1,0);

	DrawAxes();
	DrawGrid(1000);

	glEnable(GL_LIGHTING);

    glColor3f(1,0,0);

    glPushMatrix();
    glPushMatrix();
        glScaled(1,0.5,1);
        glTranslated(mx,my,mz);
        glRotatef(direction,0,1,0);
        glutSolidCube(1);
    glPushMatrix();
        glScaled(1,1,2);
        glTranslated(0,0,-2.25);
        glutSolidCube(1);
    glPopMatrix();
    glPushMatrix();
        glScaled(1,1,2);
        glTranslated(0,0,2.25);
        glutSolidCube(1);
    glPopMatrix();
    glPushMatrix();
        glScaled(2,1,2);
        glTranslated(0,0,-1.25);
        glutSolidCube(1);
    glPopMatrix();
    glPushMatrix();
        glScaled(2,1,2);
        glTranslated(0,0,1.25);
        glutSolidCube(1);
    glPopMatrix();
    glPushMatrix();
        glScaled(3,1,3);
        glTranslated(0,0,0);
        glutSolidCube(1);
    glPopMatrix();
    glPopMatrix();
    glPopMatrix();

    float markerz = -3, markerorigin = 0;
    for (markerz;markerz<=3;markerz++) {
    glPushMatrix();
        glScaled(1.0,20.0,1.0);
        glTranslated(5,0,markerorigin+markerz*3);
        glRotated(ztm*90,0,1,0);
        glutSolidCube(1);
    glPopMatrix();

    glPushMatrix();
        glScaled(1.0,20.0,1.0);
        glTranslated(-5,0,markerorigin+markerz*3);
        glRotated(ztm*90,0,1,0);
        glutSolidCube(1);
    glPopMatrix();
    }

    glDisable(GL_LIGHTING);

    SetOrthoForFont();
	ResetPerspectiveProjection();
    glutSwapBuffers();
}

static void key(unsigned char key, int x, int y)
{
    if (key=='b')
    {
    //mx=mx+xPos;
    //mz=mz+zPos;
    } else if(key=='p') {
    direction=direction+.2;
    } else if (key=='o') {
    direction=direction-.2;
    } else if (key=='l') {
    direction=direction+2;
    } else if (key=='k') {
    direction=direction-2;
    } else if (key=='m') {
    speed=speed+.2;
    } else if (key=='n') {
    speed=speed-.2;
    } else if (key=='q') {
    mx=mx+1;
    } else if (key=='w') {
    mx=mx-1;
    } else {
    speed=0;
    }

    glutPostRedisplay();
}

void Mouse(int button, int s, int x, int y)
{
	if (s == GLUT_DOWN)
	{
		oldX = x;
		oldY = y;
	}

	if(button == GLUT_MIDDLE_BUTTON)
		state = 0;
	else
		state = 1;
}

void Motion(int x, int y)
{
	if (state == 0)
		dist *= (1 + (y - oldY)/60.0f);
	else
	{
		rY += (x - oldX)/5.0f;
		rX += (y - oldY)/5.0f;
	}
	oldX = x;
	oldY = y;

	glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");


    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutReshapeFunc(OnReshape);
    glutMouseFunc(Mouse);
	glutMotionFunc(Motion);
	glutKeyboardFunc(key);

    glClearColor(0,0,0,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

    return EXIT_SUCCESS;
}



Sponsor:

#2 sjaakiejj   Members   -  Reputation: 130

Like
0Likes
Like

Posted 07 March 2012 - 07:13 PM

Have you tried swapping the order of Rotated and Translated? I think that should do the trick

so instead of
  glTranslated(mx,my,mz);
 glRotatef(direction,0,1,0);

you do
 glRotatef(direction,0,1,0);
  glTranslated(mx,my,mz);


#3 AltarofScience   Members   -  Reputation: 933

Like
0Likes
Like

Posted 07 March 2012 - 07:21 PM

Have you tried swapping the order of Rotated and Translated? I think that should do the trick

so instead of

  glTranslated(mx,my,mz);
glRotatef(direction,0,1,0);

you do
glRotatef(direction,0,1,0);
  glTranslated(mx,my,mz);


That does not appear to do anything different as far as I can tell.

#4 sjaakiejj   Members   -  Reputation: 130

Like
0Likes
Like

Posted 07 March 2012 - 07:41 PM


Have you tried swapping the order of Rotated and Translated? I think that should do the trick

so instead of

  glTranslated(mx,my,mz);
glRotatef(direction,0,1,0);

you do
glRotatef(direction,0,1,0);
  glTranslated(mx,my,mz);


That does not appear to do anything different as far as I can tell.


You should definitely see something different, though its possible I pointed to the wrong pair, as I'm not quite sure which part represents your ship. Swapping the order means rotate first, then translate. Rotation should always be done at the origin, otherwise you get the exact effect you described, where the object rotates around the origin instead of its own axis.

#5 AltarofScience   Members   -  Reputation: 933

Like
0Likes
Like

Posted 07 March 2012 - 08:00 PM



Have you tried swapping the order of Rotated and Translated? I think that should do the trick

so instead of

  glTranslated(mx,my,mz);
glRotatef(direction,0,1,0);

you do
glRotatef(direction,0,1,0);
  glTranslated(mx,my,mz);


That does not appear to do anything different as far as I can tell.


You should definitely see something different, though its possible I pointed to the wrong pair, as I'm not quite sure which part represents your ship. Swapping the order means rotate first, then translate. Rotation should always be done at the origin, otherwise you get the exact effect you described, where the object rotates around the origin instead of its own axis.


I think it does something different but not the right thing.

Here is the entire set of code to describe the ship, I commented it to make it easier:
	glPushMatrix();						  //This matrix contains a base "object" so I can perform the translation, it its covered by a later shape.
		glScaled(1,0.5,1);				 //This applies the .5 scaling to all the following shapes.
		glRotatef(direction,0,1,0);	 //If the object is moved only by the changing of the mx,mz vars or not moved, this rotates the whole shape
		glTranslated(mx,my,mz);	   //This is the central point where the object is initially placed and that the other objects are translated from
		glutSolidCube(1);				 //This draws the "object" placeholder shape, I think I don't technically need to draw a cube here
	glPushMatrix();						 //Starts one of the shapes farthest from the base object
		glScaled(1,1,2);				   //Makes it 1 square on the X and 2 on the Z
		glTranslated(0,0,-2.25);	   //Moves it so that it is in the right place
		glutSolidCube(1);				 //Draws the unscaled cube
	glPopMatrix();						  //Pop
	glPushMatrix();					    //Starts the shape at the opposite end
		glScaled(1,1,2);				   //Scaling same as the previous shape
		glTranslated(0,0,2.25);	   //Moves it to the right spot
		glutSolidCube(1);			   //Draws the unscaled cube
	glPopMatrix();						 //Pop
	glPushMatrix();					   //Draws the shape between the first shape and the last shape which is at the center on 0,0,0
		glScaled(2,1,2);				  //This shape is a 2 by 2 square x and z
		glTranslated(0,0,-1.25);	 //Translates it to the right spot
		glutSolidCube(1);			   //Draws the unscaled cube
	glPopMatrix();						 //Pop
	glPushMatrix();					   //Draws the shape between the second shape and the last shape
		glScaled(2,1,2);				 //Scale
		glTranslated(0,0,1.25);	  //Placement
		glutSolidCube(1);			  //Draws the unscaled cube
	glPopMatrix();					    //Pop
	glPushMatrix();					  //Draws the shape centered at 0,0,0
		glScaled(3,1,3);			    //Scales the shape to 3x by 3 z
		glTranslated(0,0,0);		  //Translates the shape to 0,0,0
		glutSolidCube(1);			 //Draws the unscaled cube
	glPopMatrix();					  //Pop
	glPopMatrix();					  //Pop


#6 shacktar   Members   -  Reputation: 720

Like
0Likes
Like

Posted 07 March 2012 - 08:52 PM

Try loading the identity matrix after the first glPushMatrix.

i.e.

glPushMatrix();				 //This matrix contains a base "object" so I can perform the translation, it its covered by a later shape.
   glLoadIdentity();	 // //	/*Load Identity Here*/
   glScaled(1,0.5,1);			 //This applies the .5 scaling to all the following shapes.
   glRotatef(direction,0,1,0);		 //If the object is moved only by the changing of the mx,mz vars or not moved, this rotates the whole shape
   glTranslated(mx,my,mz);		 //This is the central point where the object is initially placed and that the other objects are translated from
   glutSolidCube(1);			 //This draws the "object" placeholder shape, I think I don't technically need to draw a cube here
   glPushMatrix();
	  ...


#7 AltarofScience   Members   -  Reputation: 933

Like
0Likes
Like

Posted 07 March 2012 - 08:56 PM

Try loading the identity matrix after the first glPushMatrix.

i.e.

glPushMatrix();				 //This matrix contains a base "object" so I can perform the translation, it its covered by a later shape.
   glLoadIdentity();	 // //	/*Load Identity Here*/
   glScaled(1,0.5,1);			 //This applies the .5 scaling to all the following shapes.
   glRotatef(direction,0,1,0);		 //If the object is moved only by the changing of the mx,mz vars or not moved, this rotates the whole shape
   glTranslated(mx,my,mz);		 //This is the central point where the object is initially placed and that the other objects are translated from
   glutSolidCube(1);			 //This draws the "object" placeholder shape, I think I don't technically need to draw a cube here
   glPushMatrix();
	  ...


Puts a rectangle that never changes position as I move the camera.

#8 shacktar   Members   -  Reputation: 720

Like
0Likes
Like

Posted 07 March 2012 - 09:43 PM

Is it at least moving correctly as mx, my, and direction change?

What if you apply the inverse of the camera translation before doing the rotation?

glPushMatrix();
   glTranslatef(dmx,-(-5+dmz)/4,-dist+dmz); //moving ship back to the origin
   glScaled(1,0.5,1);
   glRotatef(direction,0,1,0);
   glTranslated(mx,my,mz);


#9 AltarofScience   Members   -  Reputation: 933

Like
0Likes
Like

Posted 07 March 2012 - 10:07 PM

Okay, I figured out my problem. It has to do with the way I was using a const double to multiple the change with xPos and zPos.
That meant that it changed the position to a multiple of the new heading from the origin.
I have a new code where I can use keyboard input to move the boat and it responds perfectly.
The problem is I want the object to be able to move based on a velocity every time unit described by the const double ztm. How can I get it to move at float speed * xPos and float speed * zPos every time unit?
#include <windows.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <GL/glut.h>
#include <stdlib.h>

using namespace std;

int WINDOW_WIDTH = 640, WINDOW_HEIGHT = 480;

/* GLUT callback Handlers */

void SetOrthoForFont()
{
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
	glScalef(1, -1, 1);
	glTranslatef(0, -WINDOW_HEIGHT, 0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void ResetPerspectiveProjection()
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}

void RenderSpacedBitmapString(
							  int x,
							  int y,
							  int spacing,
							  void *font,
							  char *string)
{
	char *c;
	int x1=x;
	for (c=string; *c != '\0'; c++) {
		glRasterPos2i(x1,y);
		glutBitmapCharacter(font, *c);
		x1 = x1 + glutBitmapWidth(font,*c) + spacing;
	}
}

void DrawAxes()
{
	//To prevent the view from disturbed on repaint
	//this push matrix call stores the current matrix state
	//and restores it once we are done with the arrow rendering
	glPushMatrix();
		glColor3f(0,0,1);
		glPushMatrix();
			glTranslatef(0,0, 0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			//Draw label
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Z");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);

		glColor3f(1,0,0);
		glRotatef(90,0,1,0);
		glPushMatrix();
			glTranslatef(0,0,0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			//Draw label
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "X");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);

		glColor3f(0,1,0);
		glRotatef(90,-1,0,0);
		glPushMatrix();
			glTranslatef(0,0, 0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			//Draw label
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Y");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);
	glPopMatrix();
}
void DrawGrid(int GRID_SIZE)
{
	glBegin(GL_LINES);
	glColor3f(0.75f, 0.75f, 0.75f);
	for(int i=-GRID_SIZE;i<=GRID_SIZE;i++)
	{
		glVertex3f((float)i,0,(float)-GRID_SIZE);
		glVertex3f((float)i,0,(float)GRID_SIZE);

		glVertex3f((float)-GRID_SIZE,0,(float)i);
		glVertex3f((float)GRID_SIZE,0,(float)i);
	}
	glEnd();
}

void OnReshape(int nw, int nh) {
	glViewport(0,0,nw, nh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60, (GLfloat)nw / (GLfloat)nh, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
}

int oldX = 0, oldY = 0, rX = 15, rY = 0, state = 0, dist = -10;

float mx = 0;
float my = 0;
float mz = 0;
float direction = 0;
float speed = -1;

static void display(void)
{
	float radians = direction * 3.141592653 / 180;
	float xPos = cos(radians) * speed;
	float zPos = sin(radians) * speed;
	float dmx = 0;
	float dmz = 0;

	const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
	const double ztm = t*+0.1;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(-dmx,(-5+dmz)/4,dist-dmz);
	glRotatef(rX,1,0,0);
	glRotatef(rY,0,1,0);

	DrawAxes();
	DrawGrid(1000);

	glEnable(GL_LIGHTING);

	glColor3f(1,0,0);

	glPushMatrix();
	glPushMatrix();
		glScaled(1,0.5,1);
		glTranslated(mx,my,mz);
		glRotatef(direction,0,1,0);
	glPushMatrix();
		glScaled(1,1,2);
		glTranslated(0,0,-2.25);
		glutSolidCube(1);
	glPopMatrix();
	glPushMatrix();
		glScaled(1,1,2);
		glTranslated(0,0,2.25);
		glutSolidCube(1);
	glPopMatrix();
	glPushMatrix();
		glScaled(2,1,2);
		glTranslated(0,0,-1.25);
		glutSolidCube(1);
	glPopMatrix();
	glPushMatrix();
		glScaled(2,1,2);
		glTranslated(0,0,1.25);
		glutSolidCube(1);
	glPopMatrix();
	glPushMatrix();
		glScaled(3,1,3);
		glTranslated(0,0,0);
		glutSolidCube(1);
	glPopMatrix();
	glPopMatrix();
	glPopMatrix();

	float markerz = -3, markerorigin = 0;
	for (markerz;markerz<=3;markerz++) {
	glPushMatrix();
		glScaled(1.0,20.0,1.0);
		glTranslated(5,0,markerorigin+markerz*3);
		glRotated(ztm*90,0,1,0);
		glutSolidCube(1);
	glPopMatrix();

	glPushMatrix();
		glScaled(1.0,20.0,1.0);
		glTranslated(-5,0,markerorigin+markerz*3);
		glRotated(ztm*90,0,1,0);
		glutSolidCube(1);
	glPopMatrix();
	}

	glDisable(GL_LIGHTING);

	SetOrthoForFont();
	ResetPerspectiveProjection();
	glutSwapBuffers();
}

static void key(unsigned char key, int x, int y)
{
	float radians = direction * 3.141592653 / 180;
	float xPos = sin(radians) * speed;
	float zPos = cos(radians) * speed;
	if (key=='b') {
	mx=mx+xPos;
	mz=mz+zPos;
	} else if(key=='v') {
	mx=mx-xPos;
	mz=mz-zPos;
	} else if(key=='p') {
	direction=direction+.2;
	} else if (key=='o') {
	direction=direction-.2;
	} else if (key=='l') {
	direction=direction+2;
	} else if (key=='k') {
	direction=direction-2;
	} else if (key=='m') {
	speed=speed+.2;
	} else if (key=='n') {
	speed=speed-.2;
	} else if (key=='q') {
	mx=mx+1;
	mz=mz+1;
	} else if (key=='w') {
	mx=mx-1;
	mz=mz-1;
	} else {
	speed=0;
	}

	glutPostRedisplay();
}

void Mouse(int button, int s, int x, int y)
{
	if (s == GLUT_DOWN)
	{
		oldX = x;
		oldY = y;
	}

	if(button == GLUT_MIDDLE_BUTTON)
		state = 0;
	else
		state = 1;
}

void Motion(int x, int y)
{
	if (state == 0)
		dist *= (1 + (y - oldY)/60.0f);
	else
	{
		rY += (x - oldX)/5.0f;
		rX += (y - oldY)/5.0f;
	}
	oldX = x;
	oldY = y;

	glutPostRedisplay();
}

static void idle(void)
{
	glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]	= { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]	= { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);
	glutInitWindowPosition(10,10);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

	glutCreateWindow("GLUT Shapes");

	glutDisplayFunc(display);
	glutIdleFunc(idle);
	glutReshapeFunc(OnReshape);
	glutMouseFunc(Mouse);
	glutMotionFunc(Motion);
	glutKeyboardFunc(key);

	glClearColor(0,0,0,1);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	glEnable(GL_LIGHT0);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHTING);

	glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

	glutMainLoop();

	return EXIT_SUCCESS;
}


#10 AltarofScience   Members   -  Reputation: 933

Like
0Likes
Like

Posted 08 March 2012 - 10:08 AM

Turns out I am a total moron. I made a new function:
static void mover(void)
{
float radians = direction * 3.141592653 / 180;
float xPos = sin(radians) * speed;
float zPos = cos(radians) * speed;
mx = mx+xPos;
mz = mz+zPos;
}

And I stuck it in my display function and bam, it all works great.
There is only one problem. I have to put the radians and xpos and ypos variables in every single function or else it will only move the ship in the direction that has sin in it. Its pretty weird. Ah well.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS