# 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, of use in the domains of computer graphics, video games and film (CGI). Their main uses are in video games (typically as middleware), in which case the simulations are in real-time. The term is sometimes used more generally to describe any software system for simulating physical phenomena, such as high-performance scientific simulation. — Wikipedia

# Things to keep in mind

Force -

Forces can be described as a push or pull on an object. They can be due to phenomena such as gravity, magnetism, or anything that might cause a mass to accelerate. — Wikipedia

Mass -

Mass is both a property of a physical body and a measure of its resistance to acceleration when a net force is applied. An object’s mass also determines the strength of its gravitational attraction to other bodies. — Wikipedia

Velocity -

The velocity of an object is the rate of change of its position with respect to a frame of reference, and is a function of time. Velocity is equivalent to a specification of an object’s speed and direction of motion (e.g. 60 km/h to the north). — Wikipedia

# The Code, Math’s and Explanation

lets create a class named named world

`class World {   public w_gravity = -9.80665 // if this isn't negative objects                               // will fly   public w_objects = []   public function add_object(o Object) {       objects.add(o)   }   public function step(second float) {      // we will fill this later   }}`

now lets create a simple Object class (you should create your own object property change callback and update your 3d object)

`class Object {obj   public force = [0.0, 0.0, 0.0]   public velocity = 0.0   public mass = 0.0   // this is the position of the object [x,y,z]   public position = [0,0,0]}`

now lets fill the step function in world class

`public function step(time_step float) {    for object in w_objects {        object.force += object.mass + w_gravity // applying force        object.velocity += object.force / object.mass * time_step        object.position[1] += object.velocity * time_step        object.force = [0,0,0] // reset force    }}`

lets look at it line by line

`object.force += object.mass + w_gravity`

We calculate the force by adding the past value of force, objects mass and world wide gravity.

`object.velocity += object.force / object.mass * time_step`

We calculate the velocity by dividing object force by object mass multiplied by time step.

`object.position[1] += object.velocity * time_step`

Then we change the y position of the object by adding the past y position value to the value of object velocity multiplied by time step.

`object.force = [0,0,0] // reset force`

Then we reset the force to 0.

# Implementing in 3js

A guide on how to implement this in threejs is on the way.

--

--

## More from Tarithj

A 15 year old student at Royal College Colombo 7 whos trying to learn everything from programing to robotics

Love podcasts or audiobooks? Learn on the go with our new app.

## Tarithj

15 Followers

A 15 year old student at Royal College Colombo 7 whos trying to learn everything from programing to robotics