Skip to main content

FlixelObject

View source

class

org.flixelgdx.FlixelObject

public class FlixelObject extends FlixelBasic implements FlixelDebugDrawable, FlixelPositional, FlixelShakeable

The base class for all visual/spatial objects in Flixel. Extends FlixelBasic with position (FlixelObject.x, FlixelObject.y), dimensions (FlixelObject.width, FlixelObject.height), rotation (FlixelObject.angle), and a full kinematic physics model (velocity, acceleration, drag, collision flags).

Most games interact with this through FlixelSprite, which adds graphical capabilities on top of this spatial foundation.

Collision

Use Flixel.overlap() and Flixel.collide() for overlap/separation checks. The static FlixelObject.separate(FlixelObject, FlixelObject) method resolves overlaps by adjusting positions and velocities.

Constructors

FlixelObject()

public FlixelObject()

Creates a new FlixelObject with the default position (0, 0) and size (0, 0).


FlixelObject(float, float)

public FlixelObject(float x, float y)

Creates a new FlixelObject with the given position and size (0, 0).

Parameters:

NameDescription
xThe x position.
yThe y position.

FlixelObject(float, float, float, float)

public FlixelObject(float x, float y, float width, float height)

Creates a new FlixelObject with the given position and size.

Parameters:

NameDescription
xThe x position.
yThe y position.
widthThe width.
heightThe height.

Fields

SEPARATE_BIAS

public static final float SEPARATE_BIAS = 4.0f

Maximum number of pixels two objects can intersect before FlixelObject.separate(FlixelObject, FlixelObject) gives up when using overlap checks.


lastX

protected float lastX

X position at the start of the current frame, before motion.


lastY

protected float lastY

Y position at the start of the current frame, before motion.


velocityX

protected float velocityX

Horizontal velocity in pixels per second.


velocityY

protected float velocityY

Vertical velocity in pixels per second.


accelerationX

protected float accelerationX

Horizontal acceleration in pixels per second squared.


accelerationY

protected float accelerationY

Vertical acceleration in pixels per second squared.


dragX

protected float dragX

Deceleration applied when FlixelObject.accelerationX is zero. Only applied when greater than 0.


dragY

protected float dragY

Deceleration applied when FlixelObject.accelerationY is zero. Only applied when greater than 0.


maxVelocityX

protected float maxVelocityX

Maximum absolute horizontal velocity.


maxVelocityY

protected float maxVelocityY

Maximum absolute vertical velocity.


angularVelocity

protected float angularVelocity

Rotational speed in degrees per second.


angularAcceleration

protected float angularAcceleration

Rotational acceleration in degrees per second squared.


angularDrag

protected float angularDrag

Rotational drag in degrees per second squared.


maxAngularVelocity

protected float maxAngularVelocity

Maximum angular velocity in degrees per second.


allowCollisions

protected int allowCollisions

Bit field of direction flags indicating which sides allow collision. Use FlixelDirectionFlags.ANY (default) for full collision, FlixelDirectionFlags.NONE for no collision, or combine individual flags.

See Also: FlixelDirectionFlags


touching

protected int touching

Bit field indicating which surfaces this object is currently touching. Set by FlixelObject.separate(FlixelObject, FlixelObject) and reset at the start of each FlixelObject.update(float).


wasTouching

protected int wasTouching

Copy of FlixelObject.touching from the previous frame, useful for detecting the moment an object lands (FlixelObject.justTouched(int)).


elasticity

protected float elasticity

Bounciness factor used during collision resolution. 0 means no bounce (default), 1 means fully elastic.


mass

protected float mass

Virtual mass used during elastic collision resolution. Default is 1. Only matters when both colliding objects have elasticity > 0.


scrollX

protected float scrollX

Parallax scroll factor on X (1 = moves with the camera like normal world objects).


scrollY

protected float scrollY

Parallax scroll factor on Y (1 = moves with the camera like normal world objects).


debugColorSolid

protected final float[] debugColorSolid

Color when allowCollisions == ANY and !immovable (solid). Default: red.


debugColorImmovable

protected final float[] debugColorImmovable

Color when immovable is true or allowCollisions is partial. Default: green.


debugColorNoCollision

protected final float[] debugColorNoCollision

Color when allowCollisions == NONE. Default: blue.


debugColorOverride

protected float[] debugColorOverride

If non-null, forces the debug bounding box to this color regardless of collision state.


moves

protected boolean moves

When true, FlixelObject.updateMotion(float) runs each frame.


immovable

protected boolean immovable

When true, this object will not be moved by collision resolution. Other objects will still be pushed away from it.


Methods

update(float)

public void update(float elapsed)

Updates this object's collision bookkeeping and, when FlixelObject.moves is true, calls FlixelObject.updateMotion(float).

Parameters:

NameDescription
elapsedSeconds since the last frame.

destroy()

public void destroy()

getScrollX()

public float getScrollX()

Parallax scroll factor on X (1 means the object moves with the camera like a normal world object).


getScrollY()

public float getScrollY()

Parallax scroll factor on Y (1 means the object moves with the camera like a normal world object).


setScrollFactor(float, float)

public void setScrollFactor(float scrollX, float scrollY)

Sets FlixelObject.getScrollX() and FlixelObject.getScrollY().


getX()

public float getX()

X position of the upper left corner of this object in world space.


setX(float)

public void setX(float x)

Sets world X.


getY()

public float getY()

Y position of the upper left corner of this object in world space.


setY(float)

public void setY(float y)

Sets world Y.


getWidth()

public float getWidth()

Width of this object's hitbox.


setWidth(float)

public void setWidth(float width)

Sets hitbox width.


getHeight()

public float getHeight()

Height of this object's hitbox.


setHeight(float)

public void setHeight(float height)

Sets hitbox height.


getAngle()

public float getAngle()

The angle in degrees of this object. Does not affect axis-aligned collision on FlixelObject.


setAngle(float)

public void setAngle(float angle)

Sets the rotation angle in degrees.


getLastX()

public float getLastX()

X position at the start of the current frame, before motion.


getLastY()

public float getLastY()

Y position at the start of the current frame, before motion.


setPosition(float, float)

public void setPosition(float x, float y)

Helper that sets both world coordinates.


setSize(float, float)

public void setSize(float width, float height)

Shortcut for setting both FlixelObject.getWidth() and FlixelObject.getHeight().


changeX(float)

public void changeX(float dx)

Adds dx to the current X position.


changeY(float)

public void changeY(float dy)

Adds dy to the current Y position.


changeAngle(float)

public void changeAngle(float deltaDegrees)

getMidpointX()

public float getMidpointX()

Returns the center X coordinate of this object.


getMidpointY()

public float getMidpointY()

Returns the center Y coordinate of this object.


getVelocityX()

public float getVelocityX()

Horizontal velocity in pixels per second.


setVelocityX(float)

public void setVelocityX(float velocityX)

Sets horizontal velocity in pixels per second.


getVelocityY()

public float getVelocityY()

Vertical velocity in pixels per second.


setVelocityY(float)

public void setVelocityY(float velocityY)

Sets vertical velocity in pixels per second.


setVelocity(float, float)

public void setVelocity(float vx, float vy)

Sets both velocity components.


getAccelerationX()

public float getAccelerationX()

Horizontal acceleration in pixels per second squared.


setAccelerationX(float)

public void setAccelerationX(float ax)

Sets horizontal acceleration in pixels per second squared.


getAccelerationY()

public float getAccelerationY()

Vertical acceleration in pixels per second squared.


setAccelerationY(float)

public void setAccelerationY(float ay)

Sets vertical acceleration in pixels per second squared.


setAcceleration(float, float)

public void setAcceleration(float ax, float ay)

Sets both acceleration components.


getDragX()

public float getDragX()

Deceleration applied when FlixelObject.getAccelerationX() is zero. Only applied when greater than 0.


setDragX(float)

public void setDragX(float dx)

Sets horizontal drag.


getDragY()

public float getDragY()

Deceleration applied when FlixelObject.getAccelerationY() is zero. Only applied when greater than 0.


setDragY(float)

public void setDragY(float dy)

Sets vertical drag.


setDrag(float, float)

public void setDrag(float dx, float dy)

Sets both drag components.


getMaxVelocityX()

public float getMaxVelocityX()

Maximum absolute horizontal velocity.


setMaxVelocityX(float)

public void setMaxVelocityX(float mvx)

Sets maximum absolute horizontal velocity.


getMaxVelocityY()

public float getMaxVelocityY()

Maximum absolute vertical velocity.


setMaxVelocityY(float)

public void setMaxVelocityY(float mvy)

Sets maximum absolute vertical velocity.


setMaxVelocity(float, float)

public void setMaxVelocity(float mvx, float mvy)

Sets both max velocity components.


getAngularVelocity()

public float getAngularVelocity()

Rotational speed in degrees per second.


setAngularVelocity(float)

public void setAngularVelocity(float av)

Sets rotational speed in degrees per second.


getAngularAcceleration()

public float getAngularAcceleration()

Rotational acceleration in degrees per second squared.


setAngularAcceleration(float)

public void setAngularAcceleration(float aa)

Sets rotational acceleration in degrees per second squared.


getAngularDrag()

public float getAngularDrag()

Rotational drag in degrees per second squared.


setAngularDrag(float)

public void setAngularDrag(float ad)

Sets rotational drag in degrees per second squared.


getMaxAngularVelocity()

public float getMaxAngularVelocity()

Maximum angular velocity in degrees per second.


setMaxAngularVelocity(float)

public void setMaxAngularVelocity(float mav)

Sets maximum angular velocity in degrees per second.


getMoves()

public boolean getMoves()

When true, FlixelObject.updateMotion(float) runs each frame on FlixelObject.


setMoves(boolean)

public void setMoves(boolean moves)

Enables or disables automatic motion integration on FlixelObject.


getAllowCollisions()

public int getAllowCollisions()

setAllowCollisions(int)

public void setAllowCollisions(int flags)

isSolid()

public boolean isSolid()

Convenience accessor, returns true when allowCollisions is not FlixelDirectionFlags.NONE.


getSolid()

public boolean getSolid()

Returns true when allowCollisions is not FlixelDirectionFlags.NONE.


setSolid(boolean)

public void setSolid(boolean solid)

Sets FlixelObject.allowCollisions to ANY when solid is true, or NONE when false.

Parameters:

NameDescription
solidWhether the object should be solid.

getTouching()

public int getTouching()

getWasTouching()

public int getWasTouching()

isImmovable()

public boolean isImmovable()

When true, this object will not be moved by collision resolution. Other objects will still be pushed away from it.


getImmovable()

public boolean getImmovable()

Returns whether this object is immovable during collision resolution.


setImmovable(boolean)

public void setImmovable(boolean immovable)

Sets whether collision resolution may move this object.


getShakeX()

public float getShakeX()

Current shake channel X (for example graphic offset or world X).


getShakeY()

public float getShakeY()

Current shake channel Y.


setShake(float, float)

public void setShake(float x, float y)

Sets the shake channel to absolute coordinates (typically base plus jitter).


getShakeWidth()

public float getShakeWidth()

Width used to scale FlixelShakeUnit.FRACTION on the X axis. Values 0 or less mean the tween treats intensity like plain pixels on that axis.


getShakeHeight()

public float getShakeHeight()

Height used to scale FlixelShakeUnit.FRACTION on the Y axis. Values 0 or less mean the tween treats intensity like plain pixels on that axis.


getElasticity()

public float getElasticity()

setElasticity(float)

public void setElasticity(float elasticity)

getMass()

public float getMass()

setMass(float)

public void setMass(float mass)

isTouching(int)

public boolean isTouching(int direction)

Returns true if this object is currently touching the given surface(s). Flags are set by Flixel.collide(...) and reset each frame.

Parameters:

NameDescription
directionOne or more direction flags ORed together.

justTouched(int)

public boolean justTouched(int direction)

Returns true if this object just started touching the given surface(s) this frame (was not touching last frame).

Parameters:

NameDescription
directionOne or more direction flags ORed together.

reset(float, float)

public void reset(float x, float y)

Resets this object to a new position, reviving it and clearing physics state.

Parameters:

NameDescription
xNew X position.
yNew Y position.

overlaps(FlixelObject)

public boolean overlaps(FlixelObject other)

Checks if this object's AABB overlaps another FlixelObject.

Parameters:

NameDescription
otherThe other object.

Returns: true if the two AABBs overlap.


overlapsPoint(float, float)

public boolean overlapsPoint(float px, float py)

Checks if a point in world space overlaps this object's AABB.

Parameters:

NameDescription
pxX coordinate.
pyY coordinate.

Returns: true if the point is inside.


inWorldBounds()

public boolean inWorldBounds()

Returns true if the object is within the world bounds configured on the global manager class.


updateMotion(float)

public void updateMotion(float elapsed)

Internal function for updating the position and speed of this object using a velocity-verlet style integration.

Parameters:

NameDescription
elapsedSeconds elapsed since the last frame.

computeVelocity(float, float, float, float, float)

public float computeVelocity(float velocity, float acceleration, float drag, float max, float elapsed)

Internal function for computing velocity after acceleration and drag.

Parameters:

NameDescription
velocityCurrent velocity.
accelerationAcceleration to apply.
dragDrag to apply when acceleration is zero.
maxMaximum allowed velocity.
elapsedSeconds elapsed since the last frame.

Returns: Updated velocity.


separate(FlixelObject, FlixelObject)

public static boolean separate(FlixelObject object1, FlixelObject object2)

Separates two overlapping objects by adjusting positions and velocities. Calls FlixelObject.separateX(FlixelObject, FlixelObject) then FlixelObject.separateY(FlixelObject, FlixelObject).

Parameters:

NameDescription
object1First object.
object2Second object.

Returns: true if the objects were overlapping and were separated.


separateX(FlixelObject, FlixelObject)

public static boolean separateX(FlixelObject object1, FlixelObject object2)

Separates two overlapping objects on the X axis.

Returns: true if the objects overlapped and were separated on X.


separateY(FlixelObject, FlixelObject)

public static boolean separateY(FlixelObject object1, FlixelObject object2)

Separates two overlapping objects on the Y axis.

Returns: true if the objects overlapped and were separated on Y.


computeOverlapX(FlixelObject, FlixelObject, boolean)

public static float computeOverlapX(FlixelObject object1, FlixelObject object2, boolean checkMaxOverlap)

Computes the overlap between two objects on the X axis and updates their FlixelObject.touching flags. Returns 0 if they do not overlap or if their allowCollisions flags disallow collision on the overlapping sides.

Parameters:

NameDescription
object1First object.
object2Second object.
checkMaxOverlapWhether to reject overlaps greater than combined movement + SEPARATE_BIAS.

Returns: Signed overlap in pixels (positive means object1's right side penetrates object2's left side).


computeOverlapY(FlixelObject, FlixelObject, boolean)

public static float computeOverlapY(FlixelObject object1, FlixelObject object2, boolean checkMaxOverlap)

Computes the overlap between two objects on the Y axis and updates their FlixelObject.touching flags. Same semantics as FlixelObject.computeOverlapX(...).

Parameters:

NameDescription
object1First object.
object2Second object.
checkMaxOverlapWhether to reject overlaps greater than combined movement + SEPARATE_BIAS.

Returns: Signed overlap in pixels (positive means object1's bottom side penetrates object2's top side).


updateTouchingFlags(FlixelObject, FlixelObject)

public static boolean updateTouchingFlags(FlixelObject object1, FlixelObject object2)

Checks if two objects overlap and updates their FlixelObject.touching flags without separating them.

Parameters:

NameDescription
object1First object.
object2Second object.

Returns: true if the two objects are touching.


getDebugX()

public float getDebugX()

getDebugY()

public float getDebugY()

getDebugDrawX(FlixelCamera)

public float getDebugDrawX(FlixelCamera cam)

getDebugDrawY(FlixelCamera)

public float getDebugDrawY(FlixelCamera cam)

getDebugWidth()

public float getDebugWidth()

getDebugHeight()

public float getDebugHeight()

getDebugBoundingBoxColor()

public float[] getDebugBoundingBoxColor()

Returns the appropriate debug color based on collision state:

  • Override color if set
  • Blue when allowCollisions == NONE
  • Green when immovable or partial collision
  • Red (normal/solid) otherwise

setDebugBoundingBoxColor(float[])

public void setDebugBoundingBoxColor(float[] rgba)

Forces a specific debug bounding box color, overriding the automatic selection. Pass null to re-enable automatic color selection.

Parameters:

NameDescription
rgba4-element float array [r, g, b, a], or null.

toString()

public String toString()