Skip to content

GameObject

⚠️ Experimental feature

The GameObject system, including RotatableGameObject and all hitbox functionality, is new and experimental. It has not been thoroughly tested in all situations. Use it with caution, especially for complex collisions or large numbers of objects. Some behaviors may change in future updates.

⚠️ Advanced Topic

This section is intended for students who are comfortable with the basics of object-oriented programming. You should already know how to define classes, create objects and work with object inheritance.

In almost every game, we have objects that can move, interact, or collide with other objects. These can be players, enemies, bullets, platforms, or even invisible triggers. To make our code organized and easy to manage, we often create a base class that handles common features for all objects.

The GameObject class is exactly that. It is a generic 2D object in the game world. Every object that extends GameObject will have:

  • A position in the world (x and y).
  • A size (width and height).
  • A set of hitboxes that define its collision area.
  • Useful helper methods to move, center, and interact with other objects.

By using GameObject as a base class, we can create more complex objects without repeating the same code over and over. For example, both a player and an enemy can have hitboxes, move around the screen, and detect collisions, but each can have its own unique behavior or appearance.

This class is part of a flexible system that allows you to:

  • Define multiple hitboxes per object.
  • Use rectangles, circles, or polygons as hitboxes.
  • Define hitboxes relative to the object’s size, so they automatically scale if the object changes size.
  • Check collisions easily between objects.
  • Draw hitboxes for debugging and visualization.

Using GameObject as a foundation helps keep your game code clean, modular, and easier to maintain.

Position and Size

Each GameObject has:

  • x and y: The position of the object (bottom-left corner).
  • width and height: The size of the object.

You can change or move a GameObject:

// Move the object by 10 units to the right and 5 units up
obj.moveBy(10, 5);

// Move the object to a specific position
obj.moveTo(50, 100);

// Center the object at a point
obj.centerOn(200, 300);

// Change width and height
obj.setWidth(50);
obj.setHeight(80);

You can also get the position of the corners or center easily:

Vector2 center = obj.getCenterPoint();
Vector2 bottomLeft = obj.getBottomLeft();
Vector2 topRight = obj.getTopRight();

Hitboxes

A hitbox is an area used for collisions. GameObject supports:

  • Rectangle hitboxes
  • Circle hitboxes
  • Polygon hitboxes (for more complex shapes)

Hitboxes are relative to the object's position and can be defined using percentages of the object’s size.

Adding Hitboxes

// Add a rectangle hitbox (relative coordinates)
obj.addHitbox(0.1f, 0.2f, 0.5f, 0.4f); // x, y, width, height as percentages

// Add a circular hitbox
obj.addCircleHitbox(0.5f, 0.5f, 0.2f); // centerX, centerY, radius as percentages

// Add a polygon hitbox
float[] vertices = {0.1f,0.1f, 0.9f,0.1f, 0.5f,0.8f}; // percentages
obj.addPolygonHitbox(vertices);

Checking Collisions

if (obj1.collidesWith(obj2)) {
    System.out.println("Objects are touching!");
}

Rendering

The GameObject class doesn’t know how to draw itself. You must implement the abstract render(float delta) method in your subclass.

You can draw hitboxes for debugging:

obj.drawHitboxes("red-500"); // Draws all hitboxes in red

Utility Methods

The class provides helpful methods for working with other objects:

// Distance to another object
float distance = obj.distanceTo(otherObj);

// Direction to another object
Vector2 direction = obj.directionTo(otherObj);

// Check if a point is inside the object
boolean inside = obj.contains(new Vector2(100, 200));

Rotatable Game Object

Sometimes, game objects need to rotate. For example:

  • A spinning enemy turret
  • A rotating saw blade
  • A player that can turn in any direction

In LibGDX, rotated rectangles cannot be used for collision detection directly. That’s why we use polygon hitboxes instead — polygons can rotate and still be checked for collisions.

The RotatableGameObject class works almost the same as GameObject, with a few key differences:

  • All rectangular hitboxes are automatically converted into polygons when added.
  • You can set a rotation angle for the object.
  • Polygon hitboxes are updated automatically to match the rotation.
  • Circle and polygon hitboxes still work as usual.

Adding Hitboxes

Even though rectangles are converted to polygons internally, the way you add hitboxes is similar:

RotatableGameObject obj = new RotatableGameObject(100, 50, 60, 40);

// Add a rectangle hitbox (will become a polygon automatically)
obj.addHitbox(0.1f, 0.1f, 0.8f, 0.8f);

// Add a circle hitbox (works the same)
obj.addCircleHitbox(0.5f, 0.5f, 0.3f);

// Add a polygon hitbox (already a polygon, just works)
float[] vertices = {0.1f,0.1f, 0.9f,0.1f, 0.5f,0.9f};
obj.addPolygonHitbox(vertices);

Setting Rotation

You can rotate the object at any time:

// Rotate 45 degrees clockwise
obj.setRotation(45);

// Rotate another 30 degrees
obj.setRotation(75);

The rotation is applied to all polygon hitboxes, so collision detection works correctly even for rotated shapes.

Example: using the GameObject

import nl.saxion.gameapp.gameobject.GameObject;

public class Player extends GameObject {

    public Player(float x, float y, float width, float height) {
        super(x, y, width, height);

        // Add a rectangle hitbox (covers most of the player)
        addHitbox(0.1f, 0.1f, 0.8f, 0.8f);

        // Add a circular hitbox (for example, for head collision)
        addCircleHitbox(0.5f, 0.9f, 0.1f);
    }

    @Override
    public void render(float delta) {
        // Draw the player as a filled rectangle
        GameApp.drawRect(getX(), getY(), getWidth(), getHeight(), "blue-500");

        // Optional: draw hitboxes in red for debugging
        drawHitboxes("red-500");
    }
}