Make 2D Games with Tiniest2D
Collision and Physics
Lesson 9 of 11 • 10 XP
Keep your place in this quest
Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.
Collision System
How It Works
- Every object has an AABB (Axis-Aligned Bounding Box) based on its sprite size
- Sprites can have custom collider bounds (set in Sprite Editor)
- Collision is checked every frame for dynamic objects
Collision Types Explained
- None - Object is completely ignored by collision system
- Static - Acts as an immovable wall (use for terrain, platforms)
- Dynamic - Moves and gets pushed out of other colliders (use for player, enemies)
- Trigger - Detects overlap but doesn't block movement (use for collectibles, zones)
Collision Resolution
- Dynamic objects are automatically pushed out of Static and other Dynamic objects
- Triggers never block movement - they only detect overlaps
The onCollision Callback
The onCollision(objA, objB) function is called when objects overlap. The callback patterns are:
| objA Type | objB Type | Callback Generated? | Notes |
|---|---|---|---|
| Dynamic | Static | Yes | Dynamic pushed out of static |
| Dynamic | Dynamic | Yes (both directions) | Both objects get callbacks |
| Dynamic | Trigger | Yes | Trigger detected |
| Trigger | Trigger | Yes | Both triggers detect each other |
| Static | Static | No | Statics don't generate callbacks |
| Static | Trigger | No | Neither generates callbacks |
Key rules:
objAis always either Dynamic or Trigger- When two Dynamic objects collide, both get a callback (each as objA)
- When two Triggers overlap, one callback is generated (first trigger as objA)
- Static objects never appear as objA
Example: Detecting Different Collision Types
func onCollision(objA, objB) {
var typeA = getCollisionType(objA)
var typeB = getCollisionType(objB)
// Player (dynamic) touching a coin (trigger)
if (getName(objA) == "player" && typeB == COLLISION_TRIGGER()) {
print("Collected!")
removeObject(gameScene, objB)
}
// Two triggers overlapping (e.g., detection zones)
if (typeA == COLLISION_TRIGGER() && typeB == COLLISION_TRIGGER()) {
print(getName(objA) + " entered " + getName(objB) + "'s zone")
}
// Player hit by enemy (both dynamic)
if (getName(objA) == "player" && getName(objB) == "enemy") {
print("Player hit!")
}
}
Physics System
Tiniest Engine has a built-in physics system that handles gravity and velocity automatically for dynamic objects.
How Physics Works
Every frame, the engine automatically:
- Applies scene gravity to each dynamic object's velocity
- Applies velocity to each dynamic object's position
// Physics pipeline (handled automatically by engine):
velocity += gravity * dt
position += velocity * dt
Setting Up Gravity
Set gravity on a scene to make dynamic objects fall:
func start() {
// Set downward gravity (300 pixels per second squared)
setGravityY(gameScene, 300)
// Optional: horizontal gravity (wind effect)
setGravityX(gameScene, 0)
setScene(gameScene)
}
Using Velocity
Control objects using velocity instead of directly setting position:
var player = Object("player")
var jumpForce = -200 // Negative = upward
func start() {
setCollisionType(player, COLLISION_DYNAMIC())
addObject(gameScene, player)
setGravityY(gameScene, 300)
setScene(gameScene)
}
func onUpdate(dt) {
// Horizontal movement via velocity
if (isKeyHeld(KEY_LEFT())) {
setVelocityX(player, -100)
} else if (isKeyHeld(KEY_RIGHT())) {
setVelocityX(player, 100)
} else {
setVelocityX(player, 0)
}
// Jump by setting upward velocity
if (isKeyPressed(KEY_SPACE())) {
setVelocityY(player, jumpForce)
}
}
Physics Functions
| Function | Description |
|---|---|
getVelocityX(obj) |
Returns the object's X velocity |
setVelocityX(obj, vx) |
Sets the object's X velocity |
getVelocityY(obj) |
Returns the object's Y velocity |
setVelocityY(obj, vy) |
Sets the object's Y velocity |
getGravityX(scene) |
Returns scene gravity X component |
setGravityX(scene, gx) |
Sets scene gravity X component |
getGravityY(scene) |
Returns scene gravity Y component |
setGravityY(scene, gy) |
Sets scene gravity Y (positive = down) |
Important: Only COLLISION_DYNAMIC() objects are affected by gravity and have their velocity applied automatically