Class GameObject

java.lang.Object
nl.saxion.gameapp.gameobject.GameObject
Direct Known Subclasses:
RotatableGameObject

public abstract class GameObject extends Object
Represents a generic game object in a 2D world.

Each game object has a position (x, y), a size (width, height), and can have multiple hitboxes of different shapes (rectangles or circles). Hitboxes are defined relative to the GameObject's position.

Subclasses must implement the render(float) method to define how the object is drawn on the screen.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected float
    The height of the object.
    protected final List<Hitbox>
    The list of hitboxes attached to this object.
    protected float
    The width of the object.
    protected float
    The x-coordinate of the object.
    protected float
    The y-coordinate of the object.
  • Constructor Summary

    Constructors
    Constructor
    Description
    GameObject(float x, float y, float width, float height)
    Creates a new game object with the specified position and size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addCircleHitbox(float relCenterX, float relCenterY, float relRadius)
    Adds a circular hitbox defined in relative percentages.
    void
    addHitbox(float relOffsetX, float relOffsetY, float relWidth, float relHeight)
    Adds a rectangular hitbox defined in relative percentages (0–1).
    void
    addHitbox(Hitbox hitbox)
    Adds a custom hitbox to this GameObject.
    void
    addPolygonHitbox(float[] relVertices)
    Adds a polygon hitbox defined in relative percentages.
    void
    centerOn(float centerX, float centerY)
    Centers the object on the specified coordinates.
    boolean
    Checks if this GameObject collides with another GameObject, considering all hitboxes.
    boolean
    contains(com.badlogic.gdx.math.Vector2 point)
    Checks if a given point is inside this GameObject's bounding rectangle.
    com.badlogic.gdx.math.Vector2
    Returns a normalized direction vector from this GameObject to another.
    float
    Calculates the Euclidean distance from this GameObject to another.
    void
    Draws all hitboxes of this GameObject using the given color.
    com.badlogic.gdx.math.Vector2
    Returns the bottom-left corner of the GameObject as a Vector2.
    com.badlogic.gdx.math.Vector2
    Returns the bottom-right corner of the GameObject as a Vector2.
    com.badlogic.gdx.math.Rectangle
    Returns the bounding rectangle of the GameObject.
    com.badlogic.gdx.math.Vector2
    Returns the center point of the GameObject.
    float
    Returns the center x-coordinate of the GameObject.
    float
    Returns the center y-coordinate of the GameObject.
    float
     
    Returns the list of all hitboxes attached to this GameObject.
    com.badlogic.gdx.math.Vector2
    Returns the top-left corner of the GameObject as a Vector2.
    com.badlogic.gdx.math.Vector2
    Returns the top-right corner of the GameObject as a Vector2.
    float
     
    float
     
    float
     
    void
    moveBy(float dx, float dy)
    Moves the object by the specified offsets.
    void
    moveTo(float newX, float newY)
    Moves the object to the specified coordinates.
    abstract void
    render(float delta)
    Renders the GameObject.
    void
    setHeight(float height)
    Sets the height of the object.
    void
    setWidth(float width)
    Sets the width of the object.
    void
    setX(float x)
    Sets the x-coordinate of the object.
    void
    setY(float y)
    Sets the y-coordinate of the object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • x

      protected float x
      The x-coordinate of the object.
    • y

      protected float y
      The y-coordinate of the object.
    • width

      protected float width
      The width of the object.
    • height

      protected float height
      The height of the object.
    • hitboxes

      protected final List<Hitbox> hitboxes
      The list of hitboxes attached to this object.
  • Constructor Details

    • GameObject

      public GameObject(float x, float y, float width, float height)
      Creates a new game object with the specified position and size.
      Parameters:
      x - initial x-coordinate
      y - initial y-coordinate
      width - width of the object
      height - height of the object
  • Method Details

    • addHitbox

      public void addHitbox(Hitbox hitbox)
      Adds a custom hitbox to this GameObject.
      Parameters:
      hitbox - the hitbox to add
    • addHitbox

      public void addHitbox(float relOffsetX, float relOffsetY, float relWidth, float relHeight)
      Adds a rectangular hitbox defined in relative percentages (0–1).
      Parameters:
      relOffsetX - x-offset as percentage of width
      relOffsetY - y-offset as percentage of height
      relWidth - width as percentage of width
      relHeight - height as percentage of height
    • addCircleHitbox

      public void addCircleHitbox(float relCenterX, float relCenterY, float relRadius)
      Adds a circular hitbox defined in relative percentages.
      Parameters:
      relCenterX - x-coordinate of center as percentage of width
      relCenterY - y-coordinate of center as percentage of height
      relRadius - radius as percentage of min(width, height)
    • addPolygonHitbox

      public void addPolygonHitbox(float[] relVertices)
      Adds a polygon hitbox defined in relative percentages.
      Parameters:
      relVertices - vertices as percentages of width/height (x1,y1,x2,y2,...)
    • getHitboxes

      public List<Hitbox> getHitboxes()
      Returns the list of all hitboxes attached to this GameObject.
      Returns:
      a list of hitboxes
    • collidesWith

      public boolean collidesWith(GameObject other)
      Checks if this GameObject collides with another GameObject, considering all hitboxes.
      Parameters:
      other - the other GameObject to check collision against
      Returns:
      true if any hitboxes overlap, false otherwise
    • render

      public abstract void render(float delta)
      Renders the GameObject.

      Subclasses must implement this method to define how the object is drawn to the screen.

      Parameters:
      delta - time elapsed since the last frame
    • drawHitboxes

      public void drawHitboxes(String color)
      Draws all hitboxes of this GameObject using the given color.
      Parameters:
      color - a string representing the color (e.g., "red-500")
    • getX

      public float getX()
      Returns:
      the x-coordinate of the object
    • getY

      public float getY()
      Returns:
      the y-coordinate of the object
    • getWidth

      public float getWidth()
      Returns:
      the width of the object
    • getHeight

      public float getHeight()
      Returns:
      the height of the object
    • setX

      public void setX(float x)
      Sets the x-coordinate of the object.
      Parameters:
      x - new x-coordinate
    • setY

      public void setY(float y)
      Sets the y-coordinate of the object.
      Parameters:
      y - new y-coordinate
    • setWidth

      public void setWidth(float width)
      Sets the width of the object.
      Parameters:
      width - new width
    • setHeight

      public void setHeight(float height)
      Sets the height of the object.
      Parameters:
      height - new height
    • moveBy

      public void moveBy(float dx, float dy)
      Moves the object by the specified offsets.
      Parameters:
      dx - offset in the x-direction
      dy - offset in the y-direction
    • moveTo

      public void moveTo(float newX, float newY)
      Moves the object to the specified coordinates.
      Parameters:
      newX - new x-coordinate
      newY - new y-coordinate
    • centerOn

      public void centerOn(float centerX, float centerY)
      Centers the object on the specified coordinates.
      Parameters:
      centerX - x-coordinate of the center
      centerY - y-coordinate of the center
    • getBounds

      public com.badlogic.gdx.math.Rectangle getBounds()
      Returns the bounding rectangle of the GameObject.
      Returns:
      rectangle representing the object's bounds
    • getCenterPoint

      public com.badlogic.gdx.math.Vector2 getCenterPoint()
      Returns the center point of the GameObject.
      Returns:
      center point as a Vector2
    • distanceTo

      public float distanceTo(GameObject other)
      Calculates the Euclidean distance from this GameObject to another.
      Parameters:
      other - the other GameObject
      Returns:
      the distance between the centers of the two objects
    • directionTo

      public com.badlogic.gdx.math.Vector2 directionTo(GameObject other)
      Returns a normalized direction vector from this GameObject to another.
      Parameters:
      other - the target GameObject
      Returns:
      a unit vector pointing from this object to the other
    • getCenterX

      public float getCenterX()
      Returns the center x-coordinate of the GameObject.
      Returns:
      x-coordinate of the center
    • getCenterY

      public float getCenterY()
      Returns the center y-coordinate of the GameObject.
      Returns:
      y-coordinate of the center
    • getBottomLeft

      public com.badlogic.gdx.math.Vector2 getBottomLeft()
      Returns the bottom-left corner of the GameObject as a Vector2.
      Returns:
      bottom-left corner position
    • getBottomRight

      public com.badlogic.gdx.math.Vector2 getBottomRight()
      Returns the bottom-right corner of the GameObject as a Vector2.
      Returns:
      bottom-right corner position
    • getTopLeft

      public com.badlogic.gdx.math.Vector2 getTopLeft()
      Returns the top-left corner of the GameObject as a Vector2.
      Returns:
      top-left corner position
    • getTopRight

      public com.badlogic.gdx.math.Vector2 getTopRight()
      Returns the top-right corner of the GameObject as a Vector2.
      Returns:
      top-right corner position
    • contains

      public boolean contains(com.badlogic.gdx.math.Vector2 point)
      Checks if a given point is inside this GameObject's bounding rectangle.
      Parameters:
      point - the point to test
      Returns:
      true if the point is inside, false otherwise