Class PhysicsManager

java.lang.Object
org.collebol.shared.physics.PhysicsManager

public class PhysicsManager extends Object
The PhysicsManager handles registration, movement, and collision detection for all GameObject instances that contain physics components.

This manager provides a simple, brute-force collision system based on BoxCollider checks. When an object is moved using tryMove(GameObject, GameLocation), the system validates that the move does not cause the object to intersect with any other registered object in the world.

Usage:

     // Create a new physics manager
     PhysicsManager physicsManager = new PhysicsManager();

     // Register game objects
     GameObject player = new Player();
     player.addPhysicsComponent(new BoxCollider(player, 1, 1));
     physicsManager.register(player);

     // Attempt to move the player to a new location
     GameLocation newLoc = new GameLocation(5, 3);
     boolean success = physicsManager.tryMove(player, newLoc);

     if (!success) {
         System.out.println("Movement blocked by collision!");
     }
 

This class can be extended or replaced with more advanced physics systems if needed, such as those involving velocity, gravity, or spatial partitioning.

Since:
1.0-dev
Author:
ColleBol - contact@collebol.org
  • Constructor Details

    • PhysicsManager

      public PhysicsManager()
      Constructs a new PhysicsManager with an empty list of registered objects.
  • Method Details

    • register

      public void register(GameObject object)
      Registers a GameObject for collision detection and movement handling.

      If the object is already registered, this method has no effect.

      Parameters:
      object - the game object to register
    • tryMove

      public boolean tryMove(GameObject object, GameLocation newLoc)
      Attempts to move the specified GameObject to a new GameLocation.

      The move will only succeed if no collision occurs with any other registered object's BoxCollider. If a collision is detected, the object's position is restored to its previous location and this method returns false.

      Parameters:
      object - the object to move
      newLoc - the desired new location
      Returns:
      true if the move was successful (no collision), false if the object collided and its position was reverted