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)
-
Field Summary
-
Constructor Summary
Constructors Constructor Description ColliderComponent()
-
Method Summary
Modifier and Type Method Description void
create()
Called when the entity is created and registered.void
dispose()
Called when the component is disposed.Fixture
getFixture()
short
getLayer()
void
setAsBox(Vector2 size)
Set physics as a box with a given size.void
setAsBox(Vector2 size, Vector2 position)
Set physics as a box with a given size and local position.void
setAsBoxAligned(Vector2 size, PhysicsComponent.AlignX alignX, PhysicsComponent.AlignY alignY)
Set physics as a box with a given size.void
setDensity(float density)
Set densityvoid
setFriction(float friction)
Set friction.ColliderComponent
setLayer(short layerMask)
Set the collider layer, used in collision logicvoid
setMaskBits(short layerMask)
Set the collider maskBits, used in collision logicvoid
setRestitution(float restitution)
Set restitutionvoid
setSensor(boolean isSensor)
Set whether this physics component is a sensor.ColliderComponent
setShape(Shape shape)
Set shapeMethods inherited from class com.deco2800.game.components.Component
earlyUpdate, getEntity, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update
-
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. -
setAsBox
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 boxalignX
- how to align x relative to entityalignY
- how to align y relative to entity
-
setAsBox
Set physics as a box with a given size and local position. Box is centered around the position.- Parameters:
size
- size of the boxposition
- 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
Set shape- Parameters:
shape
- shape, default = bounding box the same size as the entity- Returns:
- self
-
getFixture
- Returns:
- Physics fixture of this collider. Null before created()
-
setLayer
Set the collider layer, used in collision logic- Parameters:
layerMask
- Bitmask ofPhysicsLayer
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 ofPhysicsLayer
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.
-