Class ColliderComponent

java.lang.Object
com.deco2800.game.components.Component
com.deco2800.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

  • 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 void setAsBox​(Vector2 size)
      Set physics as a box with a given size. Box is centered around the entity.
      Parameters:
      size - size of the box
    • setAsBoxAligned

      public void setAsBoxAligned​(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
    • setAsBox

      public void setAsBox​(Vector2 size, 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.
    • setFriction

      public void 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
    • setSensor

      public void 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.
    • setDensity

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

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

      public ColliderComponent setShape​(Shape shape)
      Set shape
      Parameters:
      shape - shape, default = bounding box the same size as the entity
      Returns:
      self
    • getFixture

      public 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
    • setMaskBits

      public void setMaskBits​(short layerMask)
      Set the collider maskBits, used in collision logic
      Parameters:
      layerMask - maskBits of 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