Class ColliderComponent

java.lang.Object
com.csse3200.game.components.Component
com.csse3200.game.physics.components.ColliderComponent
Direct Known Subclasses:
HitboxComponent

public class ColliderComponent extends Component
Attaches a physics collider to an entity. By default, this is a rectangle the same size as the entity's scale. This allows an entity to collide with other physics objects, or detect collisions without interaction (if sensor = true)
  • Constructor Details

    • ColliderComponent

      public ColliderComponent()
  • Method Details

    • create

      public void create()
      Description copied from class: Component
      Called when the entity is created and registered. Initial logic such as calls to GetComponent should be made here, not in the constructor which is called before an entity is finished.
      Overrides:
      create in class Component
    • setAsBox

      public ColliderComponent setAsBox(com.badlogic.gdx.math.Vector2 size)
      Set physics as a box with a given size. Box is centered around the entity.
      Parameters:
      size - size of the box
      Returns:
      self
    • setAsBoxAligned

      public ColliderComponent setAsBoxAligned(com.badlogic.gdx.math.Vector2 size, PhysicsComponent.AlignX alignX, PhysicsComponent.AlignY alignY)
      Set physics as a box with a given size. Box is aligned based on alignment.
      Parameters:
      size - size of the box
      alignX - how to align x relative to entity
      alignY - how to align y relative to entity
      Returns:
      self
    • setAsBox

      public ColliderComponent setAsBox(com.badlogic.gdx.math.Vector2 size, com.badlogic.gdx.math.Vector2 position)
      Set physics as a box with a given size and local position. Box is centered around the position.
      Parameters:
      size - size of the box
      position - position of the box center relative to the entity.
      Returns:
      self
    • setFriction

      public ColliderComponent setFriction(float friction)
      Set friction. This affects the object when touching other objects, but does not affect friction with the ground.
      Parameters:
      friction - friction, default = 0
      Returns:
      self
    • setSensor

      public ColliderComponent setSensor(boolean isSensor)
      Set whether this physics component is a sensor. Sensors don't collide with other objects but still trigger collision events. See: https://www.iforce2d.net/b2dtut/sensors
      Parameters:
      isSensor - true if sensor, false if not. default = false.
      Returns:
      self
    • setDensity

      public ColliderComponent setDensity(float density)
      Set density
      Parameters:
      density - Density and size of the physics component determine the object's mass. default = 0
      Returns:
      self
    • setRestitution

      public ColliderComponent setRestitution(float restitution)
      Set restitution
      Parameters:
      restitution - restitution is the 'bounciness' of an object, default = 0
      Returns:
      self
    • setShape

      public ColliderComponent setShape(com.badlogic.gdx.physics.box2d.Shape shape)
      Set shape
      Parameters:
      shape - shape, default = bounding box the same size as the entity
      Returns:
      self
    • getFixture

      public com.badlogic.gdx.physics.box2d.Fixture getFixture()
      Returns:
      Physics fixture of this collider. Null before created()
    • setLayer

      public ColliderComponent setLayer(short layerMask)
      Set the collider layer, used in collision logic
      Parameters:
      layerMask - Bitmask of PhysicsLayer this collider belongs to
      Returns:
      self
    • getLayer

      public short getLayer()
      Returns:
      The PhysicsLayer this collider belongs to
    • dispose

      public void dispose()
      Description copied from class: Component
      Called when the component is disposed. Dispose of any internal resources here.
      Overrides:
      dispose in class Component