PDA

View Full Version : How to animate so the ball keeps on moving and how to scale per keyboard input



Tezelia
01-01-2013, 02:38 PM
How DO I animate so the ball keeps on moving and how do i scale per keyboard input?


1) Below is a code which moves the ball towards the target once the z key is pressed incrementing it once per click so I have to keep on pressing z. How can I just animate the ball so it continues until it hits the target?

void keyPressed (unsigned char key, int x, int y) {

if (key == 'z') {

Ballz = Ballz - Speed;
Bally = Bally + 2.8;

}

I tried

Ballx++
Bally++
Bally++

But even that doesn't animate



2) Basically we have to add two levers/bars or rectangles so for example when i press the left key the lever is supposed to increase on the x axis showing the user is aiming left

I have 2 problems in this section, I've done both levers or bars but its only displayed on the scene when it's on line loop and not polygon...

b) I'm trying to scale the bars every time a left or right arrow key is pressed, so for example if I press left the x axis on the leftrightbar has to scale towards the left, how can this be done?

What would I put in the special keys section, I tried putting

if(key == GLUT_KEY_LEFT){
Ballx = Ballx - Speed;
leftrightbar == glScalef (2.0, 4.0, 4.0);

leftrightbar is the name of my function for the horizontal bar...


here's the code for the bars (important parts highlighted)

void updownbar(){
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 1.0);
//specify the vertices (points in 3D space) of the shape - note that these are 2D points

glVertex2f( 0.0, 0.0);
glVertex2f( 0.0, 20.0);
glVertex2f( 4.0, 20.0);
glVertex2f( 4.0, 0.0);
glEnd();

glFlush();

}

////////////////////////////
void leftrightbar(){
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 0.0);
//specify the vertices (points in 3D space) of the shape - note that these are 2D points

glVertex2f( 0.0, 0.0);
glVertex2f( 0.0, 4.0);
glVertex2f( 20.0, 4.0);
glVertex2f( 20.0, 0.0);
glEnd();

glFlush();

}

if(key == GLUT_KEY_LEFT){
Ballx = Ballx - Speed;

}

if(key == GLUT_KEY_RIGHT){
Ballx = Ballx + Speed;

}

glutPostRedisplay();

}

glMatrixMode(GL_MODELVIEW);


glPushMatrix();
glScalef (4.0, 4.0, 4.0);
glTranslatef (Ballx, Bally-20.0, Ballz +50.0);
ball();

glPopMatrix();


glPushMatrix();
glScalef (4.0, 4.0, 4.0);
glTranslatef (0.0, 12.0, 25.0);
target1();

glPopMatrix();

//////////////////////////////

glPushMatrix();
glScalef (4.0, 3.0, 4.0);
glTranslatef (-30.0, -45.0, 25.0);
updownbar();

glPopMatrix();

////////////////////////

glPushMatrix();
glScalef (3.0, 4.0, 4.0);
glTranslatef (25.0, -30.0, 25.0);
leftrightbar();

glPopMatrix();

Tezelia
01-02-2013, 08:53 AM
Anybody?

Here I tried animating the ball but its not working

void TimerFunction(int value)
{
glClear (GL_COLOR_BUFFER_BIT);
//the angle by which the star is rotated

if (tt == '1')
ballz = ballz + speed;
glutPostRedisplay();
glutTimerFunc(5,TimerFunction, 0);//calls TimerFunction on tick - callback
}


void keyPressed (unsigned char key, int x, int y) {

if (key == 'z') {

tt == tt + 1;
ballz = ballz + speed;


}

}


void TimerFunc(int value)
{
glutSwapBuffers();
glutPostRedisplay();
glutTimerFunc(25, TimerFunc, 1);
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);// a 4:3 ratio
glutCreateWindow("FOOTBALL COMING HOME ");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutKeyboardFunc(keyPressed);
glutSpecialFunc(SpecialKeys); // calls speical key function


glutTimerFunc(25, TimerFunc, 1);
SetupRC();
glutMainLoop();
return 0;
}

Carmine
01-02-2013, 09:49 AM
Here I tried animating the ball but its not working ...

This is too vague. What happens when you run this? Do you get any graphics at all?

Do you know if your TimerFunc is even getting called? Use your debugger or print
statements to see if it's being called.

It's good you posted your code. It helps the forum to help you. But you should take
it a bit further by enclosing the code in [ code] and [ /code] tags. Use indenting to
make it more readable, and leave out stuff not pertinent to your question. In other
words, post a simplified, cleaned up, version of your code that we can compile and
run.

Tezelia
01-02-2013, 11:17 AM
void TimerFunction(int value)
{
glClear (GL_COLOR_BUFFER_BIT);
//the angle by which the star is rotated

if (tt == 1)
ballz = ballz ++;
glutPostRedisplay();
glutTimerFunc(5,TimerFunction, 0);//calls TimerFunction on tick - callback
}




void keyPressed (unsigned char key, int x, int y) {

if (key == 'z') {

tt == tt + 1;
ballz = ballz - speed;
bally = bally + 0.5;
if (ballz < -5){
reset();
}


}

In simple terms in my code if I press z, t = t + 1 which = 1

and if t==1 then it plays the animation

if (tt == 1)
ballz = ballz ++;
glutPostRedisplay();
glutTimerFunc(5,TimerFunction, 0);

How do I call it?

Hopefully this is an improvement Carmine ): Just struggling with this assignment

Kind Regards

Carmine
01-02-2013, 12:19 PM
Look for examples of glutTimerFunc with Google.

Tezelia
01-03-2013, 10:29 AM
Thank you I will try and look online for tutorials, etc

ZeZee90
01-07-2013, 03:38 AM
I'm doing that coursework too. Have u done any collision?