To make the game more realistic use real gravity equations. A constant rise and fall seems dull.
Gravity class:
class Gravity // G = 9.8 m/s
{
public:
Gravity(const int NumCreatures)
{
V = new float[NumCreatures];
T = new float[NumCreatures];
}
~Gravity()
{
delete[] V;
delete[] T;
}
// Set
inline void SetVelocity(const float Velocity) { V[m_Creature] = Velocity; }
inline void SetTime(const float Time) { T[m_Creature] = Time; }
inline void SetCurrentCreature(const int Creature) { m_Creature = (Creature-1); }
// Gravity Equations for Upward Motion
inline float UpVelocityAtAnyTime(void) const { return (V[m_Creature] - (G * (T[m_Creature]))); }
inline float UpTimeToReachPeak(void) const { return (V[m_Creature] / G); }
inline float UpMaximumHeight(void) const { return ( pow(V[m_Creature],2) / (G * 2) ); }
// Gravity Equations for Falling Objects
inline float DownVelocity(void) const { return (G * (T[m_Creature])); }
inline float DownDistanceInFall(void) const { return ( (G * pow(T[m_Creature],2) ) / 2 ); }
inline float DownReachingGivenVelocity(void) const { return ( (pow(V[m_Creature],2) ) / (2 * G) ); }
inline float DownTimeToFallDistance(const float Height) const { return ( sqrt( (2 * Height) / G ) ); }
private:
float* V, *T;
int m_Creature;
};
Jumping:
const float foot = 0.30480f;
void Rise()
{
float I = 0;
for(; I <= m_pGravity->UpTimeToReachPeak(); I += .1)
{
// Set Time
m_pGravity->SetTime(I);
//Movement
if(LEFT) MoveLeft();
else if(RIGHT) MoveRight();
//May need to put a negative sign before m_pGravity depending what function you use.
//Move up (m_pGravity->UpVelocityAtAnyTime() * foot )
//Update Screen
dbSync();
}
}
Falling:
const float foot = 0.30480f;
float I = 0;
while(We don't hit the ground)
{
I += 0.1;
//Set Time
m_pGravity->SetTime(I);
//Movement
if(LEFT) MoveLeft();
else if(RIGHT) MoveRight();
//May need to put a negative sign before m_pGravity depending what function you use.
//Move down (m_pGravity->DownVelocity() * foot)
dbSync();
}