defold-orthographic

repository·master·Indexed 42 days ago

https://github.com/britzl/defold-orthographic

An orthographic camera API for the Defold game engine that extends standard camera capabilities. It provides functionality for following game objects, managing camera bounds and deadzones, and implementing visual effects such as screen shake and recoil. The API is accessible via the camera.lua module or a message-based system using msg.post to the camera.script.

Tokens
2.7K
Snippets
13
Records
16
Agent score
3%

What's inside defold-orthographic

  1. Use message-based API for camera control

    master
    Most functions in the Orthographic Camera API have message equivalents that can be sent to the camera component using msg.post. This allows you to control the camera from other game objects without needing direct access to the camera's Lua module.
  2. Use the camera.lua module API

    master

    The Orthographic Camera API can be accessed in two ways:

    1. Directly via the camera.lua module: Call functions like camera.shake() or camera.follow() in your Lua code.
    2. Via Messages: Send messages to the camera.script attached to your camera game object.
  3. How to use multiple cameras or custom viewports

    master
    The default Defold render script only renders a single camera covering the entire screen. To use multiple cameras or custom viewports, you must modify your render script. It is recommended to use the render script provided in the library: orthographic/render/orthographic.render_script.
  4. Install defold-orthographic as a library dependency

    master

    To use the orthographic camera in your Defold project, add it as a library dependency in your game.project file. Under the dependencies field, add the following URL:

    https://github.com/britzl/defold-orthographic/archive/master.zip

    Alternatively, you can point to a specific release ZIP file from the GitHub releases page.

    https://github.com/britzl/defold-orthographic/archive/master.zip
  5. Quick Start: Set up a camera in Defold

    master
    1. Add a camera.go game object to your collection.
    2. Attach the camera.script to that game object.
    3. Configure the script properties in the Defold editor to set up your initial camera state (zoom, bounds, follow target, etc.).

    Important Note on accessing properties: When using Defold's built-in go.set(), go.get(), or go.animate() functions, you must specify the URL to the camera script, not just the game object. For example, if your game object is named mycamera, use mycamera#camerascript.

    go.animate("mycamera#camerascript", "zoom", ...)
    go.set("mycamera#camerascript", "zoom")
    go.get("mycamera#camerascript", "zoom")
  6. Control camera following via messages

    master

    Use follow and follow_offset messages to make the camera track a target object.

    Follow target: Accepted message keys:

    • target: The hash of the target object.
    • lerp: Linear interpolation factor for smoothing.
    • horizontal: Boolean to enable/disable horizontal tracking.
    • vertical: Boolean to enable/disable vertical tracking.
    • immediate: Boolean to snap to target immediately.
    • offset: A vmath.vector3 for target offset.

    Follow with offset: Accepted message keys:

    • offset: A vmath.vector3 for the tracking offset.

    Stop following: Send the unfollow message to stop tracking.

    -- Follow a player with smoothing
    msg.post("camera", "follow", { target = hash("player"), lerp = 0.7, horizontal = true, vertical = false, immediate = true })
    
    -- Follow with a specific offset
    msg.post("camera", "follow_offset", { offset = vmath.vector3(150, 250, 0) })
    
    -- Stop following
    msg.post("camera", "unfollow")
  7. Convert between Screen and World coordinates

    master

    The API provides utilities to translate coordinates between the screen (input/GUI) and the game world.

    • Screen to World: Use camera.screen_to_world(camera_id, screen) to convert a vector3 screen coordinate to world coordinates.
    • Screen Boundaries to World: Use camera.screen_to_world_bounds(camera_id) to get the world coordinates of the screen corners (returns a vector4: x=left, y=top, z=right, w=bottom).
    • World to Screen: Use camera.world_to_screen(camera_id, world, [adjust_mode]) to convert a vector3 world coordinate to screen coordinates. This is useful for positioning GUI elements over game objects.
    local world_pos = camera.screen_to_world("mycamera#camerascript", vmath.vector3(input.get_touch_x(), input.get_touch_y(), 0))
    local screen_pos = camera.world_to_screen("mycamera#camerascript", vmath.vector3(player_pos.x, player_pos.y, 0))
  8. Trigger camera shake via messages

    master

    Send a shake message to the camera component to initiate a screen shake effect. You can specify the intensity, duration, and direction.

    Accepted message keys:

    • intensity: Float value for shake strength.
    • duration: How long the shake lasts.
    • direction: The axis/pattern of the shake (e.g., "both").

    When the shake finishes, the camera component sends a shake_complete message back to the sender.

    msg.post("camera", "shake", { intensity = 0.05, duration = 2.5, direction = "both" })
  9. Enable, disable, and set projection via messages

    master

    Control the active state and projection type of the camera.

    Enable/Disable:

    • enable: Activates the camera. While enabled, it updates view/projection and sends them to the render script.
    • disable: Deactivates the camera.

    Set projection: Accepted message keys:

    • projection: A hash representing the projection type (e.g., hash("FIXED_AUTO")).
    msg.post("camera", "enable")
    msg.post("camera", "disable")
    msg.post("camera", "use_projection", { projection = hash("FIXED_AUTO") })
  10. Trigger camera recoil via messages

    master

    Send a recoil message to create a sudden offset effect.

    Accepted message keys:

    • offset: A vmath.vector3 representing the displacement.
    • duration: How long the recoil transition lasts.
    msg.post("camera", "recoil", { offset = vmath.vector3(100, 100, 0), duration = 0.75 })
  11. Control camera zoom via messages

    master

    Manage camera zoom levels and automatic zoom behavior.

    Zoom to specific level: Accepted message keys:

    • zoom: The target zoom level.

    Set automatic zoom: Accepted message keys:

    • enabled: Boolean to enable or disable automatic zoom.
    -- Zoom to 2.5x
    msg.post("camera", "zoom_to", { zoom = 2.5 })
    
    -- Enable automatic zoom
    msg.post("camera", "set_automatic_zoom", { enabled = true })
  12. Configure camera deadzone and bounds via messages

    master

    Adjust the area within which the target can move before the camera reacts, or set the camera's movement boundaries.

    Deadzone: Accepted message keys:

    • left, right, bottom, top: Pixel values defining the deadzone area.

    Bounds: Accepted message keys:

    • left, right, bottom, top: Pixel values defining the camera movement limits.
    -- Set deadzone
    msg.post("camera", "deadzone", { left = 10, right = 200, bottom = 10, top = 100 })
    
    -- Set camera bounds
    msg.post("camera", "bounds", { left = 10, right = 200, bottom = 10, top = 100 })