Axmol 3.0.0-89d6d9a
Loading...
Searching...
No Matches
PhysicsWorld2D Class Reference

An PhysicsWorld2D object simulates collisions and other physical properties. More...

Public Member Functions

bool isGlobalEventEnabled (ContactEventBits events) const
 Check whether specific collision events are globally enabled in this physics world.
void setGlobalEventEnabled (ContactEventBits events, bool enabled)
 Enable or disable specific collision events globally in this physics world.
void setPreSolveCallback (PreSolveCallback cb)
 Set the PreSolve callback for the 2D physics world.
void rayCast (RayCastHitCallback2D func, const Ray2D &ray, void *data=nullptr)
 Searches for physics shapes that intersects the ray.
void overlapBox (PhysicsQueryCallback2D func, const Rect &rect, void *data)
 f Enumerates all physics shapes whose bounding box overlaps the specified rectangle.
Collider2D * overlapBox (const Rect &rect) const
 Returns the nearest physics shape whose bounding box overlaps the specified rectangle.
Vector< Collider2D * > overlapBoxAll (const Rect &rect) const
 Returns all physics shapes whose bounding box overlaps the specified rectangle.
void overlapPoint (PhysicsQueryCallback2D func, const Vec2 &point, void *data)
 Enumerates all physics shapes that contain the specified point.
Collider2D * overlapPoint (const Vec2 &point) const
 Returns the nearest physics shape that contains the specified point.
Vector< Collider2D * > overlapPointAll (const Vec2 &point) const
 Returns all physics shapes that contain the specified point.
Scene * getScene () const
 Get a scene contain this physics world.
Vec2 getGravity () const
 Get the gravity value of this physics world.
void setGravity (const Vec2 &gravity)
 set the gravity value of this physics world.
void setSpeed (float speed)
 Set the speed of this physics world.
float getSpeed ()
 Get the speed of this physics world.
void setSubsteps (int steps)
 set the number of substeps in an update of the physics world.
int getSubsteps () const
 Get the number of substeps of this physics world.
void setPreUpdateCallback (const std::function< void()> &callback)
 set the callback which invoked before update of each object in physics world.
void setPostUpdateCallback (const std::function< void()> &callback)
 set the callback which invoked after update of each object in physics world.
void setAutoStep (bool autoStep)
 To control the step of physics.
bool isAutoStep ()
 Get the auto step of this physics world.
void stepSimulation (float delta)
 Advances the physics simulation by one step.

Static Public Attributes

static const int DEBUGDRAW_NONE
 draw nothing
static const int DEBUGDRAW_SHAPE
 draw shapes
static const int DEBUGDRAW_JOINT
 draw joints
static const int DEBUGDRAW_CONTACT
 draw contact
static const int DEBUGDRAW_ALL
 draw all

Detailed Description

An PhysicsWorld2D object simulates collisions and other physical properties.

You do not create PhysicsWorld2D objects directly; instead, you can get it from an Scene object.

Member Function Documentation

◆ isGlobalEventEnabled()

bool isGlobalEventEnabled ( ContactEventBits events) const

Check whether specific collision events are globally enabled in this physics world.

This method verifies if the given event bits are allowed by the world's global event mask. Even if a collider enables these events locally, they will only take effect if the world also permits them.

Parameters
eventsCollision event bits to check.
Returns
True if all specified events are enabled globally, false otherwise.

◆ setGlobalEventEnabled()

void setGlobalEventEnabled ( ContactEventBits events,
bool enabled )

Enable or disable specific collision events globally in this physics world.

This modifies the world's global event mask by turning on or off the specified event bits. Colliders can only generate these events if they are enabled both locally and globally.

Note
This setting should be applied before any rigid-body components are added to nodes, because global event state cannot be changed at runtime once rigidbodies are active. By default, all global collision events are disabled (mask = 0).
Parameters
eventsCollision event bits to modify.
enabledTrue to enable the specified events globally, false to disable them.

◆ setPreSolveCallback()

void setPreSolveCallback ( PreSolveCallback cb)

Set the PreSolve callback for the 2D physics world.

The PreSolve callback is invoked before constraint resolution, allowing the user to decide whether a contact should be accepted or rejected. This mimics Box2D-style PreSolve behavior.

Parameters
cbUser-provided callback function with signature: bool(const ContactInfo2D&)
  • Return true to accept the contact.
  • Return false to reject the contact.
Warning
PreSolve callbacks are potentially dangerous:
  • Avoid race conditions and shared state modifications.
  • Do not perform blocking or long-running operations.
  • Execution order of multiple callbacks is not guaranteed, which may lead to non-deterministic behavior.
Note
If no callback is set, all contacts are accepted by default.

◆ rayCast()

void rayCast ( RayCastHitCallback2D func,
const Ray2D & ray,
void * data = nullptr )

Searches for physics shapes that intersects the ray.

Query this physics world along the line segment from start to end.

Parameters
funcFunc is called for each shape found.
startA Vec2 object contains the begin position of the ray.
endA Vec2 object contains the end position of the ray.
dataUser defined data, it is passed to func.

◆ overlapBox() [1/2]

void overlapBox ( PhysicsQueryCallback2D func,
const Rect & rect,
void * data )

f Enumerates all physics shapes whose bounding box overlaps the specified rectangle.

Queries the physics world for shapes intersecting the given rect. For each overlapping shape, the provided callback function is invoked.

Parameters
funcCallback function executed for each overlapping shape.
rectRectangle region (x, y, width, height) used for overlap testing.
dataUser-defined data passed to the callback.

◆ overlapBox() [2/2]

Collider2D * overlapBox ( const Rect & rect) const

Returns the nearest physics shape whose bounding box overlaps the specified rectangle.

Queries the physics world at the given rect and finds the closest shape that intersects it. If no shape overlaps the rect, nullptr is returned.

Parameters
rectRectangle region (x, y, width, height) used for overlap testing.
Returns
Pointer to the nearest Collider2D overlapping the rect, or nullptr if none were found.

◆ overlapBoxAll()

Vector< Collider2D * > overlapBoxAll ( const Rect & rect) const

Returns all physics shapes whose bounding box overlaps the specified rectangle.

Queries the physics world and collects every shape intersecting the given rect into a vector.

Parameters
rectRectangle region (x, y, width, height) used for overlap testing.
Returns
A vector of Collider2D pointers representing all shapes overlapping the rect.

◆ overlapPoint() [1/2]

void overlapPoint ( PhysicsQueryCallback2D func,
const Vec2 & point,
void * data )

Enumerates all physics shapes that contain the specified point.

Queries the physics world for shapes covering the given point. For each shape that contains the point, the provided callback function is invoked.

Attention
The point must lie inside at least one shape to trigger callbacks.
Parameters
funcCallback function executed for each shape containing the point.
pointPosition of the point to test.
dataUser-defined data passed to the callback.

◆ overlapPoint() [2/2]

Collider2D * overlapPoint ( const Vec2 & point) const

Returns the nearest physics shape that contains the specified point.

Queries the physics world at the given point and finds the closest shape that covers it. If no shape contains the point, nullptr is returned.

Parameters
pointPosition of the point to test.
Returns
Pointer to the nearest Collider2D containing the point, or nullptr if none were found.

◆ overlapPointAll()

Vector< Collider2D * > overlapPointAll ( const Vec2 & point) const

Returns all physics shapes that contain the specified point.

Queries the physics world and collects every shape covering the given point into a vector.

Attention
The point must lie inside at least one shape to produce results.
Parameters
pointPosition of the point to test.
Returns
A vector of Collider2D pointers representing all shapes that contain the point.

◆ getScene()

Scene * getScene ( ) const
inline

Get a scene contain this physics world.

Attention
This value is initialized in constructor
Returns
A Scene object reference.

◆ getGravity()

Vec2 getGravity ( ) const
inline

Get the gravity value of this physics world.

Returns
A Vec2 object.

◆ setGravity()

void setGravity ( const Vec2 & gravity)

set the gravity value of this physics world.

Parameters
gravityA gravity value of this physics world.

◆ setSpeed()

void setSpeed ( float speed)
inline

Set the speed of this physics world.

Attention
if you setAutoStep(false), this won't work.
Parameters
speedA float number. Speed is the rate at which the simulation executes. default value is 1.0.

◆ getSpeed()

float getSpeed ( )
inline

Get the speed of this physics world.

Returns
A float number.

◆ setSubsteps()

void setSubsteps ( int steps)

set the number of substeps in an update of the physics world.

One physics update will be divided into several substeps to increase its accuracy.

Parameters
stepsAn integer number, default value is 1.

◆ getSubsteps()

int getSubsteps ( ) const
inline

Get the number of substeps of this physics world.

Returns
An integer number.

◆ setAutoStep()

void setAutoStep ( bool autoStep)
inline

To control the step of physics.

If you want control it by yourself( fixed-timestep for example ), you can set this to false and call step by yourself.

Attention
If you set auto step to false, setSpeed setSubsteps won't works, you need to control the time step by yourself.
Parameters
autoStepA bool object, default value is true.

◆ isAutoStep()

bool isAutoStep ( )
inline

Get the auto step of this physics world.

Returns
A bool object.

◆ stepSimulation()

void stepSimulation ( float delta)

Advances the physics simulation by one step.

By default, the physics world runs with auto-step enabled, and this function is called automatically each frame. If you disable auto-step via setAutoStep(false), you must call this function manually to update the simulation.

Parameters
deltaThe time step (in seconds) to simulate.

The documentation for this class was generated from the following file:
  • PhysicsWorld2D.h