PDA

View Full Version : Bouncing a 3d Sphere



valonj
05-27-2012, 11:46 AM
Ok so i have this sphere with shinnies and specular and some other effects, i am a starter in opengl so what i need is the sphere to go up and down again, it should be like a bouncing ball but like in real life the ball has to stop at some point, i have been searching the entire google and there is not a single project like this, there are some functions that i didn't understand, again i am a starter so sorry from me if this is lame, or an amateur question.


glPushMatrix ();
glTranslatef (0.0, 0.0, -1.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse4);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular4);
glMaterialfv(GL_FRONT, GL_SHININESS,mat_shininess4);
glutSolidSphere (1.0, 35, 35);
glPopMatrix ();

BionicBytes
05-28-2012, 01:31 AM
You are on the right track by using glTranslatef (0.0, 0.0, -1.0); However, for up/down motion you want to translate on the Y axis glTranslatef (0.0, Y, 0.0);
Use Sin or Cos to simulate the wave like properties of the bouncing ball and each time the ball is at the lowest point ensure you decrease the strength of the 'spring' force allpied to the sin/cos function. Eventually, the ball will stop bouncing when the spring force is zero.
So perhaps something like:
Y = SIN (ballangle) * ballspring; //ballangle between 0-180 degrees - converted to radians; Ball spring=some linear multiplier
if ballangle =180 then { exponentiallyreduce (ballspring); ballangle=0}
else ballangle += 0.02; //should use some time dependant rate to increase ball angle instead of fixed amount