Original Post
Hello, I'm trying to simulate an object vertically falling with gravity and bouncing against another object, with no horizontal forces involved. Currently I have the following being executed every cycle (60 Hz):- The code basically checks to see if double dVelocity is nonzero or there is nothing immediately beneath the object. Then it adds the acceleration due to gravity to the velocity (multiplied by 1/60th of a second) and rounds the result in either sign to increment the y position. Thn we have the code that is executed when the object hits something:- The velocity is reduced to 2/5 of its original value and then inverted to signify velocity in the opposite direction (up). Unfortunately it doesn't seem to work as planned. The object continues to bounce to a particular height. The results from the OutputDebugString calls are:- 7.840000 -> -3.136000 3.332000 -> -1.332800 2.979200 -> -1.191680 2.924320 -> -1.169728 2.750272 -> -1.100109 3.015891 -> -1.206356 2.713644 -> -1.085457 3.030543 -> -1.212217 2.707783 -> -1.083113 2.640887 -> -1.056355 2.863645 -> -1.145458 2.970542 -> -1.188217 2.731783 -> -1.092713 The velocity seems to oscillate over time and I don't know why. :(
if (tt->dVelocity || !DetectRect(me->parent, me, me->x, me->y + 1, me->w, me->h))
{
tt->dVelocity += GRAVITY * 0.02;
if (tt->dVelocity > 0)
me->y += (tt->dVelocity - (int)tt->dVelocity >= 0.5) ? (int)tt->dVelocity + 1 : (int)tt->dVelocity;
else
me->y += ((int)tt->dVelocity - tt->dVelocity >= 0.5) ? (int)tt->dVelocity - 1 : (int)tt->dVelocity;
}
char szTemp[64];
sprintf(szTemp, "%lf -> ", tt->dVelocity);
OutputDebugString(szTemp);
tt->dVelocity *= 0.4;
tt->dVelocity = -tt->dVelocity;
sprintf(szTemp, "%lf\n", tt->dVelocity);
OutputDebugString(szTemp);