Body#

struct nvBody#

Body struct.

A rigid body is a non deformable object with mass in space. It can be affected by various forces and constraints depending on its type.

Some things to keep in mind to keep the simulation accurate and stable:

  • If you want to move bodies in space, applying forces is the best solution. Changing velocities directly may result in poor accuracy. Changing positions directly means teleporting them around.

  • Avoid creating gigantic or really tiny dynamic bodies. This of course depends on the application but keeping the sizes between 0.1 and 10.0 is a good range.

  • Make sure polygon shape’s centroid is the same as the body’s center position. Or else the center of gravity will be off and the rotations will not be accurate.

Public Members

struct nvSpace *space#

Space instance the body is in.

nv_uint16 id#

Unique identity number of the body.

nvBodyType type#

Type of the body.

nvShape *shape#

Shape of the body.

nvVector2 position#

Position of the body.

nv_float angle#

Rotation of the body in radians.

nvVector2 linear_velocity#

Linear velocity of the body.

nv_float angular_velocity#

Angular velocity of the bodyin radians/s.

nv_float linear_damping#

Amount of damping applied to linear velocity of the body.

nv_float angular_damping#

Amount of damping applied to angular velocity of the body.

nvVector2 force#

Force applied on the body. This is reset every space step.

nv_float torque#

Torque applied on the body. This is reset every space step.

nv_float gravity_scale#

Scale multiplier to the gravity applied to this body. 1.0 by default.

nvMaterial material#

Material of the body.

nv_float mass#

Mass of the body.

nv_float invmass#

Inverse mass of the body (1/M). Used in internal calculations.

nv_float inertia#

Moment of ineartia of the body.

nv_float invinertia#

Inverse moment of inertia of the body (1/I). Used in internal calculations.

bool is_sleeping#

Flag reporting if the body is sleeping.

unsigned int sleep_timer#

Internal sleep counter of the body.

bool is_attractor#

Flag reporting if the body is an attractor.

bool enable_collision#

Whether to collide this body with other bodies or not.

nv_uint32 collision_group#

Collision group of the body. Bodies that share the same non-zero group do not collide.

nv_uint32 collision_category#

Bitmask defining this body’s collision category.

nv_uint32 collision_mask#

Bitmask defining this body’s collision mask.

bool _cache_transform#

Internal flag reporting whether to cache AABB or not.

nvAABB _cached_aabb#

Internal flag reporting whether to cache vertices or not.

Enums#

enum nvBodyType#

Body type enumerator.

Values:

enumerator nvBodyType_STATIC#

Static bodies do not get affected or moved by any force in the simulation. They behave like they have infinite mass. Generally all terrain and ground objects are static bodies in games.

enumerator nvBodyType_DYNAMIC#

Dynamic bodies interact with all the other objects in the space and are effected by all forces, gravity and collisions in the simulation. Their mass is calculated by their shape, and unless you know what you’re doing, it’s not recommended to change their mass manually. However, if you want a dynamic body that can’t rotate, you can set it’s inertia to 0.

Methods#

nvBody *nvBody_new(nvBodyType type, nvShape *shape, nvVector2 position, nv_float angle, nvMaterial material)#

Create a new body.

Parameters:
  • type – Type of the body

  • shape – Shape of the body

  • position – Position of the body

  • angle – Angle of the body in radians

  • material – Material of the body

Returns:

nvBody *

void nvBody_free(void *body)#

Free body.

Parameters:

body – Body to free

void nvBody_calc_mass_and_inertia(nvBody *body)#

Calculate and update mass and moment of inertia of the body.

Parameters:

body – Body to calculate masses of

void nvBody_set_mass(nvBody *body, nv_float mass)#

Set mass (and moment of inertia) of the body.

Parameters:
  • body – Body

  • mass – Mass

void nvBody_set_inertia(nvBody *body, nv_float inertia)#

Set moment of inertia of the body.

Parameters:
  • body – Body

  • inertia – Moment of inertia

void nvBody_integrate_accelerations(nvBody *body, nvVector2 gravity, nv_float dt)#

Integrate linear & angular accelerations.

Parameters:
  • body – Body to integrate accelerations of

  • dt – Time step size (delta time)

void nvBody_integrate_velocities(nvBody *body, nv_float dt)#

Integrate linear & angular velocities.

Parameters:
  • body – Body to integrate velocities of

  • dt – Time step size (delta time)

void nvBody_apply_attraction(nvBody *body, nvBody *attractor, nv_float dt)#

Apply attractive force to body towards attractor body.

Parameters:
  • body – Body

  • attractor – Attractor body

  • dt – Time step size (delta time)

void nvBody_apply_force(nvBody *body, nvVector2 force)#

Apply force to body at its center of mass.

Parameters:
  • body – Body to apply force on

  • force – Force

void nvBody_apply_force_at(nvBody *body, nvVector2 force, nvVector2 position)#

Apply force to body at some local point.

Parameters:
  • body – Body to apply force on

  • force – Force

  • position – Local point to apply force at

void nvBody_apply_impulse(nvBody *body, nvVector2 impulse, nvVector2 position)#

Apply impulse to body at some local point.

Note

This method is mainly used internally by the engine.

Parameters:
  • body – Body to apply impulse on

  • impulse – Impulse

  • position – Local point to apply impulse at

Warning

doxygenfunction: Cannot find function “nvBody_apply_pseudo_impulse” in doxygen xml output for project “NovaPhysics” from directory: xml

void nvBody_sleep(nvBody *body)#

Sleep body.

Parameters:

body – Body

void nvBody_awake(nvBody *body)#

Awake body.

Parameters:

body – Body

nvAABB nvBody_get_aabb(nvBody *body)#

Get AABB (Axis-Aligned Bounding Box) of the body.

Parameters:

body – Body to get AABB of

Returns:

nvAABB

nv_float nvBody_get_kinetic_energy(nvBody *body)#

Get kinetic energy of the body in joules.

Parameters:

body – Body

Returns:

nv_float

nv_float nvBody_get_rotational_energy(nvBody *body)#

Get rotational kinetic energy of the body in joules.

Parameters:

body – Body

Returns:

nv_float

bool nvBody_get_is_attractor(nvBody *body)#

Get whether the body is attractor or not.

Parameters:

body – Body

Returns:

bool

void nvBody_local_to_world(nvBody *polygon)#

Transform body’s polygon shape’s vertices from local space to world space.

Parameters:

body – Body with polygon shape