🌀 Rotation System

The engine uses a degree-based rotation system that applies to all game objects: sprites, entities, colliders, particles, and UI elements. Each object has a rotation property defining its current orientation in the world.

📐 Basic Concepts

⚙️ Rotation Properties

🧩 API Functions

1️⃣ setRotation(obj, angle)

Sets the absolute rotation of an object (in degrees).

const ship = getObjectByName("Ship");
setRotation(ship, 90); // rotate the ship 90 degrees

2️⃣ getRotation(obj)

Returns the object’s current rotation (in degrees).

const angle = getRotation("Ship");
console.log("Current angle:", angle);

3️⃣ rotateObject(obj, delta)

Adds an incremental rotation (in degrees).

// Rotate the object 5° per frame
function Update(o, dt) {
  rotateObject(o, 5 * dt * 60);
}
🔸 Internally, the engine keeps rotations within the 0–360° range. If it exceeds 360°, it wraps back to 0 to avoid infinite accumulation.

🧭 Rotations with Hierarchy

When an object has a parent, its total rotation is:

globalRotation = parentRotation + rotation;

This means child objects rotate automatically with their parent.

Example

// The weapon rotates along with the player
const player = getObjectByName("Player");
const weapon = getObjectByName("Weapon");

setParent(weapon, player);
setRotation(weapon, 45); // initial angle of the weapon

// Rotate the player 30°
rotateObject(player, 30);

// The weapon now points at 75° globally
console.log("Weapon global rotation:", getRotation(weapon));

💡 Practical Examples

🧱 1. Constant Rotation

// Script: rotate.js
let speed = 60; // degrees per second

function Update(obj, dt) {
  rotateObject(obj, speed * dt);
}

🚀 2. Rotation Controlled by Keyboard

// Script: rotate_keys.js
let speed = 90; // degrees per second

function Update(obj, dt) {
  if (keys["ArrowLeft"])  rotateObject(obj, -speed * dt);
  if (keys["ArrowRight"]) rotateObject(obj,  speed * dt);
}

🪄 3. Synchronized Rotation Between Objects

const sun = getObjectByName("Sun");
const planet = getObjectByName("Planet");
setParent(planet, sun);

// the planet rotates with the sun
rotateObject(sun, 10 * dt);

📌 Summary

– Rotations are always expressed in degrees.
– Applied around the object's center.
rotateObject() safely adds incremental rotations (0–360°).
– Child objects inherit the parent’s rotation.

⚠️ Current Limitations with Collisions

Even though objects can visually rotate freely, the engine uses a collision system based on AABB (Axis-Aligned Bounding Boxes), meaning collision boxes always remain aligned with the X and Y axes regardless of the object’s visual angle.

🔸 This only affects objects with collisionType = "square".
🔸 Visual rotation does not affect the real collision shape.
🔸 Circular colliders (collisionType = "circle") are unaffected because their shape is symmetric.

🎯 Illustrative Example

// Sprite visually rotated 45°, but its collision stays AABB
const box = getObjectByName("Crate");
setRotation(box, 45);

// Visually:
// 🔄 The sprite appears tilted 45°

// Physically:
// 🧱 The collision remains an axis-aligned rectangle

Future versions may incorporate OBB (Oriented Bounding Box) collision detection for precise rotated collisions.

⚙️ In summary:
– Square-type collisions do not rotate.
– Circular collisions behave normally.