top of page

Week 1: Rigidbody & Physics World

  • Writer: Abhijit Baruah
    Abhijit Baruah
  • Sep 16, 2022
  • 2 min read

Updated: Sep 25, 2022

A rigidbody in modern game engines is a component that can be added to any gameobject, the gameobject's motion will now be handled and controlled by the physics world.


To implement this I first decided to add additional functionality to the GameObject class. In its private members I added a Rigidbody* and added the following public functions :-

1) Add Rigidbody

2) Get Rigidbody

3) Remove Rigidbody


For the Rigidbody class I added all the helper functions available to a rigidbody in a modern game engine like Adding Velocity , Adding Force , Adding Impulse and so on.

Every rigidbody has a GameObject* to its parent Gameobject that gets set in the Add Rigidbody function inside gameObject.


This design allows the user to add and remove Rigidbodies to game objects during runtime.


For the actual implementation of Physics equations every frame I decided to create a separate class called the physics world. This class will be responsible for calculating the velocity and resolving force equations of every rigidbody in the scene.


This class has an update method that will be called by the game engine (similar to Tick in unreal and Physics Update in Unity). It also maintains a vector of all Rigidbodies in the scene. A small code snippet showing how gravity is calculated per frame :-


At the beginning of every frame the forces acting on that object is calculated and then applied to the object using basic physics equations like Force = Mass * Acceleration , velocity per frame is the change in acceleration for that frame.

The resulting velocity for that frame is applied to the transform of the object and finally the force is reset to 0.


The 0.016f is just a placeholder and will be replaced by the concept of delta time in the future.


Finally , some videos that show the system working as expected!



Two game Objects, one with a rigidbody which falls due to grvity and the other without a Rigidbody.


The next thing on the list is to implement Collider's and build a collision system.


Thank you for reading and stay tuned for more!

Comments


bottom of page