Getting Started#

Nova Physics is a lightweight 2D rigid body physics engine. It is designed with game development in mind, however you can utilize it anywhere you need to simulate rigid body dynamics. It is written in portable C with no dependencies other than the standard library, meaning anyone can easily write a binding for their favored programming language.

Nova Physics is, and always will be, free and open-source. It is licensed under MIT, meaning you don’t need to pay to use Nova Physics in your projects. Altough we would greatly appreciate donations!

Hello World#

After installing (you can follow installing), you are ready for your first Nova Physics program, where a ball free falls onto a ground and we print its coordinates every step.

 1#include <stdio.h>
 2#include "novaphysics/novaphysics.h"
 3
 4
 5int main() {
 6    // Create an empty space
 7    nvSpace *space = nvSpace_new();
 8
 9    // Create a ground body with a rectangle shape
10    // Making it static means the body will never move no matter what.
11    nvBody *ground = nvBody_new(
12        nvBodyType_STATIC,
13        nvRectShape_new(10.0, 1.0), // A rectangle shape.
14        NV_VEC2(0.0, 30.0), // NV_VEC2 is a utility macro to quickly creating vectors.
15        0.0,
16        nvMaterial_CONCRETE // You can specify a custom material as well.
17    );
18
19    // Add the body to the space.
20    nvSpace_add(space, ground);
21
22    // Now create a ball that is going to fall to the ground.
23    nvBody *ball = nvBody_new(
24        nvBodyType_DYNAMIC, // Notice the dynamic type. The ball will move in space.
25        nvCircleShape_new(1.5), // Circle shape with radius of 1.5
26        NV_VEC2(0.0, 0.0),
27        0.0,
28        nvMaterial_RUBBER // Giving the ball a rubber material, so it bounces
29    );
30
31    nvSpace_add(space, ball);
32
33    // The scene is set up. Now we only have to simulate it!
34
35    // This is the time step length the engine going to simulate the space in.
36    nv_float dt = 1.0 / 60.0;
37
38    // Let's simulate for 5 seconds.
39    nv_float duration = 5.0;
40
41    for (nv_float t = 0.0; t < duration; t += dt) {
42        printf(
43            "Ball is at (%.2f, %.2f) with velocity (%.2f, %.2f) at time %.2f.\n",
44            ball->position.x, ball->position.y,
45            ball->linear_velocity.x, ball->linear_velocity.y,
46            t
47        );
48
49        // Simulate the space
50        nvSpace_step(space, dt, 10, 10, 5, 1);
51    }
52
53    // Free the space and all resources it used.
54    // Space also manages the bodies and constraints we add to it.
55    // Unless you removed them manually, in that case you have to free your bodies.
56    nvSpace_free(space);
57}

When the code is run, we can see that the ball bounces on the ground few times and then rests.