I'm not a physicist so I'm having great trouble getting my ball to bounce. Basically, I have a ball and a floor and I'm just trying to get the ball to bounce on the floor. I'm not trying to degrade the amount it bounces like in real life all I want it to do is slow down as it reaches the peak of it's bounce and accelerate as it falls. For this I'm using the following equation in C++...
ballY = (0.5 * g * (t*t));
g is set to -9.81 (I know normally minus means going upwards but if I set it to positive 9.81 it shoots upwards). A problem I spot immediately with this equation is that I have no control about the initial Y position of the ball. Because whatever I set it to it will get changed on the first render by this equation. t is incremented by 0.1 on each render of the frame. My Render() function looks thus...
void Render()
{
// Clear the backbuffer to a blue colour, also clear the Z buffer at the same time.
g_pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
D3DXMATRIX TranslateMat;
// Begin the scene
if (SUCCEEDED(g_pd3dDevice -> BeginScene()))
{
checkCollision();
if (collided == false)
t += 0.1f;
else
t -= 0.1f;
ballX += 0.4f;
ballY = (0.5 * g * (t*t));
SetupMaterial(0.0f, 1.0f, 0.0f);
D3DXMatrixTranslation(&Tra
nslateMat,
floorX, floorY, floorZ);
g_pd3dDevice -> SetTransform(D3DTS_WORLD, &TranslateMat);
FloorMesh -> DrawSubset(0);
SetupMaterial(col, 0.0f, 1.0f);
D3DXMatrixTranslation(&Tra
nslateMat,
ballX, ballY, ballZ);
g_pd3dDevice -> SetTransform(D3DTS_WORLD, &TranslateMat);
BallMesh -> DrawSubset(0);
// End the scene.
g_pd3dDevice -> EndScene();
}
// Present the backbuffer to the display.
g_pd3dDevice -> Present(NULL, NULL, NULL, NULL);
}
Now the only way I can get the ball to bounce is to reverse time once a collision has occured (hence the t -= 0.1f). I know this isn't the correct way to do this as time would be moving backwards.
Can someone please help me with this because it's driving me nuts (hence the 500 points I made it worth). My entire source code is located here if you want to compile and test it yourself...
http://www.dunmanifestin.co.uk/bouncy.zipredneon
Start Free Trial