PDA

View Full Version : 3D Game - When any arrow key is pressed the screen goes smaller then dissapears



donmitz
12-23-2012, 11:18 AM
I have made a 3d game when any arrow buttons are pressed the ball is meant to travel that way it was working until i put the aim targets on the wall after that it decided to stop working, instead the screen goes small first time a arrow button is pressed then after second press screen goes all black why is this

929930931


Heres some code that could help

GLfloat ballSpin = 0.0f;

GLfloat ballXpos = 40.0f;
GLfloat ballYpos = -30.0;
GLfloat ballZpos = 180.0;

GLfloat ballZspeed = 8.0f;
GLfloat ballXspeed = 2.0f;

GLfloat tempXpos = 40.0f;
GLfloat tempYpos = -30.0;
GLfloat tempZpos = 180.0;


void specialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP){
ballZpos = ballZpos - ballZspeed;
ballSpin -= 90.0f;
glutPostRedisplay();
}

if(key == GLUT_KEY_DOWN){
ballZpos = ballZpos + ballZspeed;
ballSpin += 90.0f;
glutPostRedisplay();
}


if(key == GLUT_KEY_LEFT){
ballXpos = ballXpos - ballXspeed;
ballSpin += 45.0f;
glutPostRedisplay();
}

if(key == GLUT_KEY_RIGHT){
ballXpos = ballXpos + ballXspeed;
ballSpin -= 45.0f;
glutPostRedisplay();
}
}


void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
sky(skylight);
frontWall(bricks);
leftWall(bricks);
rightWall(bricks);
floor(grass);

eightPoints();
sixPoints();
fivePoints();

glPushMatrix();
glTranslatef(ballXpos, ballYpos, ballZpos);
glRotatef(ballSpin, 1.0, 1.0, 1.0);
ball(football);
glPopMatrix();
glFlush();

glutSwapBuffers();

}





Also can anybody tell me how I would code this for the when i press the z key it should shoot the ball toward the z axis, and if i use any of the arrows it will aim towards that angle.

Carmine
12-23-2012, 04:58 PM
Try adding a couple of lines to your RenderScene function - as shown below ...


void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

sky (skylight);
frontWall (bricks);
leftWall (bricks);
rightWall (bricks);
floor (grass);
.
.
.

donmitz
12-23-2012, 05:11 PM
Yes that worked thanks, but i also found out another way.

I was playing around with it and i put:
eightPoints();
sixPoints();
fivePoints();

between
glPushMatrix();
glTranslatef(ballXpos, ballYpos, ballZpos);

and it also worked, do you know if there is any difference between the way i done it compared to your metod ??

Thanks again

Carmine
12-23-2012, 10:38 PM
Yes that worked thanks, but i also found out another way.

I was playing around with it and i put:
eightPoints();
sixPoints();
fivePoints();

between
glPushMatrix();
glTranslatef(ballXpos, ballYpos, ballZpos);

and it also worked, do you know if there is any difference between the way i done it compared to your metod ??

Thanks again

What you are doing is different from what I'm doing.
You are sort of lucky that your way works.
It's a good idea to initialize the matrix stack each time you redraw a scene.
If you don't, modeling transformations can compound on each redraw.