Hello My dear friends , this is me The Aikido Engineer AKA CMPSamurai ( Moustafa Mahmoud )
Our Graphics project this year in college this year is to design A 3d game , so i am going to write you a tutorial on how to use A physics engine in your game to make more realistic and save you tons of effort :)
First What is a physics engine ??
A physics engine is computer software that provides an approximate simulation of certain physical systems, such as rigid body dynamics (including collision detection), soft body dynamics, and fluid dynamics,
( physics Engine )
I have found a very easy one and I am going to explain it to you now
it is called Jitter
Adding bodies to the world
So far we just have created our World class which is connected to the CollisionSystem - our world is
pretty empty and we want to add a single box to it:
1 CollisionSystem collisionSystem = new CollisionSystemSAP();
2 World world = new World(collisionSystem);
4 Shape shape = new BoxShape(JVector.One);
5 RigidBody body = new RigidBody(shape);
First a Shape is created. The shape represents the collidable part of the RigidBody. We wanted to
have a box, so we used the BoxShape. There are much more default shapes in Jitter: Box, Cone, Sphere,
Cylinder, Capsule, Compound, TriangleMesh. The shape is passed to the constructor of the body which
gets added to the world. Done. Calculating mass, inertia.. is done inside the engine - but you also can set
it manually. The body with the side length (1,1,1) is created at the origin (with RigidBody.Position you set
the position of the center of mass of the simple shapes).
Units in Jitter: The default gravity which adds force to a body is set to 9:81
one length unit in Jitter is one meter. Because of numerical instabilities when using
smaller objects it's recommended not to use objects which are much smaller than one
The last thing to do, is to update the engine and to integrate the current physics state a timestep further:
1 world.Step(1.0f / 100.0f, true);
The rst parameter is the timestep, the second one tells the engine to use internal multithreading or not.
In our simple one-body world the box gets a ected by gravity and the position changes. In your 'Draw'
method you are now able to draw a box with the position and orientation of the body.
1.4 Complete sample
In Tutorial1 a complete physics scene is build and simulated using just a few lines of code.
Figure 1: Tutorial1 - Two boxed falling to the ground. Clicking on them moves them away from the camera.
1 private void BuildScene()
3 // creating two boxShapes with different sizes
4 Shape boxShape = new BoxShape(JVector.One);
5 Shape groundShape = new BoxShape(new JVector(10, 1, 10));
7 // create new instances of the rigid body class and pass
8 // the boxShapes to them
9 RigidBody boxBody1 = new RigidBody(boxShape);
10 RigidBody boxBody2 = new RigidBody(boxShape);
12 RigidBody groundBody = new RigidBody(groundShape);
14 // set the position of the box size=(1,1,1)
15 // 2 and 5 units above the ground box size=(10,1,10)
16 boxBody1.Position = new JVector(0, 2, 0.0f);
17 boxBody2.Position = new JVector(0, 5, 0.3f);
19 // make the body static , so it can 't be moved
20 groundBody.IsStatic = true;
22 // add the bodies to the world.
The scene is drawn with the XNA framework, but it would be similar for every other graphics frame-work/engine. Jitter uses it's own math classes (JVector,JMatrix,JBox,JMath,JOctree), so you have to
convert orientations (represented by 3x3Matrices) and positions to the type of your graphics framework -this isn't that complicated.
You can convert from jitter Matrices / vectors to XNA matrices and vectors using Conversion class
just type Conversion. ( and select what you want )
You can find all the tutorials Source files and the Library here :)
And the PDF explaining the Physics engine
if you need anything please contact me , I am ready anytime / leave a comment here