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

CircleCollider

The CircleCollider is a physics body with a round shape. More...

Import Statement: import Felgo 4.0
Inherits:

ColliderBase

Properties

Detailed Description

You can set the radius property to match the visual representation of the entity and define its physics properties for simulating physics behavior. If you only want to use the physics system for collision detection, set the ColliderBase::collisionTestingOnlyMode property to true. This component is derived from ColliderBase. See the ColliderBase documentation for a list and description of its properties that are also available for CircleCollider.

The origin, i.e. the 0/0 position of the CircleCollider, is the top left corner of the circle's bounding box. If you want to move the circle center position, you can modify the x and y position offset. With the following example, the circle would have its center at 30/30 and not at 20/20:

 CircleCollider {
   radius: 20
   x: 10
   y: 10
 }

This means, a CircleCollider should always be placed inside an entity whose width and height are twice the radius. If that is not the case, x and y properties need to be adapted manually.

Note: A CircleCollider does NOT have a width and height, but only a radius. That means, it can not be positioned using Item::anchors. Always provide a radius, and modify x and y properties if needed.

Example for Physics Used Only for Collision Detection

In the following example, the CircleCollider is only used for collision detection. That means, the physics properties are ignored. This is useful for games where the CircleCollider should not have a gravity, and the position of the entity is set from non-physics components like MoveToPointHelper or NumberAnimation. In the example, a collision is detected with the Fixture::onBeginContact signal handler and the entity is moved from the initial x position 0 to 100 within 2 seconds.

 import Felgo
 import QtQuick
 GameWindow {

   PhysicsWorld {
     // set no gravity, the collider is not physics-based
   }

   Scene {

      EntityBase {
        //make entity the same size as its representation
        //the image should be a circular image with squared dimensions (same width and height)
        width: circleImage.width
        height: circleImage.height

        entityId: "circle1"
        entityType: "circle"

        Image {
            id: circleImage
            source: "../assets/img/circle.png"
        }

        CircleCollider {
          id: circleCollider

          // the CircleCollider will not be affected by gravity or other applied physics forces
          collisionTestingOnlyMode: true

          // approximate the collider with the image size - if the image is circular, this is a good approximation
          radius: circleImage.width/2

          fixture.onBeginContact: {
             // handle the collision and make the image semi-transparent
             circleImage.opacity = 0.5
          }
        }

        // moves the entity to x position 100 within 2 seconds
        NumberAnimation on x {
          to: 100
          duration: 2000
        }
      }
    }
 }

Example for Physics-Driven Entity Positioning

In this example, the circle1 entity falls down and moves to the right, based on physics properties like the world gravity, the density, friction and linearVelocity of the entity and will bounce off other entities based on the restitution setting.

 import Felgo

 import QtQuick

 GameWindow {

   PhysicsWorld {
     // set to world gravity
     gravity.y: -9.81
   }

   Scene {

      EntityBase {
        //make entity the same size as its representation
        //the image should be a circular image with squared dimensions (same width and height)
        width: circleImage.width
        height: circleImage.height

        entityId: "circle1"
        entityType: "circle"

        Image {
            id: circleImage
            source: "../assets/img/circle.png"
        }
        CircleCollider {

           // these are the default physics property values, but they can be changed to match the desired physics behavior
           friction: 0.2
           restitution: 0

           bodyType: Body.Dynamic
           bullet: false
           angularDamping: 0
           linearDamping: 0
           fixedRotation: false

           // initially, move to the right as linearVelocity.x is set to 100
           linearVelocity: Qt.point(100, 0)

           // approximate the collider with the image size - if the image is circular, this is a good approximation
           radius: circleImage.width/2

           fixture.onBeginContact: {
              // handle the collision and make the image semi-transparent
              circleImage.opacity = 0.5
           }
        }
      }
    }
 }

For more information about collision handlers, physics movement see the BoxCollider documentation.

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"

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

   EntityBase {
     entityType: "enemy"

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

   EntityBase {
     entityType: "powerup"

     CircleCollider {
       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 : alias

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.


radius : real

This property holds the radius of the CircleCollider. If you use a CircleCollider, this property must be set! The default value is 0.

You can set it to half of the width of an Image to approximate the physics shape from the Image like in this example:

 EntityBase {
  entityId: "circle1"
  entityType: "circle"

  Image {
      id: circleImage
      source: "../assets/img/circle.png"
  }
  CircleCollider {

    // approximate the collider with the image size - if the image is circular, this is a good approximation
    radius: circleImage.width/2

  }
 }

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.


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded