Circle Collider2D Component

The CircleCollider2D class is a component in Tsar engine used to define a circle outline to a 2D rigidbody.
There are two types of colliders: solid and sensor.

  • Solid -> default collider which provides collision and other bodies with solid colliders cannot pass through it. IsSensor should be false.
  • Sensor -> collider which doesn't provide collision and other colliders pass through (overlap) it. IsSensor should be true.

Below is an image showing CircleCollider2D component from Debug rendering inside Tsar. You can change collider color in Editor from Settings->Physics2D->Gizmo.


Class Declaration

class CircleCollider2D : public Component

This class inherits from the Component class and adds functionality related to defining shape outline of a Rigidbody for simulating 2D physics.


Public Methods

Radius

CircleCollider2D stores the radius in meters and when a collider is created the X-scale from Transform is normally applied. Radius of 2.0 and X-scale of 3.0 will result in circle with radius of 6.0 and diameter of 12.0.


float GetRadius(bool applyTransformScale = true)

Returns the radius of collider from component.

  • applyTransformScale - should the returned radius from component have applied scale, default is true.

void SetRadius(float radius, bool applyTransformScale = true)

Change radius of shape. May have overhead if not called right after initialization/construction.

  • radius - Sets component data regardless of applyTransform.
  • applyTransformScale - true(applies scale on X), false(uses collider size only).

Offset

Offset is the local offset (relative to entity itself) that can be used to precisely adjust collider position on the body.
glm::vec3 Offset uses X and Y for physics and Z is used only for Debug Rendering.
float OffsetRot is local angle offset along Z-axis in degrees. Final rotation is: Rotation Z from Transform + OffsetRot


glm::vec3 GetOffset()
Returns shape local offset.

void SetOffset(const glm::vec3& offset)
Set local offset position of collider, X and Y matter for physics.


float GetOffsetRot()
Returns angle offset along Z-axis in degrees.

void SetOffsetRot(float offsetRot)
Set local angle offset of collider along Z-axis in degrees.


Properties

The CircleCollider2D class exposes properties (that can also be accessed via getter and setter functions). These properties allow users to manipulate the shape properties of the object in a more intuitive manner:


float Density Density of the shape, in kg/m².

  • Get: GetDensity()
  • Set: SetDensity(float density, bool updateMass = true)
    • density - desired density in kg/m².
    • updateMass - should the body mass data be updated with this shape?

float Restitution From Physics Material. Energy conservation coefficient. 1 means perfect bounce with no energy loss, 0 means perfect absorbtion with no bounce.

  • Get: GetRestitution()
  • Set: SetRestitution(float restitution)
    • restitution - should be value between [0; 1].

float Friction From Physics Material. Friction coefficient, 0 means no friction(slippery, like ice), 1 means high friction(grippy, like rubber), values >1 are allowed.

  • Get: GetFriction()
  • Set: SetFriction(float friction)
    • friction - should be non-negative value.

bool IsSensor Is the shape sensor or not(solid)? true means no collision, false means solid collider providing collision.
Default is false.

  • Get: GetIsSensor()
  • Set: SetIsSensor(bool isTrigger)
    • isTrigger - true makes shape like ghost/hollow detector, false makes shape solid.

bool EnableCollisionEvents Should collision events be registered from this shape and trigger Enter/Exit events in scripting.
Requires IsSensor=false. This itself doesn't affect collisions.

  • Get: GetEnableCollisionEvents()
  • Set: SetEnableCollisionEvents(bool enableCollisionEvents)
    • enableCollisionEvents - true enables event registration, false disables event registration.

bool EnableHitEvents Should hit events be registered from this shape and trigger Enter/Exit events in scripting.
Requires IsSensor=false and EnableCollisionEvents=true to work. This itself doesn't affect collisions.

  • Get: GetEnableHitEvents()
  • Set: SetEnableHitEvents(bool enableHitEvents)
    • enableHitEvents - true enables event registration, false disables event registration.

bool EnableSensorEvents Should sensor/overlap events be registered from this shape and trigger Enter/Exit events in scripting.
Requires IsSensor=true. This itself doesn't affect collisions.

  • Get: GetEnableSensorEvents()
  • Set: SetEnableSensorEvents(bool enableSensorEvents)
    • enableSensorEvents - true enables event registration, false disables event registration.