Beginner: Simple 2D flyer physics with Chipmunk?

Official forum for the Chipmunk2D Physics Library.
Post Reply
aidynskas
Posts: 1
Joined: Fri Aug 20, 2010 9:09 am
Contact:

Beginner: Simple 2D flyer physics with Chipmunk?

Post by aidynskas »

Hi,

I'm working on a very small 2d sidescroller-type flying game. Last few days I've been trying to simulate lift and drag with Chipmunk with no luck. I've looked through various resources on the internet dealing with drag and lift but still couldn't implement it correctly.

I'm having a simple cpBody with some initial impulse force applied for speed. This is how I'm trying to calculate lift and drag (every frame):

Code: Select all

float lift = -airDensity * speed*speed * attackAngle * liftCoefficient;
float drag = -airDensity * speed*speed * attackAngle*attackAngle * dragCoefficient;
And full code below:

Code: Select all

float airDensity=1.29;
float speed=cpvlength(airplaneBodyVelocity);
float velocityAngle=-atan2(airplaneBodyVelocity.y,airplaneBodyVelocityy.x);
float attackAngle=bodyAngle-velocityAngle; //attack angle is difference between velocity and current angle set by player

float liftCoefficient=1.0;
float dragCoefficient=1.0;

float lift = -airDensity * speed*speed * attackAngle * liftCoefficient;
float drag = -airDensity * speed*speed * attackAngle*attackAngle * dragCoefficient;

cpVect;
forceVector.x=drag;
forceVector.y=lift;

cpBodyResetForces(airplaneBody);
cpBodyApplyForce(airplaneBody, forceVector, cpvzero);
Any help is welcome!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Beginner: Simple 2D flyer physics with Chipmunk?

Post by slembcke »

Assuming that the formulas are right, that looks like it should be pretty close. There are a couple potential issues though:

First of all, the following snippets are equivalent:

Code: Select all

cpVect;
forceVector.x=drag;
forceVector.y=lift;

cpBodyResetForces(airplaneBody);
cpBodyApplyForce(airplaneBody, forceVector, cpvzero);

Code: Select all

airplaneBody->f = cpv(drag, lift); // or you can use cpBodySetForce() if you find it more clear
Now, this means that lift always goes straight up and drag is always horizontal. The forces should rotate with the wing (assuming that the wing rotates). So something like this:

Code: Select all

airplaneBody->f = cpvrotate(cpv(drag, lift), airplaneBody->rot);
The next potential problem is this:

Code: Select all

float attackAngle=bodyAngle-velocityAngle;
atan() always returns results between Pi and -Pi. Using an example in degrees 170 degrees - 190 degrees should be -20. When wrapped to the range (180, -180) you will instead be doing 170 degrees - -170 degrees which is 340 degrees. While 340 degrees and -20 degrees are the same thing, 340 degrees will break your calculation.

Code: Select all

float attackAngle=bodyAngle-velocityAngle;
if(attackAngle > M_PI){ attackAngle -= 2.0f*M_PI }
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests