Overview
MyCharacter is the base class that extends THREE.Object3D and provides common functionality for all movable characters in the game (Pac-Man and ghosts). It handles positioning, collision detection, rotation, and movement.
Inheritance: THREE.Object3D → MyCharacter
Constructor
Initial position of the character in grid coordinates (will be multiplied by
MyConstant.BOX_SIZE)Size of the character model
Properties
model
animated
true
hitbox
BOX_SIZE for more forgiving collision detection.
helper
MyConstant.SHOW_HITBOX is true.
speed
0.15
dirX
dirZ
Methods
getPosition()
Returns the current position of the character’s model. Returns:THREE.Vector3 - The model’s position
setPosition2D(pos2D)
Sets the character’s 2D position (X and Z axes only). Parameters:pos2D(THREE.Vector2) - New X and Z coordinates
getCollisionBox()
Returns the character’s collision bounding box. Returns:THREE.Box3 - The hitbox used for collision detection
adjustedPosition(pos, dir)
Calculates an adjusted grid-aligned position based on the current position and direction. Parameters:pos(THREE.Vector2) - Current position in grid coordinatesdir(THREE.Vector2) - Current direction vector
THREE.Vector2 - Grid-aligned position
adjustPosition()
Snaps the character to the nearest grid position based on current movement direction. Updates both model position and hitbox.rotate(dir)
Rotates the character model to face the specified direction. Parameters:dir(Object) - Direction object withxandyproperties (values: -1, 0, or 1)
{x: 1, y: 0}- Face right (rotation.y = 0){x: -1, y: 0}- Face left (rotation.y = π){x: 0, y: 1}- Face down (rotation.y = 3π/2){x: 0, y: -1}- Face up (rotation.y = π/2)
update()
Called every frame to update character state. Moves the character and updates the hitbox position.Usage Example
Notes
- Position coordinates are automatically converted from grid coordinates to world coordinates by multiplying by
MyConstant.BOX_SIZE - The hitbox is 75% of
BOX_SIZEto allow smoother navigation through corridors - Movement is based on rotation: the character always moves forward in the direction it’s facing
- This class should typically be extended rather than instantiated directly