vec3 t_Position = vec3(0, 0, 150);vec3 t_Velocity = vec3(0, 0, 0);vec3 t_Gravity = vec3(0, 0, (-9.8f));float t_Elasticity = 1.0f;float t_CurTime = 0.0f;//#define EULERfloat GetTimeOfCollision(vec3& a_Acceleration, vec3& a_Velocity, vec3& a_Position ){ // Use the ABC formula as explained here http://nl.wikipedia.org/wiki/Wortelformule or http://en.wikipedia.org/wiki/Quadratic_equation#Quadratic_formula // Thanks go out to Joey float a = a_Acceleration.z; float b = a_Velocity.z; float c = a_Position.z; float t1 = 0; // There should be two positions the parabola collides with, otherwise it's already on the ground and the code would just make it lie still anyway. float t2 = 0; float d = ((b*b) - 4.0f * a * c); // The upper part.. Should be above 0 for an answer if(d > 0) { float sqrtf = sqrt(d); // Optimisation. t1 = sqrtf + b / (2 * a); // And the rest of the formula t2 = -(sqrtf - b / (2 * a)); if(t1 > 0 && t2 > 0) { if(t1 < t2) return t1; // Get the lowest one if they're both above 0 else return t2; // Because we need one after time T = 0 } else { if( t1 > 0 ) return t1; // Otherwise just get the one above 0. else return t2; } } return 0.0f; // No answer..}vec3 GetPositionAtTimeT( float a_Time, vec3& a_Acceleration, vec3& a_Velocity, vec3& a_Position ){ // Just the regular ABC return (a_Acceleration * a_Time * a_Time) * 0.5f + a_Velocity * a_Time + a_Position;}vec3 GetVelocityAtTimeT( float a_Time, vec3& a_Acceleration, vec3& a_Velocity ){ // The integrated one to get the velocity. return a_Velocity + a_Acceleration * a_Time;}void PerfectIntegrator(float a_DT){ float CollisionTime = GetTimeOfCollision(t_Gravity, t_Velocity, t_Position); vec3 PosAtCollision = GetPositionAtTimeT(CollisionTime, t_Gravity, t_Velocity, t_Position); // Calculate everything to know when the collision will occur float timeleft = a_DT - (CollisionTime); t_Position = GetPositionAtTimeT(timeleft, t_Gravity, t_Velocity, PosAtCollision); t_Velocity = GetVelocityAtTimeT(a_DT, t_Gravity, t_Velocity); // Change the position and the velocity to what it should be right now if(t_Position.z<=0) // The check for the bounce { t_Position.z = t_Position.z += t_Velocity = GetVelocityAtTimeT(CollisionTime, t_Gravity, t_Velocity)*-(t_Elasticity); }}int main(int argc, char* argv[]){ // Removed some code for setting up the framework float t_DT = 0; LARGE_INTEGER ticksPS, start, end; QueryPerformanceFrequency( &ticksPS); env.set_scale(100.0f); // env is my model.. My ball while(g_Framework->do_frame(curThread)) { QueryPerformanceCounter(&start);#ifdef EULER // If I wanted to use the Euler integrator t_Velocity += t_Gravity; // Simply add the gravity onto the velocity t_Position += t_Velocity; // And then add the velocity onto the position if(t_Position.z<=0) // The bounce code. { t_Position = PosAtCollision; t_Velocity = GetVelocityAtTimeT(CollisionTime, t_Gravity, t_Velocity)*-(t_Elasticity); }#else PerfectIntegrator(t_DT); // Otherwise, if we're not using Euler, use the perfect one.#endif env.set_pos(t_Position.x, t_Position.y, t_Position.z); // Set the position correctly QueryPerformanceCounter( &end ); t_DT = float ( end.QuadPart - start.QuadPart ) / float ( ticksPS.QuadPart / 1000 ); } // Removed some framework code return 1;}