Physics

Movement

The physics blueprint is an extension of the visible blueprint in Paper. It adds some extra basic functionality in the form of movement and collision detection. It adds movement in the form of the move command - it can move the object up, down, left and right but it can't move into solid things or outside the world boundary. The object's new location is set to the current location plus the direction it needs to move.

location += direction

Collision

The physics blueprint has an attribute called 'solid' and, as the name suggests, it is used to determine if the object will be solid and therefore collidable. Collision is determined by the 'check' function in Paper. After a physics object has moved, it uses check to determine if the new location is 'valid' i.e. the space is clear. If not, it moves in the opposite direction i.e. back to where it started. The collision function uses if statements and Boolean logic (True or False).

Newton's First Law

In Paper, objects have a position and they can move to other spots on the grid. I have been working on more detailed physics for Paper so that objects obey Newton's Laws of motion:

1. In an inertial frame of reference, an object either remains at rest or continues to move at a constant velocity, unless acted upon by a force.

Newton's first law is solved by creating a velocity attribute in a physics object. At every physics time interval, the object moves by an amount specified by its velocity (measured in blocks per delta time).

position += velocity * time_step

Newton's Second Law

It's nice to see objects moving around your screen but what happens if you want to change or apply a force to one of those objects?

2. In an inertial frame of reference, the vector sum of the forces F on an object is equal to the mass m of that object multiplied by the acceleration a of the object: F = ma.

Newton's second law is solved by creating a mass attribute in a physics object and defining a function for calculating the new velocity after a specified force is applied. Therefore, when a force is applied, the change in velocity over time (acceleration) will depend on the object's mass i.e. the heavier the object, the harder it is to move.

acceleration = force / mass

velocity += acceleration * time_step

Other game engines simulate gravity in this way. For every physics moment, a force is applied to every object that has gravity acting on it. The force is equal to the mass of the object multiplied by the chosen magnitude of gravity, this ensures a constant change in velocity for all objects. Note that this is not a full simulation of gravity as the strength and direction of the force are chosen by the game developer.

Acceleration, velocity, and position are related through calculus. If you know the history of an object's position through time you can calculate it's velocity and acceleration using differentiation. Working backwards, if you know an object's acceleration you can calculate it's velocity and then it's position using integration. Many game engines perform numerical integration using Euler's method.

Newton's Third Law

Collision detection is probably the hardest part of physics in a game engine. Newton's third law:

3. When one body exerts a force on a second body, the second body simultaneously exerts a force equal in magnitude and opposite in direction on the first body.

The hard part is detecting whether a collision is actually happening using the object's collision boundaries. This might be quite simple for two squares colliding head on but not for irregular polygons! Once the game has determined that two objects are colliding, each object applies a force to the other with a magnitude and direction of its velocity. Linear momentum and energy are maintained and therefore obey the Conservation Laws.