Represents a Box2D physics world including bodies and joints. More...
Import Statement: | import Felgo 4.0 |
Inherits: | |
Inherited By: |
This wraps the underlying Box2D b2World object as a QML type.
Felgo provides additional functionality in the PhysicsWorld component.
autoClearForces : bool |
If this is set to true, the forces applied to Bodies will be cleared every time step() with the clearForces() function.
The default value is true.
enableContactEvents : bool |
If this is set to false, no contact events will be sent out by the physics engine.
The default value is true.
gravity : point |
This property holds the x and y gravity of the PhysicsWorld in meters/second^2, which is the constant acceleration applied to all physics bodies.
The default gravity is {"x": 0, "y": 0}.
If you want to use earth gravity, set it to 0/9.81, for example with this code:
PhysicsWorld { gravity.y: 9.81 }
To make objects fall faster, increase the y value. Keep in mind, that also the applied forces should be adopted in case of a gravity change to have the same outcome.
pixelsPerMeter : real |
Configures the conversion factor between QML units, pixels and Box2D units, meters.
Box2D uses meters as units for all distances, velocities, forces etc. In QML, pixels are used. In older version of Felgo, the scaling ratio between pixels and meters was fixed to 32 pixels / meter.
This ratio can be configured using the World::pixelsPerMeter property. It defaults to 32 so no change is needed for older games. This is useful to create more accurate physics simulation by specifying how big the simulated area on the screen would be in a real world. For example, if your game world should be 5 meters wide, and your Scene has a width of 480 logical pixels, this property should be set to 480 pixels / 5 meter = 96 pixels / meter.
This property affects the conversion of units between QML and Box2D for properties like sizes, distances, velocities, acceleration, forces, torques and densities. It also affects the mass of bodies, which gets calculated based on their fixture's dimensions in meters and their density.
The default value is 32 pixels/meter.
positionIterations : int |
This property holds the number of position iterations used to process in one physics update step. Tune this variable for each game so it still looks good and set is as low as possible for best performance. Higher values cause the physics system to take longer, whereas lower values might cause unrealistic behaviors like merging colliders. Modify this value together with the PhysicsWorld::updatesPerSecondForPhysics property and the World::velocityIterations property.
By default, it is set to 5.
See also velocityIterations and PhysicsWorld::updatesPerSecondForPhysics.
profile : Profile |
running : bool |
This property holds if the physics world is simulated. The default value is true
. Set it to false when you would like to pause the game (e.g. when in the game menu).
timeStep : real |
This determines the time length of one physics step. The step() function is then attempted to be called 1 / timeStep times per second. Note that it may be called less or more times based on current hardware workload, but the dt parameter will be adapted accordingly.
It gets normally automatically set to 1 / PhysicsWorld::updatesPerSecondForPhysics and does not need to be set manually.
See also PhysicsWorld::updatesPerSecondForPhysics.
velocityIterations : int |
This property holds the number of velocity iterations used to process in one physics update step. Tune this variable for each game so it still looks good and set is as low as possible for best performance. Higher values cause the physics system to take longer, whereas lower values might cause unrealistic behaviors like merging colliders. Modify this value together with the PhysicsWorld::updatesPerSecondForPhysics property and the World::positionIterations property.
By default, it is set to 5.
See also positionIterations and PhysicsWorld::updatesPerSecondForPhysics.
postSolve(Contact contact) |
Called after a contact event has been passed to the contact solver of the physics engine. It does not make sense to modify the contact parameter at this time.
Note: The corresponding handler is onPostSolve
.
See also preSolve.
preSolve(Contact contact) |
Called before a contact event is passed to the contact solver of the physics engine. The parameter contact contains information about the contact event.
This can be used to completely disable a contact event and allow two bodies to temporarily pass through each other by setting contact.enabled
to false. This can be useful if colliders change behavior, for
example only letting objects pass through from one side, like in a platformer game:
PhysicsWorld { id: physicsWorld gravity: Qt.point(0, 25) onPreSolve: contact => { //this is called before the Box2DWorld handles contact events var entityA = contact.fixtureA.getBody().target var entityB = contact.fixtureB.getBody().target if(entityB.entityType === "platform" && entityA.entityType === "player" && entityA.y + entityA.height > entityB.y) { //by setting enabled to false, they can be filtered out completely //-> disable cloud platform collisions when the player is below the platform contact.enabled = false } } }
Furthermore, other properties of the involved bodies and fixtures, like velocity, force, friction and restitution can be changed here before the contact gets handled.
Note: The corresponding handler is onPreSolve
.
See also postSolve.
stepped() |
Called when a time step has been processed.
Note: The corresponding handler is onStepped
.
Box2DBody bodyAt(point p) |
Returns the Box2D Body at the given point p in world coordinates or null if there is no body at the given point or if the body is inactive.
Note: This function is slow as linear search is performed and should only be used if not many fixtures exist. It is also not guaranteed to find the top-most body, as the order is undefined! This function is blocking (so no callback involved), so use with care! Also note that only active bodies are considered.
void clearForces() |
Clears all forces applied to all bodies.
Cast a ray(rayCast) from the point from to the point to and report fixtures in the ray's direction in RayCast::fixtureReported.
void step(real dt) |
Manually perform a physics world time step with the given delta time(dt). This is normally called 1 / timeStep times per second automatically.