- #1
anupgupta
- 1
- 0
hello
I am a game programmer..and working on a game which has a player attached to the catapult arm. now when the arm is released the player gets thrown in the air.. while in air..the air resistance could make the player rotate left or right... and then glides and lands on the ground.
i have written the code for player getting thrown at random angle and then following a projectile and touching the ground..but want to know about the rotation in air and gliding part.
the game physics is similar to this link
http://www.miniclip.com/games/catapult/en/
can anyone please let me know what physics formulas be required to achieve this
Any help is highly appreciated
//currently the code is here what i have written
float _velocityAngle;
b2Vec2 _linearVelocity, _orientation;
float torque;
float torqueFactor = -100.0f; //set this to whatever works for your units -100
float torqueDamping = 2; //this is a good starting place, but experiment 2
float _airGripFactor = 1.0f; //1
_orientation.Set(cos(gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngle()), sin(gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngle()));
_linearVelocity = gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetLinearVelocity();
_velocityAngle = (float) (atan2f( _linearVelocity.y, _linearVelocity.x ));// + (b2_pi / 2));
float angVelDelta = gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngle() - _velocityAngle;
//Constrain to -PI -> PI
while (angVelDelta > b2_pi)
{
angVelDelta -= 2*b2_pi;
}
while (angVelDelta < -b2_pi)
{
angVelDelta += 2*b2_pi;
}
float absVel = _linearVelocity.Length();
torque = absVel * torqueFactor * (angVelDelta) - absVel * gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngularVelocity() * torqueDamping;
gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->ApplyTorque(torque);
float32 proj = b2Dot(_linearVelocity, _orientation);
b2Vec2 targetVelocity = b2Vec2(_orientation.x * proj, _orientation.y * proj);
b2Vec2 force = (targetVelocity - _linearVelocity);
force = b2Vec2(force.x * _airGripFactor, force.y * _airGripFactor);
gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->ApplyForce(force, gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetWorldCenter());
I am a game programmer..and working on a game which has a player attached to the catapult arm. now when the arm is released the player gets thrown in the air.. while in air..the air resistance could make the player rotate left or right... and then glides and lands on the ground.
i have written the code for player getting thrown at random angle and then following a projectile and touching the ground..but want to know about the rotation in air and gliding part.
the game physics is similar to this link
http://www.miniclip.com/games/catapult/en/
can anyone please let me know what physics formulas be required to achieve this
Any help is highly appreciated
//currently the code is here what i have written
float _velocityAngle;
b2Vec2 _linearVelocity, _orientation;
float torque;
float torqueFactor = -100.0f; //set this to whatever works for your units -100
float torqueDamping = 2; //this is a good starting place, but experiment 2
float _airGripFactor = 1.0f; //1
_orientation.Set(cos(gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngle()), sin(gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngle()));
_linearVelocity = gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetLinearVelocity();
_velocityAngle = (float) (atan2f( _linearVelocity.y, _linearVelocity.x ));// + (b2_pi / 2));
float angVelDelta = gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngle() - _velocityAngle;
//Constrain to -PI -> PI
while (angVelDelta > b2_pi)
{
angVelDelta -= 2*b2_pi;
}
while (angVelDelta < -b2_pi)
{
angVelDelta += 2*b2_pi;
}
float absVel = _linearVelocity.Length();
torque = absVel * torqueFactor * (angVelDelta) - absVel * gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetAngularVelocity() * torqueDamping;
gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->ApplyTorque(torque);
float32 proj = b2Dot(_linearVelocity, _orientation);
b2Vec2 targetVelocity = b2Vec2(_orientation.x * proj, _orientation.y * proj);
b2Vec2 force = (targetVelocity - _linearVelocity);
force = b2Vec2(force.x * _airGripFactor, force.y * _airGripFactor);
gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->ApplyForce(force, gameSceneRef->gameBodies[BOD_ANIM_PLAYER]->GetWorldCenter());