Physics

Physics for Game Developing :




The use of physics in games has become quite popular in recent years,
physics always comes along with some heavy mathematics,
it is certainly possible to use a physics framework or apply a simple formula such as the famous

F= m*a
with out knowing much about physics and math.

if you need to create your own physics system for your game , you will need atleast a basic understanding of Newton's laws of motion.

Newtons,Laws of Motion

First law :

Objects stay in motion unless some external forces interacts with them,
This is really easy because that can move usually have movement vector or some velocity value.
just dont change that value unless you apply some force to slow it down, accelerate it , or change the direction, Thats it.


Objects just stay in motion until some force changes that (gravity, collisions, explosion)


Second Law :

When you do apply an external force, it means you can accelerate or decelerate an object. if you have more than one dimension (well, you have three for 3D games), you can also change the direction the object is moving. in that case, you work with vectors instead of scalar values.
As a result, all vector components (x, y and z) will be changed of one simple value. because masses for your objects will always be constant.

you can use the simplified F= m* a formula, where "a" is the acceleration defined by change of velocity divided by the change of time ;

a = dv/dt.
take an example of a rocket
m= mass of Rocket
a= Resulting acceleration
F=Acceleration Force through the rocket booster

F =m * a
a= F/m

Each applied force on an object changes its momentum, which means the Object either accelerates, decelerates, or changes direction.



In case of collision of two objects , F= Collision force , and two masses M1 and M2
each body have its own Mass,
all the momentum of both objects builds the collision force, which is then used to either break the objects or give the force back to them and let them bounce off in opposite directions

Third Law :

Each action has an equal and opposite reaction . Thus, if you have a force such as a gravity of the earth acting on an object, there is a force that reacts, applying pressure in the opposite direction . Because the earth has a much greater mass than a small object, such as a stone or apple, it may not matter if the earth is acting on it. But if you take a much bigger object, such as sun , the gravity of the earth is comparatively smaller to that of the sun.
Good thing our earth always in motion rotating aroung the sun , keeping it in orbit, the same way the moon is orbiting around the earth.

-----------------------------------------------------------------------------------------------


Physics is a big topic and implementing physics into your games requires full attention.

you should try to keep things simple. Often you dont even have to know which formula is correct one. just find something that works, for ex: you dont have to code anything to follow the first law of motion. the third law can be ignored, as well , because you are only interested in forces that change motion for your objects.

For a racing game , the basic physics requirement ,

1. our car engine pushes the car as we step on the gas
2. Wind road friction, etc .. pull the car back and slow it down , we can also brake the car ourselves,
3. Gravity pulls the car down and keeps the car on the road, if we drive fast enough, we might stay in the air for a few seconds through the momentum of the car until gravity pushes us down again.


================================================================
the car movement in Action script

the car moves forward ven the value of speed is increment
speed += 1.5;
decreasing the speed by multiplying it with 0.98, that means the speed will get to 98% of the previous speed.

speed -= 0.8;
Now, to get the car move. Because the car can rotate, we need to know at what direction is turned. The easiest way to do that is by using the math sine and cosine;

in action script, Math.sin & Math.cos. With those we can calculate the angle of the car position.

_x += Math.sin (_rotation * Math.PI / 180) * speed;
_y += Math.cos (_rotation * Math.PI / 180) * -speed;

wheel trail:

xTrail = Math.sin (_rotation * Math.PI / 180) * 10;
yTrail = Math.cos (_rotation * Math.PI / 180) * 10;
xTrailCalculated = _x - yTrail;
yTrailCalculated = _y - xTrail;

by using  the values xTrailCalculated, yTrailCalculated
u show a trail effect using Drawing API.

I Hope u have get some basic knowledge of gaming..
sreejith.k.s

Comments

Popular Posts