2D Physics Engine

Started by
3 comments, last by haphazardlynamed 18 years, 1 month ago
Hello, I was just wondering if someone could point me in the right direction for writing my own 2D physics engine. I am not planning on making it very complicated. I just want to be able to apply forces to objects and possibly have support for joints.
Advertisement
A good starting point, which will make your life Much easier
is to write a good vector math library first
it will greatly similify code for the actual physics if the math all wrapped up nicely...

This is an overview what should be done. Below is used the Euler-method to calculate the change in position.

1.First you define your class for the objects.
2.Second you write the calculation to make the next position.
3.Third you implement the update function.
You can make the below implementation more compact. The code below is here more descriptive.

Also check out this thread
http://www.gamedev.net/community/forums/topic.asp?topic_id=380587


1.

class object {

public:
// standard properties
float mass;
float xpos;
float ypos;
float xspeed;
float yspeed;
float xaccel;
float yaccel;


//forces applied to the object
// The net force
float force_net_x;
float force_net_y;

// force added by keyboard steering
float force_move_x ;
float force_move_y ;

// other forces like gravity
float force_gravity_x;
float force_gravity_y;

// fiction force
float force_fiction_x;
float force_fiction_y;

// and other forces etc.
// ...


CalcForceNet () ;
CalcAccelFromForce ();
CalcVelocityFromAccel(float deltatime);
CalcPositionFromVelocity(float deltatime);
} ;

this is an example how the object could look like.


2.
CalcForceNet () ;
this function add all the forces together putting the result in force_net_x, and force_net_y.

CalcAccelFromForce ();
this function calculate the accelaration by dividing the force with mass.

CalcVelocityFromAccel(float deltatime);
this function calculate the velocity using accelaration.
like xspeed = xspeed + xaccel * deltatime;

CalcPositionFromVelocity(float deltatime);
this function calculate the position, which is the new position, using speed.
like xpos=xpos+xspeed * deltatime;



3.
Your update loop will probably look like this

class object object1

Update (float time_elapsed_between_frames) {
static unprocessed_time = 0;
unprocessed_time += time_elapsed_between_frames;

while( unprocessed_time > timestep )
{

//perform simulation;
//like
object1.force_move_x=10;
object1.force_move_y=10;
// etc..
object1.CalcForceNet();
object1.CalcAccelFromForce();
object1.CalcVelocityFromAccel( timestep); // note that timestep is used here
object1.CalcPositionFromVelosity( timestep);

unprocessed_time -= timestep;
}

Display(); // render
}

Thank you, this is exactly what I was looking for
Quote:Original post by Anonymous Poster

class object {

public:
// standard properties
float mass;
float xpos;
float ypos;
float xspeed;
float yspeed;
float xaccel;
float yaccel;


//forces applied to the object
// The net force
float force_net_x;
float force_net_y;

// force added by keyboard steering
float force_move_x ;
float force_move_y ;

// other forces like gravity
float force_gravity_x;
float force_gravity_y;

// fiction force
float force_fiction_x;
float force_fiction_y;

// and other forces etc.
// ...






I'm going to seriously recommend that instead of using separate X and Y variables for everything, you adopt a Vector based approach.
Rather than 'xspeed and yspeed' have 'netVelocity' for example
It will make code a lot simpler, and potentially you'll have to deal with trig functions a lot less (they are slow and annoying to code for)

this will also enable you to adopt are more 'geometric' based logic for lots of the physics (as opposed to trigonometric), which makes lots of problems a lot simpler

struct myvector{float x,y,z;}struct myvector vectorcreate(float x, float y, float z){myvector temp;temp.x=x;...etc...return temp;}struct myvector dotproduct(myvector a,myvector b){return vectorcreate(a.x*b.x,a.y*b.y,a.z*b.z);}and so on....vectoraddvectorsubtractvectorprojectionvectorlengthvectornormalizevectorcrossproduct

This topic is closed to new replies.

Advertisement