Body

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

Import Statement: import
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().


Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support
Sign up for Free and start developing right away!
I want to make Games
I want to make Apps
Game Development
Cross-Platform, Any Resolution

Use one IDE to deploy to all supported platforms, including iOS and Android, from a single code base.

Support all screen resolutions, aspect ratios and sizes with auto-adapting UI elements, smart scene scaling and memory efficient asset selection.

Learn More

V-Play ranked #1 at research2guidance cross-platform tool benchmarking 2014 by 2188 developers comparing 40 leading tools.

Multiplayer

V-Play Multiplayer supports both real-time and turn-based gameplay, so you can use it to make many different types of cross-platform multiplayer games. It's perfect for making player-vs-player games like 'Words with Friends' or games for a large amount of players, such as 'Clash of Clans'.

Learn More Video

Highscores & Achievements

The V-Play Game Network is a cross-platform gaming service that allows players around the world to compare game highscores and achievements. Players can also challenge each other across multiple platforms and share their progress on Facebook.

Learn More

QML & JavaScript

Qt Meta Language is a highly intuitive reactive language, which is super easy to learn, yet it's extremely powerful and flexible. Mix it with JavaScript to create awesome stuff, with just a few lines of code.

import QtQuick 2.0
import VPlay 2.0

GameWindow {
  Scene {
    
    SimpleButton {
      anchors.centerIn: parent
      text: "Press Me"
      onClicked: {
        console.debug("Wow you pressed me!")
      }
    }
  }
}

Learn More

Monetize & Analyze

With V-Play, you can use many 3rd party services for ads, in-app purchases, analytics and more, with just a few lines of code. All of these plugins work cross-platform, which allows you to maintain a single code base.

Learn More

Level Editor

The LevelEditor can be used during development to create and modify levels for your game, which you can then bundle in your final publishing build. Additionally, you can also integrate the in-game level editor to your published game and let your gamers create new levels.

Learn More Video

Card Game like UNO, Hearthstone or Poker

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/OneCard/OneCard.pro

Match-3 like Candy Crush Saga

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/JuicySquash/JuicySquash.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/JuicySquashAdvanced/JuicySquashAdvanced.pro

Puzzle like 2048 or Threes!

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/2048/2048.pro

Casino like Big Win Slots

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/FlaskOfRum/FlaskOfRum.pro

Side Scroller like Jetpack Joyride or Flappy Bird

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/FlappyBird/FlappyBird.pro

Tower Defense like Castle Defense or Bloons TD

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/Squaby/Squaby.pro

Falldown & Jump like Doodle Jump or Mega Jump

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/ChickenOutbreak/ChickenOutbreak.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/ChickenOutbreak2/ChickenOutbreak2.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/DoodleJump/DoodleJump.pro

Platformer like Super Mario or Lep's World

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/Platformer/Platformer.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/PlatformerWithLevelEditor/PlatformerWithLevelEditor.pro

Action like Angry Birds, Fruit Ninja, Cut the Rope

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/StackTheBoxWithCommunityEditor/StackTheBoxWithCommunityEditor.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/BalloonPop/BalloonPop.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/CarChallenge/CarChallenge.pro

Arcade like Arkanoid or Space Invaders

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/ZombieBreak/ZombieBreak.pro

Community like Super Mario Maker or Minecraft

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/examples/Squaby/Squaby.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/StackTheBoxWithCommunityEditor/StackTheBoxWithCommunityEditor.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/PlatformerWithLevelEditor/PlatformerWithLevelEditor.pro

Any other Idea? let us know how we can help you

You are looking for another demo?

The V-Play SDK includes many open-source demos for different game genres. You can use their source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

If you do not find your game genre in this list and wonder if V-Play is the right choice, just contact us, we are happy to help!

Contact Us

App Development
Better Apps, Less Effort

Develop feature-rich, cross-platform mobile apps from a single code base.

V-Play apps look, feel and perform exactly like native iOS, Android and Desktop apps. With less code & much faster development time.

Learn More Highlights Video

V-Play ranked #1 at research2guidance cross-platform tool benchmarking 2014 by 2188 developers comparing 40 leading tools.

Save Time, Code & Money

Save up to 90% source code with V-Play, compared to frameworks like Xamarin, Titanium, React Native and others.

Learn More

QML & JavaScript

Qt Meta Language is a highly intuitive reactive language, which is super easy to learn, yet it's extremely powerful and flexible. Mix it with JavaScript to create awesome stuff, with just a few lines of code. QML also allows you to easily create smooth and complex animations.

import QtQuick 2.0
import VPlayApps 2.0

App {
  Page {
    
    AppButton {
      anchors.centerIn: parent
      text: "Press Me"
      onClicked: {
        console.debug("Wow you pressed me!")
      }
    }
  }
}

Learn More

Monetize & Analyze

With V-Play, you can use many 3rd party services for ads, in-app purchases, analytics and more, with just a few lines of code. All of these plugins work cross-platform, which allows you to maintain a single code base.

Learn More

Native Sensors & More

V-Play Apps harness the power of Qt, the leading cross-platform development framework used by over 1,000,000 developers.

This gives your access to native device features like sensors, camera, file system as well as multimedia, networking, localization and much more.

Learn More

Responsive Design

V-Play has built in responsive design capabilities, you can target phones and tablets with the same source code.

Learn More

Highly Extensible

You have existing JavaScript, C++ or native code? You want to add any 3rd party SDK that we do not offer already?

No worries, you can add any JS, C++, Java or Objective-C code to your project.

Learn More

Component Showcase App shows the most important V-Play features and components

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/showcase/Showcase.pro

Qt World Summit Conference App a full-featured conference management app made by V-Play

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/qtws2016/QtWS2016.pro

Twitter App how to build layouts like in the official Twitter app for iOS and Android

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/twitter/Twitter.pro

Maps App displays free bikes or boxes at bike stations for Vienna's bike sharing service Citybike Wien

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/maps/Maps.pro

Messaging App demonstrates how to create the UI of a Facebook Messenger like app

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/messaging/Messaging.pro

Weather App how to include powerful animations into your user interface

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/waether/Weather.pro

Any other Idea? let us know how we can help you

You are looking for another app demo?

The V-Play SDK includes many open-source demos for different app types. You can use their source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play..

If you do not find your app type in this list and wonder if V-Play is the right choice, just contact us, we are happy to help!

Contact Us