Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Body

A physics body which consists of one or more Fixture elements. More...

Import Statement: import Felgo 4.0
Inherits:

QtObject

Properties

Methods

Detailed Description

For the most use cases, only a single fixture of type Box or Circle is sufficient. Thus a Body and Fixture are combined in a single component in BoxCollider, CircleCollider and PolygonCollider.

 Body {
     fixtures: Circle {
         radius: 12.5
     }
 }

Property Documentation

active : bool

This property holds whether the physics Body is active, i.e. if it is affected by the physics simulation.

In a collider object, this property is connected to the EntityBase::collidersActive property, which is by default true.


angularDamping : real

Damping is used to reduce the world velocity of bodies. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together.

Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1. A value of 1 would mean that the velocity reduces to roughly 1/2 every second.

The default value is 0.


angularVelocity : real

This property holds the angular speed of the body at the center of mass in degrees/second, clockwise.


awake : bool

This property holds whether the physics Body is not currently sleeping for performance improvements. Sleeping bodies are unaffected by the physics simulation. It can be set to false to wake a sleeping body up.


bodyType : BodyType

Set this property to either Body.Static, Body.Dynamic or Body.Kinematic.

A dynamic body is a body which is affected by world forces and reacts to collisions.

A static body is a body which isn’t affected by world forces it does not react to collisions.

A kinematic body is a hybrid body which is not affected by forces and collisions like a static body but can moved with a linear velocity like a dynamic body.

Note that ONLY dynamic bodies can collide with each other.

By default, bodyType is set to Body.Dynamic.


bullet : bool

Set this flag to true for a very fast moving, small body that may otherwise tunnel through other bodies. Bullet bodies will use a more complex collision detection, which makes the performance worse, so use these only if necessary.

The default value is false.


collisionTestingOnlyMode : bool

Set this property to false when the position & rotation should be updated by Box2D (the default value). When you set it to true, the positions are taken from the target.

So if set to true, also specify a target whose position & rotation should get set to the physics body. It is also supported to change this property at runtime, so e.g. when testing for collisions while dragging an entity.

By default, this property is set to false.


fixedRotation : bool

Set this property to true if you do not want the body to rotate. The default value is false, so the body can freely rotate.


fixtures : list<Fixture>

Use this property to add fixtures in your derived components.

Here is an example:

 Body {
   fixtures: Circle {
     radius: 5
   }
 }

Fixtures is also the default property of Body, so the fixtures: part could be omitted.


force : point

If you set this property, this force is applied continuously every frame from the center of the body. The force is set in kg*pixels/s^2 and is 0 by default.

It is a convenience function for applying forces, which should usually be done over a longer time period. So instead of setting up a Timer object and call applyForce() in there, you can set this property to anything non-zero and it is applied automatically.

If you want to apply a force from an origin point that is not the body center, you can use applyForce(). An applied force from no center has the effect of the object being rotated. Except if the fixedRotation property is set.

Note: A force is only applied when the bodyType is not set to Body.Static. Applying the force wakes up the body, so awake gets set to true if it was false before.

See also applyForce().


gravityScale : real

Scales the gravity defined in the PhysicsWorld before being applied to this body. A scale of 0 would mean this body is not affected by gravity.

The default value is 1.0, so the defined gravity is unchanged.


linearDamping : real

Damping is used to reduce the world velocity of bodies. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together.

Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1. A value of 1 would mean that the velocity reduces to roughly 1/2 every second.

The default value is 0.


linearVelocity : point

The linearVelocity holds the current speed in x and y direction of the physics body at the center of mass in local coordinates in pixels/second.

If you want to retrieve the world coordinates of the current velocity, use Body::toWorldVector(linearVelocity).

You can also calculate the rotation of a local linearVelocity by using Math::atan2(). Consider this example:

 // get the current local forward vector
 var localForward = linearVelocity;
 // mirror the current velocity horizontally
 localForward.x *= -1;
 // calculate the new angle of the mirrored direction
 var newAngle = Math.atan2(localForward.y, localForward.x);
 // set the mirrored angle to the entity AND to the collider, as the rotation of the entity is not automatically forwarded because collisionTestingOnlyMode is set to false
 entity.rotation = newAngle;
 collider.rotation = newAngle;

sleepingAllowed : bool

This property holds whether the body is allowed to get inactive for performance optimizations. The default value in a collider object is the inverse of collisionTestingOnlyMode, so sleepingAllowed: !collisionTestingOnlyMode. This is needed to avoid sleeping when the collider gets used for collision detection, because if no forces or impulses are applied to the body it will sleep by default.


target : Item

Returns and sets the target QML Item this Body synchronizes its state with. By default, target is set to the Body's parent Item.

If the target is set explicitly, the position of target is used for synchronizing. This allows adding bodies anywhere as child of to track objects and allows component-based design.

If collisionTestingOnlyMode is false, the target Item's transform will be synchronized from the Body. If it is true, this gets reversed and the Body's transform will be synchronized from the target Item.


torque : real

If you set this property, this torque is applied continuously every frame. This affects the angular velocity without affecting the linear velocity. The torque is in clockwise kg*pixels^2/seconds^2.

This is a convenience function for applying torques, which should usually be done over a longer time period. So instead of setting up a Timer object and call applyTorque() in there, you can set this property to anything non-zero and it is applied automatically.

Note: A torque is only applied when the bodyType is not set to Body.Static. Applying the torque wakes up the body, so active gets set to true if it was false before.

See also applyTorque().


world : World

Returns and sets the World this Body belongs to. It is set by default to the currently active world managed in Box2D::defaultWorld. If you use more than one World, it needs to be set manually.


Method Documentation

void addFixture(Fixture fixture)

Add a new fixture to this Body at runtime. This method is necessary because the fixtures property can only be defined by the QML component statically.


void applyAngularImpulse(real impulse)

Applies an angular impulse at the center of the body. This affects the angular velocity without affecting the linear velocity. The impulse is in clockwise kg*pixels^2/second.

This is like applying the strength of 1 second of torque instantly.


void applyForce(point force, point worldCenter)

Applies a force at the worldCenter point of the body. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. A call of this function wakes up the body. The force is set in kg*pixels/s^2.

If you want to apply a force towards the current direction of the body (so towards the current rotation), use the following method:

 EntityBase {

   BoxCollider {
     id: boxCollider
   }

   Keys.onUpPressed: {
     // first transform the local forward vector to the world coordinate system
     // instead of 100, add a force to suit your games needs, depending on the mass of your body
     var localForward = boxCollider.body.toWorldVector(Qt.point(100,0));
     // then apply the force to the body
     boxCollider.body.applyForce(localForward, targetBody.getWorldCenter());
   }
 }

Note: The force is only applied once when this function is called! To simulate a continuous force, use the Timer element like in this example:

 EntityBase {

   BoxCollider {
     id: boxCollider
     width: 30
     height: 20
   }

   Timer {
     repeat: true
     duration: 33 // for 30 updates per second
     onTriggered: {
       // apply a force towards the world position 100/0 every 33ms
       boxCollider.body.applyForce(Qt.point(100, 0), boxCollider.body.getWorldCenter() );
     }
   }
 }

If you want to apply the force at the body center, you can also use the force property. It is equivalent to the above solution, and applies a continuous force while the force property is non-zero. This shows the above example with the force property:

 EntityBase {

   BoxCollider {
     id: boxCollider
     width: 30
     height: 20

     // the force gets applied automatically every frame
     force: Qt.point(100, 0)
   }
 }

See also applyLinearImpulse() and applyTorque().


void applyForceToCenter(point force)

Shorthand for applyForce(force, getWorldCenter()).


void applyLinearImpulse(point impulse, point worldCenter)

Applies an impulse at the worldCenter point of the body. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. A call of this function wakes up the body. The force is set in kg*pixels/second.

This is like applying the strength of 1 second of force instantly.

Here is an example to apply an impulse in the forward direction, so in the direction the entity is currently rotated to, when the "Up" key is pressed:

 EntityBase {

   BoxCollider {
     id: boxCollider
     width: 100
     height: 50
   }

   // the focus is required so the keyboard press can be handled
   Keys.focus: true
   Keys.onUpPressed: {
     // localForwardVector points towards the forward direction - if rotated at 0 degrees, that is to the right
     var localForwardVector = boxCollider.body.toWorldVector(Qt.point(1500,0));

     boxCollider.body.applyLinearImpulse(localForwardVector, boxCollider.body.getWorldCenter());
   }
 }

See also force, applyForce(), and applyTorque().


void applyTorque(real torque)

Applies a torque at the center of the body. This affects the angular velocity without affecting the linear velocity. The torque is in clockwise kg*pixels^2/second^2.

Note: A torque is only applied when the bodyType is not set to Body.Static. Applying the torque wakes up the body, so active gets set to true if it was false before.

See also torque.


real getInertia()

Returns the computed inertia of this Body.


point getLocalCenter()

Returns the the local coordinates of the center of mass of this body.


real getMass()

Returns the computed mass of this Body, in kg. If the fixtures changes, call resetMassData() to recompute the mass.


point getWorldCenter()

Returns the the world coordinates of the center of mass of this body.


void resetMassData()

Reset the computed mass to let the physics engine recompute it. Can be used when the mass a Body should change, for example after modifying the density of fixtures.


point toLocalPoint(point worldPoint)

Converts a point which is in the World's coordinate system to local coordinates respective to this Body. This will apply the Body's rotation and translation and should be used for points like positions.

For converting vectors, use toLocalVector().


point toLocalVector(point worldVector)

Converts a vector which is in the World's coordinate system to local coordinates respective to this Body. This will only apply the Body's rotation and not translation and should be used for vectors like forces.

For converting points, use toLocalPoint().


point toWorldPoint(point localPoint)

Converts a point which is in this Body's local coordinate system to world coordinates. This will apply the Body's rotation and translation and should be used for points like positions.

For converting vectors, use toWorldVector().


point toWorldVector(point localVector)

Converts a vector which is in this Body's local coordinate system to world coordinates. This will only apply the Body's rotation and not translation and should be used for vectors like forces.

For converting points, use toWorldPoint().


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded