PolygonCollider

The PolygonCollider is a physics body with a polygonal shape set up with at least 3 vertices. More...

Import Statement: import VPlay 2.0
Inherits:

ColliderBase

Properties

Detailed Description

The vertices need to be defined in counter clockwise order and must have a convex shape. Convex means, an imaginary line drawn through the polygon meets its boundary exactly twice.

The origin of a vertex at position 0/0 is the top left corner. If you want to change the origin, you can adjust the x and y position of the PolygonCollider.

The following is an example with a trapezoid polygon and a rectangle:

 import VPlay 2.0
 import QtQuick 2.0
 GameWindow {

   PhysicsWorld {
     z: 1 // this draws the debugDraw on top of the entity
     debugDrawVisible: true
   }

   Scene {

     EntityBase {

       x: 30

       entityId: "trapezoid1"
       entityType: "trapezoidEntity"

       Rectangle {
         width: 100
         height: 100
         color: "red"
         opacity: 0.3
       }

       PolygonCollider {

         vertices: [
           Qt.point(-30, 0), // top left
           Qt.point(0, 100), // bottom left
           Qt.point(130, 100), // bottom right
           Qt.point(100, 0) // top right
         ]

       }
     }

   }

 }

The following image shows the physics shape (green) and the red rectangle of the above code. The physics shape's top left vertex is at x position -30, and the rectangle starts at position 0. See PhysicsWorld how to enable the physics debug view.

For more information on how to move entities based on physics or without physics, see the BoxCollider documentation and How to Move Entities & Objects with V-Play.

See also BoxCollider and CircleCollider.

Property Documentation

categories : CategoryFlags

The properties categories, collidesWith and groupIndex are used for collision filtering. That is useful if you want only some of your fixtures to collide with each other. By default, all fixtures collide with each other, so the default categories is Category1. The default collidesWith is All, and the default groupIndex is 0.

For example, say you make a character that rides a bicycle. You want the bicycle to collide with the terrain and the character to collide with the terrain, but you don't want the character to collide with the bicycle (because they must overlap). Therefore Box2D supports such collision filtering using categories and groups.

Box2D supports 16 collision categories. For each fixture you can specify which category it belongs to. You also specify what other categories this fixture can collide with. For example, you could specify in a game that all players don't collide with each other and enemies don't collide with each other, but players and enemies should collide. You might also have powerups in your game, with which the player should be able to collide, but not the monsters. This can be done with masking bits. For example:

 Scene {
   EntityBase {
     entityType: "player"

     PolygonCollider {
       categories: Circle.Category1
       // collide with enemies and powerups
       collidesWith: Circle.Category2 | Circle.Category3
     }
   }

   EntityBase {
     entityType: "enemy"

     PolygonCollider {
       categories: Circle.Category2
       // collide with players
       collidesWith: Circle.Category1
     }
   }

   EntityBase {
     entityType: "powerup"

     PolygonCollider {
       categories: Circle.Category3
       // collide with players
       collidesWith: Circle.Category1
     }
   }
 }

The groupIndex can also be used to choose fixtures that collide with each other: Fixtures with the same positive group index will always collide, regardless of their categories or collidesWith settings. Fixtures with the same negative groupIndex will never collide, regardless of categories or collidesWith.

Note: Only dynamic bodies collide with others. So 2 static bodies or 2 kinematic bodies can never collide with each other. When you use Joints to connect 2 fixtures, you can enable or disable collisions between these connected fixtures.

See also Fixture::categories, Fixture::collidesWith, and Fixture::groupIndex.


collidesWith : CategoryFlags

See the categories property documentation.


density : real

This property holds the density in kg/pixel^2. The fixture density is used to compute the mass properties of the parent body. The density can be 0 or positive. You should generally use similar densities for all your fixtures. This will improve stacking stability.

The default value is 0. The masses of all fixtures of a Body get added for dynamic bodies. If none of the fixtures of a body has a density set, the default body mass is set to 1kg.

Consider that static and kinematic bodies internally do not have a mass, so setting the density for them is useless.

If you want to make objects fall down faster, increase the PhysicsWorld::gravity property.

See also Fixture::density.


fixture : Fixture

This property alias allows access to the Circle physics shape, which is called a fixture in Box2D.

Usually, you will not directly need to access this property, because you can access all fixture properties by the provided alias properties.


friction : real

Friction is used to make objects slide along each other realistically. It is usually in the range [0,1]. The default value is 0.2. A friction value of 0 turns off friction and a value of 1 makes the friction strong. If any of 2 colliding shapes has 0 friction, the resulting friction is 0 as the friction values get multiplied.

See also Fixture::friction.


groupIndex : CategoryFlags

See the categories property documentation.


restitution : real

Restitution is used to make objects bounce, so to simulate elastic objects like a rubber ball. It is usually in the range [0,1]. The default value is 0. Consider dropping a ball on a table. A value of zero means the ball won't bounce. This is called an inelastic collision. A value of one means the ball's velocity will be exactly reflected. This is called a perfectly elastic collision. The resulting restitution of 2 colliding shapes will be the maximum value.

See also Fixture::restitution.


sensor : bool

Set this property if you do not want the shape to react to collisions. This is useful if you only want to know that a collision happened, but not physically respond to it. The default value is based on the collisionTestingOnlyMode property - if it is set to true, the sensor flag also is set to true, otherwise to false.

See also Fixture::sensor.


vertices : list<variant>

Contains a list of 3 or more (up to 8) vertices that make up the shape in a counter clockwise winding. The vertices must define a convex polygon. See here for the definition of a convex polygon: http://en.wikipedia.org/wiki/Polygon#Convexity_and_types_of_non-convexity.

You can set the vertices like in this example:

 EntityBase {
   x: 30
   entityId: "trapezoid1"
   entityType: "trapezoidEntity"

   PolygonCollider {

     vertices: [
       Qt.point(-30, 0), // top left
       Qt.point(0, 100), // bottom left
       Qt.point(130, 100), // bottom right
       Qt.point(100, 0) // top right
     ]

   }
 }

The 0/0 position of the vertices is the top left point, thus when the entity is positioned at 30/0 the PolygonCollider physics shape will look like the following image:


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