2D Physics Engine From Scratch (Jack in Box)


Overview

This project simulates planer multi-body impact dynamics of a jack bouncing around inside a box. In this simulation, external forces are applied to the box as input to the system. The primary forces applied are a sinusoidal rotational force that rotates the box back and forth and a vertical support force that stops the box from falling. The jack is in free-fall, and bounces off the walls of the box.

Video demo


System Diagram

Below are diagrams showing the system used in this assignment. First, the frames and transformations used to simulate the box in this project are as follows:

For simulating the jack, the following are used:


Parameters

Algorithm Description

State Variable
The current state of the system is stored as a vector, q, which contains the following variables describing the position and orientation of the box and the jack relative to the world frame:

Velocities and accelerations for the two objects are also found by taking the first and second time derivatives of q. Velocities are stored in the vector qdot and accelerations in the vector qddot.

Euler Lagrange
To solve for the dynamics of the objects in the system, first the Euler Lagrange equations and the external force were found. The following steps are used to find Euler Lagrange equations:

Constraint Equations
The constraints are defined between each corner of the jack and each wall of the box; the corners of the jack cannot pass through the walls of the box. As a result, there are a total of 16 constraint equations defined. The constraint variable phi is defined as the expression of the distance between the two impacting objects. As a specified jack corner and wall of the box move closer together in the simulation, the phi value associated with that specific constraint equation approaches zero. When phi gets close enough to 0 (for this simulation, a threshold of 0.1 was used), the two objects are considered to have collided.

Impact Updates
After two objects collide, their states are updated to reflect the results of the impact. The following steps are used to solve for impact updates:

GitHub