Catapult Physics for Game Programming

In summary, you need to use the equations of motion for a projectile and take into account the air resistance force and torque to achieve the rotation and gliding of the player in your game.
  • #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());
 
Physics news on Phys.org
  • #2
You need to use the equation of motion of a projectile, which is a special case of Newton's second law of motion. As the player is in the air, they experience a force due to air resistance. The equations of motion for this situation are as follows: F = ma Where F is the force on the player due to air resistance, m is the mass of the player, and a is the acceleration of the player in the x and y directions. To calculate the air resistance force, you can use the equation: F_air = -1/2 * C_d * A * p * v^2Where C_d is the drag coefficient, A is the cross-sectional area of the player, p is the density of the air, and v is the velocity of the player. You can also calculate the torque on the player due to the air resistance by using the equation: T = 1/2 * C_d * A * p * v^2 * RWhere R is the radius of the player's body. Finally, you can calculate the gliding position of the player using the equation for the trajectory of a projectile: x = x_0 + v_0 * t * cos(theta) y = y_0 + v_0 * t * sin(theta) - 1/2 * g * t^2Where x_0 and y_0 are the initial position of the player, v_0 is the initial velocity of the player, t is the time since the launch, theta is the angle of launch, and g is the acceleration due to gravity. I hope this helps!
 

Related to Catapult Physics for Game Programming

1. What is a catapult?

A catapult is a mechanical device used for launching objects at a high speed and distance. It uses the principles of potential and kinetic energy to propel objects forward.

2. How does a catapult work?

A catapult works by storing potential energy in its tensioned parts, such as the ropes or springs, and releasing it rapidly to convert it into kinetic energy. This energy transfer propels the object, such as a projectile, forward.

3. What are the different types of catapults?

There are various types of catapults, including trebuchets, mangonels, and ballistas. These differ in their designs and mechanisms, but all follow the same principle of converting potential energy into kinetic energy to launch an object.

4. How does physics play a role in catapults?

Physics is essential in understanding the mechanics and functionality of catapults. The principles of force, energy, and motion are all involved in the design and operation of catapults. Without a thorough understanding of physics, it would be challenging to create an effective and accurate catapult.

5. How can the physics of catapults be applied to game programming?

The physics of catapults can be applied to game programming by using mathematical equations to simulate the movement and trajectory of the projectile. This adds a level of realism and accuracy to the game, making it more engaging for players. Additionally, understanding the physics of catapults can inspire creative and unique game mechanics and challenges.

Similar threads

  • Classical Physics
Replies
18
Views
769
  • Programming and Computer Science
Replies
29
Views
6K
  • Introductory Physics Homework Help
Replies
2
Views
1K
Replies
1
Views
603
Replies
1
Views
7K
  • Programming and Computer Science
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
2
Views
16K
  • Introductory Physics Homework Help
Replies
2
Views
8K
Back
Top