Space#

struct nvSpace#

Space struct.

Space is the core of the simulation. It manages and simulates all bodies, constraints and collisions.

Public Members

nvArray *bodies#

Array of bodies in the space.

nvArray *attractors#

Array of attractive bodies in the space.

nvArray *constraints#

Array of constraints in the space.

nvArray *_removed_bodies#

Bodies that are waiting to be removed. You shouldn’t access this directly, instead use nvSpace_remove method.

nvArray *_killed_bodies#

Bodies that are waiting to be removed and freed. You shouldn’t access this directly, instead use nvSpace_kill method.

nvHashMap *res#

Set of collision resolutions.

nvVector2 gravity#

Global and uniform gravity applied to all bodies in the space. For gravitational attraction between body pairs, see attractive bodies.

bool sleeping#

Flag that specifies if space allows sleeping of bodies.

nv_float sleep_energy_threshold#

Threshold value which bodies sleep if they exceed it.

nv_float wake_energy_threshold#

Threshold value which bodies wake up if they exceed it.

unsigned int sleep_timer_threshold#

How long space should count to before sleeping bodies.

bool warmstarting#

Flag that specifies if solvers use warm-starting for accumulated impulses.

int collision_persistence#

Number of frames the collision resolutions kept cached.

nvPositionCorrection position_correction#

Position correction algorithm used.

nvBroadPhaseAlg broadphase_algorithm#

Broad-phase algorithm used to detect possible collisions.

nvSHG *shg#

Spatial Hash Grid object.

Warning

Should be only accessed if the used broad-phase algorithm is SHG.

nvAABB kill_bounds#

Boundary where bodies get deleted if they go out of.

bool use_kill_bounds#

Whether to use the kill bounds or not. True by default.

nvCoefficientMix mix_restitution#

Method to mix restitution coefficients of collided bodies.

nvCoefficientMix mix_friction#

Method to mix friction coefficients of collided bodies.

void *callback_user_data#

User data passed to collision callbacks.

nvSpace_callback before_collision#

Callback function called before solving collisions.

nvSpace_callback after_collision#

Callback function called after solving collisions.

nvProfiler profiler#

Profiler.

bool multithreading#

Whether multi-threading is enabled or not.

size_t thread_count#

Number of threads Nova Physics utilizes. 0 if multithreading is disabled.

nvTaskExecutor *task_executor#

Task executor.

nv_uint16 _id_counter#

Internal ID counter.

Methods#

nvSpace *nvSpace_new()#

Create new space instance.

Returns:

nvSpace *

void nvSpace_free(nvSpace *space)#

Free space.

Parameters:

space – Space to free

void nvSpace_set_broadphase(nvSpace *space, nvBroadPhaseAlg broadphase_alg_type)#

Set the current broadphase algorithm used to check possible collision pairs.

Parameters:
  • space – Space

  • broadphase_type – Broadphase algorithm

void nvSpace_set_SHG(nvSpace *space, nvAABB bounds, nv_float cell_width, nv_float cell_height)#

Create & set a new SHG and release the old one.

Parameters:
  • space – Space

  • bounds – Boundaries of the new SHG

  • cell_width – Cell width of the new SHG

  • cell_height – Cell height of the new SHG

void nvSpace_clear(nvSpace *space)#

Clear and free everything in space.

Parameters:

space – Space

void nvSpace_add(nvSpace *space, nvBody *body)#

Add body to space.

Parameters:
  • space – Space

  • body – Body to add

void nvSpace_remove(nvSpace *space, nvBody *body)#

Remove body from the space.

The removal will not pe performed until the current simulation step ends. After removing the body managing body’s memory belongs to user. You should use nvBody_free if you are not going to add it to the space again.

Parameters:
  • space – Space

  • body – Body to remove

void nvSpace_kill(nvSpace *space, nvBody *body)#

Remove body from the space and free it.

The removal will not pe performed until the current simulation step ends. Unlike nvSpace_remove, this method also frees the body. It can be useful in games where references to bullets aren’t usually kept.

Parameters:
  • space – Space

  • body – Body to remove and free

void nvSpace_add_constraint(nvSpace *space, nvConstraint *cons)#

Add constraint to space.

Parameters:
  • space – Space

  • cons – Constraint to add

void nvSpace_step(nvSpace *space, nv_float dt, size_t velocity_iters, size_t position_iters, size_t constraint_iters, size_t substeps)#

Advance the simulation.

Iteration counts defines how many iterations the solver uses to converge constraints. Higher the iteration count, more accurate simulation but higher CPU usage, thus lower performance. Velocity and position iteration counts are used for the contact constraint solver. Constraint iteration count is used for other constraints like joints. For a game, it is usually sufficient to keep them around 5-10.

Substep count defines how many substeps the current simulation step is going to get divided into. This effectively increases the accuracy of the simulation but also impacts the performance greatly because the whole simulation is processed and collisions are recalculated by given amounts of times internally. In a game, you wouldn’t need this much detail. Best to leave it at 1.

Parameters:
  • space – Space instance

  • dt – Time step size (delta time)

  • velocity_iters – Velocity solving iteration count

  • position_iters – Position solving iteration count

  • constraint_iters – Constraint solving iteration count

  • substeps – Substep count

void nvSpace_enable_sleeping(nvSpace *space)#

Enable sleeping.

Parameters:

space – Space

void nvSpace_disable_sleeping(nvSpace *space)#

Disable sleeping.

Parameters:

space – Space