Coder Social home page Coder Social logo

defold-orthographic's Introduction

defold-orthographic

Orthographic camera API for the Defold game engine. The API makes it super easy to convert screen to world coordinates, smoothly follow a game object and create a screen shake effect. This project is inspired by the camera component of the Phaser engine.

The project is shipped with an example that shows all the features of the orthographic camera. Test the example app in your browser.

Installation

You can use the orthograpic camera in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

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

Or point to the ZIP file of a specific release.

Quick Start

Getting started with Orthographic is easy:

  1. Add camera.go to your game.
  2. Open game.project and make sure to reference orthographic/render/orthographic.render in the Render field in the Bootstrap section.

Next step is to read the section on "Camera Configuration" to learn how to change the behavior of the camera.

Camera Configuration

Select the script component attached to the camera.go to modify the properties. The camera has the following configurable properties:

near_z (number) and far_z (number)

This is the near and far z-values used in the projection matrix, ie the near and far clipping plane. Anything with a z-value inside this range will be drawn by the render script.

zoom (number)

This is the zoom level of the camera. Modify it by calling camera.set_zoom(), go.set(camera, "zoom", zoom) or go.animate(camera, "zoom", ...). Read it using camera.get_zoom() or go.get(camera_id, "zoom").

Note that when using go.animate(), go.get() and go.set() you need to make sure to specify the URL to the actual camera script and not to the camera game object:

  • go.animate("mycamera#camerascript", "zoom", ...)
  • go.set("mycamera#camerascript", "zoom")
  • go.get("mycamera#camerascript", "zoom")

order (number)

The order in which multiple cameras should be drawn, lower is drawn first.

projection (hash)

The camera can be configured to support different kinds of orthographic projections. The default projection (aptly named DEFAULT) uses the same orthographic projection matrix as in the default render script (ie aspect ratio isn't maintained and content is stretched). Other projections are available out-of-the box:

  • FIXED_AUTO - A fixed aspect ratio projection that automatically zooms in/out to fit the original viewport contents regardless of window size.
  • FIXED_ZOOM - A fixed aspect ratio projection with zoom.

Note: For the above projections to work you need to pass the window dimensions from your render script to the camera. See the section on render script integration.

Additional custom projections can be added, see camera.add_projector() below.

enabled (boolean)

This controls if the camera is enabled by default or not. Send enable and disable messages to the script or use go.set(id, "enable", true|false) to toggle this value.

follow (boolean)

This controls if the camera should follow a target or not. See camera.follow() for details.

follow_horizontal (boolean)

This controls if the camera should follow the target along the horizontal axis or not. See camera.follow() for details.

follow_vertical (boolean)

This controls if the camera should follow the target along the vertical axis or not. See camera.follow() for details.

follow_immediately (boolean)

This controls if the camera should immediately position itself on the follow target when initialized or if it should apply lerp (see below). See camera.follow() for details.

follow_target (hash)

Id of the game object to follow. See camera.follow() for details.

follow_lerp (number)

Amount of lerp when following a target. See camera.follow() for details.

follow_offset (vector3)

Camera offset from the position of the followed target. See camera.follow() for details.

bounds_left (number), bounds_right (number), bounds_top (number), bounds_bottom (number)

The camera bounds. See camera.bounds() for details.

deadzone_left (number), deadzone_right (number), deadzone_top (number), deadzone_bottom (number)

The camera deadzone. See camera.deadzone() for details.

viewport_left (number), viewport_right (number), viewport_top (number), viewport_bottom (number)

The camera viewport.

Render script integration

In order for the Orthographic camera to function properly you need to integrate it in your render script. You can do this in a number of different ways:

1. Using the provided render script

The Orthographic API comes with a ready to use render script in orthographic/render/orthograpic.render_script. Open game.project and make sure to reference orthographic/render/orthograpic.render in the Render field in the Bootstrap section.

2. Integrating in an existing render script

Get a list of active cameras and apply the camera viewport, view and projection before drawing:

	local camera = require "orthographic.camera"

	function update(self)
		...
		for _,camera_id in ipairs(camera.get_cameras()) do
			local viewport = camera.get_viewport(camera_id)
			local view = camera.get_view(camera_id)
			local projection = camera.get_projection(camera_id)

			render.set_viewport(viewport.x, viewport.y, viewport.z, viewport.w)
			render.set_view(view)
			render.set_projection(projection)

			-- draw using the viewport, view and projection
			...
		end
	end

Example render script

The orthographic/render folder contains a render script that does the above mentioned integration of the Orthographic Camera API. Use it as it is or copy it into your project and make whatever modifications that you need.

Window vs Screen coordinates

The camera API allows you to convert to and from world coordinates. This is useful when positioning a game object at the position of the mouse or knowing where in a game world a mouse click was made. The API supports conversion from both window and screen coordinates.

Screen coordinates

This refers to the actual mouse pixel position within the window, scaled to the display size specified in game.project. These are the values from action.x and action.y in on_input().

Window coordinates

This refers to the actual mouse pixel position within the window. These are the values from action.screen_x and action.screen_y in on_input(). Window coordinates should be provided as is, without compensation for High DPI (this will be done automatically).

The Orthographic Camera API - functions

The API can be used in two ways:

  1. Calling functions on the camera.lua module
  2. Sending messages to the camera.script

camera.get_view(camera_id)

Get the current view of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • view (matrix) The current view

camera.get_viewport(camera_id)

Get the current viewport of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • x (number) The viewport left position
  • y (number) The viewport bottom position
  • w (number) The viewport width
  • h (number) The viewport height

camera.get_projection(camera_id)

Get the current projection of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • projection (matrix) The current projection

camera.get_projection_id(camera_id)

Get the current projection id of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • projection_id (hash) The current projection id

camera.shake(camera_id, [intensity], [duration], [direction], [cb])

Shake the camera.

PARAMETERS

  • camera_id (hash|url)
  • intensity (number) - Intensity of the shake, in percent of screen. Defaults to 0.05
  • duration (number) - Duration of the shake, in seconds. Defaults to 0.5
  • direction (hash) - Direction of the shake. Possible values: both, horizontal, vertical. Defaults to both.
  • cb (function) - Function to call when the shake has finished. Optional.

camera.stop_shaking(camera_id)

Stop shaking the camera.

PARAMETERS

  • camera_id (hash|url)

camera.recoil(camera_id, offset, [duration])

Apply a recoil effect to the camera. The recoil will decay using linear interpolation.

PARAMETERS

  • camera_id (hash|url)
  • offset (vector3) - Offset to apply to the camera. Defaults to 0.05
  • duration (number) - Duration of the recoil, in seconds. Defaults to 0.5

camera.get_offset(camera_id)

Get the current offset of the camera (caused by shake or recoil)

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • offset (vector3) The current offset of the camera

camera.get_zoom(camera_id)

Get the current zoom level of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • zoom (number) The current zoom of the camera

camera.set_zoom(camera_id, zoom)

Change the zoom level of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • zoom (number) The new zoom level of the camera

camera.follow(camera_id, target, [options])

Follow a game object.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • target (hash|url) - Game object to follow
  • options (table) - Options (see below)

Acceptable values for the options table:

  • lerp (number) - Lerp from current position to target position with lerp as t.
  • offset (vector3) - Camera offset from target position.
  • horizontal (boolean) - True if following the target along the horizontal axis.
  • vertical (boolean) - True if following the target along the vertical axis.
  • immediate (boolean) - True if the camera should be immediately positioned on the target even when lerping.

camera.follow_offset(camera_id, offset)

Change the camera follow offset.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • offset (vector3) - Camera offset from target position.

camera.unfollow(camera_id)

Stop following a game object.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

camera.deadzone(camera_id, left, top, right, bottom)

If following a game object this will add a deadzone around the camera position where the camera position will not update. If the target moves to the edge of the deadzone the camera will start to follow until the target returns within the bounds of the deadzone.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • left (number) - Number of pixels to the left of the camera
  • top (number) - Number of pixels above the camera
  • right (number) - Number of pixels to the right of the camera
  • bottom (number) - Number of pixels below the camera

camera.bounds(camera_id, left, top, right, bottom)

Limits the camera position to within the specified rectangle.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • left (number) - Left edge of the camera bounds
  • top (number) - Top edge of camera bounds
  • right (number) - Right edge of camera bounds
  • bottom (number) - Bottom edge of camera bounds

camera.screen_to_world(camera_id, screen)

Translate screen coordinates to world coordinates, based on the view and projection of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • screen (vector3) Screen coordinates to convert

RETURN

  • world_coords (vector3) World coordinates

camera.window_to_world(camera_id, window)

Translate window coordinates to world coordinates, based on the view and projection of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • window (vector3) Window coordinates to convert

RETURN

  • world_coords (vector3) World coordinates

camera.screen_to_world_bounds(camera_id)

Translate screen boundaries (corners) to world coordinates, based on the view and projection of the camera.

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera

RETURN

  • bounds (vector4) Screen bounds (x = left, y = top, z = right, w = bottom)

camera.world_to_screen(camera_id, world, [adjust_mode])

Translate world coordinates to screen coordinates, based on the view and projection of the camera, optionally taking into account an adjust mode. This is useful when manually culling game objects and you need to determine if a world coordinate will be visible or not. It can also be used to position gui nodes on top of game objects.

PARAMETER

  • camera_id (hash|url|nil) nil for the first camera
  • world (vector3) World coordinates to convert
  • adjust_mode (number) One of gui.ADJUST_FIT, gui.ADJUST_ZOOM and gui.ADJUST_STRETCH, or nil to not take into account the adjust mode.

RETURN

  • screen_coords (vector3) Screen coordinates

camera.world_to_window(camera_id, world)

Translate world coordinates to window coordinates, based on the view and projection of the camera.

PARAMETER

  • camera_id (hash|url|nil) nil for the first camera
  • world (vector3) World coordinates to convert

RETURN

  • window_coords (vector3) Window coordinates

camera.unproject(view, projection, screen)

Translate screen coordinates to world coordinates using the specified view and projection.

PARAMETERS

  • view (matrix4)
  • projection (matrix4)
  • screen (vector3) Screen coordinates to convert

RETURN

  • world_coords (vector3) Note: Same v3 object as passed in as argument

camera.project(view, projection, world)

Translate world coordinates to screen coordinates using the specified view and projection.

PARAMETERS

  • view (matrix4)
  • projection (matrix4)
  • world (vector3) World coordinates to convert

RETURN

  • screen_coords (vector3) Note: Same v3 object as passed in as argument

camera.add_projector(projector_id, projector_fn)

Add a custom projector that can be used by cameras in your project (see configuration above).

PARAMETERS

  • projector_id (hash) - Id of the projector. Used as a value in the projection field of the camera script.
  • projector_fn (function) - The function to call when a projection matrix is needed for the camera. The function will receive the id, near_z and far_z values of the camera.

camera.use_projector(camera_id, projector_id)

Set a specific projector for a camera. This must be either one of the predefined projectors (see above) or a custom projector added using camera.add_projector().

PARAMETERS

  • camera_id (hash|url|nil) nil for the first camera
  • projector_id (hash) - Id of the projector.

camera.set_window_scaling_factor(scaling_factor)

Set window scaling factor (basically retina or no retina screen). There is no built-in way to detect if Defold is running on a retina or non retina screen. This information combined with the High DPI setting in game.project can be used to ensure that the zoom behaves the same way regardless of screen type and High DPI setting. You can use an extension such as DefOS to get the window scaling factor.

PARAMETERS

  • scaling_factor (number) - Current window scaling factor

camera.get_window_size()

Get the current window size. The default values will be the ones specified in game.project.

RETURN

  • width (number) - Current window width.
  • height (number) - Current window height.

camera.get_display_size()

Get the display size, as specified in game.project.

RETURN

  • width (number) - Display width.
  • height (number) - Display height.

The Orthographic Camera API - messages

Most of the functions of the API have message equivalents that can be sent to the camera component.

shake

Message equivalent to camera.shake(). Accepted message keys: intensity, duration and direction.

msg.post("camera", "shake", { intensity = 0.05, duration = 2.5, direction = "both" })

stop_shaking

Message equivalent to camera.stop_shaking().

msg.post("camera", "stop_shaking")

recoil

Message equivalent to camera.recoil(). Accepted message keys: offset and duration.

msg.post("camera", "recoil", { offset = vmath.vector3(100, 100, 0), duration = 0.75 })

shake_complete

Message sent back to the sender of a shake message when the shake has completed.

follow

Message equivalent to camera.follow(). Accepted message keys: target, lerp, horizontal, vertical, immediate, offset.

msg.post("camera", "follow", { target = hash("player"), lerp = 0.7, horizontal = true, vertical = false, immediate = true })

follow_offset

Message equivalent to camera.follow_offset(). Accepted message keys: offset.

msg.post("camera", "follow_offset", { offset = vmath.vector3(150, 250, 0) })

unfollow

Message equivalent to camera.unfollow().

msg.post("camera", "unfollow")

deadzone

Message equivalent to camera.deadzone(). Accepted message keys: left, right, bottom and top.

msg.post("camera", "deadzone", { left = 10, right = 200, bottom = 10, top = 100 })

bounds

Message equivalent to camera.bounds(). Accepted message keys: left, right, bottom and top.

msg.post("camera", "bounds", { left = 10, right = 200, bottom = 10, top = 100 })

zoom_to

Message equivalent to camera.zoom_to(). Accepted message keys: zoom.

msg.post("camera", "zoom_to", { zoom = 2.5 })

enable

Enable the camera. While the camera is enabled it will update it's view and projection and send these to the render script.

msg.post("camera", "enable")

disable

Disable the camera.

msg.post("camera", "disable")

use_projection

Set which projection to use.

msg.post("camera", "use_projection", { projection = hash("FIXED_AUTO") })

defold-orthographic's People

Contributors

3vilwind avatar 8bitskull avatar britzl avatar dev-masih avatar dri-richard avatar matyuschenko avatar notbadgun avatar redoak avatar sayuris1 avatar temeez avatar tiberiuichim avatar u546342 avatar wistpotion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

defold-orthographic's Issues

Gui and Orthographic

So, i’ve started working with Orthographic asset for camera. Also, i used “shake” method, but the problem is that this method doesnt affect gui on my screen.
Is it possible to “shake” gui by this method too?

camera.follow not working due to NAN value in camera_world_pos vector

In my game, I want the camera to follow this specific GO, but the camera instead rises this error:
ERROR:SCRIPT: orthographic/camera.lua:419: argument #1 contains one or more values which are not numbers: vmath.vector3(nan, nan, 0) stack traceback: [C]:-1: in function __add orthographic/camera.lua:419: in function update orthographic/camera.script:73: in function <orthographic/camera.script:71>

I debugged and found out that this variable called camera_world_pos is recieving x and y values NAN. Giving an empty options table to the function won't rise any error, but the camera will not follow the target.

screen_to_world return a z value of 1

Just trying out the module. It seems that the vector returned by screen_to_world has a z value of 1 (instead of the 0 passed).
Is this correct behaviour?

Screenshot 2020-12-10 at 16 06 54

Typo in the readme

Hi, lots of thanks for your work.
I found a little typo in the readme, i wanted to copy / paste and that ended in an error. :)

Capture d’écran 2021-12-20 à 18 29 57

"h" is missing in "orthographic".

I'm sorry if this wasn't the right place for it. I'm still not used to Github.

Having issues setting zoom

Hello there! I've tried setting zoom straight from the properties on the camera.go and changing it through script. But each time it seem's to have no effect at all.

Documentation for animate with "zoom"

It took me a little while to figure out how to zoom with the camera using go.animate. When referencing the camera, it seems you have to use "camera#script" instead of camera, camera id or "camera".

If this is true, maybe change:

go.animate(camera, "zoom", ...)

To:

go.animate("camera#script", "zoom", ...)

Collection proxy

If my character is within a Collection proxy, how should I fill in the value of "target"?

Disable stencil test

Clip node clips the profiler too if stencil test is not disabled at the end of the render script. I think we should add
render.disable_state(render.STATE_STENCIL_TEST) after drawing gui in orthographic.render_script.

Feature request: Follow on one axis

Allow following a game object on x or y axis, rather than both.

Maybe something like:

camera.follow(camera_id, target, [lerp], [offset], [axis])

Where axis is "x" or "y".

camera.screen_to_world doesn't actually work?

I was expecting this to work like rendercam.screen_to_world_2d(screen_x, screen_y) but it doesn't seem to produce similar results when using screen_x and screen_y mouse position data?

zz

Should I be using camera.unproject ? it doesn't seem very user friendly (I'll read the source soon..)

There is no way of knowing when set_zoom has been actioned.

It's currently not possible to know when screen_to_world_bounds is going to return a new result since the set_zoom (and probably other methods like it) are delayed by an unknowable amount of time.

print( camera.screen_to_world_bounds(camera_id) )
local zoom = 0.25
camera.set_zoom(camera_id, zoom)
print( camera.screen_to_world_bounds(camera_id) )

camera.shake doesn't allow callback to be optional

This code:
camera.shake( camera_id )

Results in:

ERROR:SCRIPT: /orthographic/camera.lua:293: attempt to call field 'cb' (a nil value)
stack traceback:
	/orthographic/camera.lua:293: in function 'update'
	/orthographic/camera.script:68: in function </orthographic/camera.script:66>

Centre the view with bounds

When using bounds with FIXED_AUTO projection, if the window is larger than the area defined by the bounds then it is rendered in the top-right of the window. Is there any way to centre it in the window?

alignment_with_bounds

Viewport position controls cause errors and crash build

In the included example project, I set the Viewport Left to 10, and when I run, I get the error below and the build immediately crashes (the editor is fine).

I'm running Defold 1.2.183 on MacOS 11.4.

ERROR:SCRIPT: /example/assets/objects/crosshair.script:12: argument #3 contains one or more values which are not numbers: vmath.vector3(nan, inf, nan)
stack traceback:
  [C]:-1: in function lerp
  /example/assets/objects/crosshair.script:12: in function </example/assets/objects/crosshair.script:10>

ERROR:GRAPHICS: OpenGLSetViewport(1887): gl error 1281: invalid value

Assertion failed: (0), function OpenGLSetViewport, file ../src/opengl/graphics_opengl.cpp, line 1887.
INFO:CRASH: Successfully wrote Crashdump to file: /Users/brettjohnson/Library/Application Support/Defold/_crash
ERROR:CRASH: CALL STACK:

# 0 pc     0x30bd7d libxpc.dylib _sigtramp+29

Camera gets offset when resizing window and using bounds

In my game, I have the camera (FIXED_ZOOM) set to follow the player, and automatically set the bounds to the level tilemap dimensions. Everything works great until the window gets resized, then the camera gets shifted up and to the right, with the player no longer in the center.

Before resizing window (how it should look):
Default

After resizing window (now the camera is offset):
resized

Minimal example project

Camera.bounds() causes game object's position to increase enormously

My window resolution is set to 480 x 320. I’m using the FIXED_AUTO projection.

If I use the function:
camera.bounds(hash("/camera"), 0, 320, 480, 0)

The camera game object’s position skyrockets into the void to like <3e23, 15e47, 0> and continues rising like this. I’d like to add a feature where you can push a button and the camera “zooms out” and perfectly displays the entire level.

Here is a snippet of the collection hierarchy:
https://i.imgur.com/eUOv04p.png

Here is a snippet of the camera's properties:
https://i.imgur.com/llXsTcu.png

All properties omitted are set to 0 (bounds, deadzone, etc).
When I use the camera.bounds() function, the camera is not following any game object, nor is the camera a child of any game object. If I use camera.follow(player) then use camera.bounds(), the same issue occurs.

Zoom not working

I am having problems getting the zoom to work. Even following the example in the repo I cannot seem to get my code working. A minimal example which utilizes some of the same libraries I am using has been created in a branch on my Gitlab. The issue is present in this example.

window.get_size is nil?

Hi, I'm pretty new to Defold but I'm fairly familiar with Lua (I'm a LOVE2D dev considering making the switch). It's entirely possible I messed up somewhere but I'm not sure.

Here's the steps I followed, which should be essentially just following the readme:

  • Imported the dependency as instructed in the readme: https://github.com/britzl/defold-orthographic/archive/master.zip
  • Fetched dependencies so I have access to it
  • In my game.project I set /orthographic/render/orthographic.render as my Render
  • In my main.collection I added a game object from a file as /orthographic/camera.go
  • CTRL+B to build and run and... I get an error!
ERROR:SCRIPT: /orthographic/camera.lua:153: attempt to call field 'get_size' (a nil value)
stack traceback:
	/orthographic/camera.lua:153: in function 'update_window_size'
	/orthographic/camera.lua:280: in function 'update'
	/orthographic/camera.script:70: in function </orthographic/camera.script:68>

Seems straightforward enough, window.get_size appears to be nil. So I popped a breakpoint and inspected the "window" table:

{ -- <table: 0x01fe3955bc60>
  DIMMING_OFF = 2,
  DIMMING_ON = 1,
  DIMMING_UNKNOWN = 0,
  WINDOW_EVENT_FOCUS_GAINED = 1,
  WINDOW_EVENT_FOCUS_LOST = 0,
  WINDOW_EVENT_RESIZED = 2,
  get_dim_mode = <function: 0x01fe3955be00>,
  set_dim_mode = <function: 0x01fe3955bd90>,
  set_listener = <function: 0x01fe3955bd20>
}

According to the API Docs this looks like the right "window" table except for it's missing exactly one function: get_size.

To confirm, I tried to check for window.get_size and sure enough it's nil.

> window.get_size
= 
> window.get_size == nil
= true

I did a CTRL+SHIFT+F to search for anything that might have assigned over the value of window.get_size and found nothing. The only usage of get_size in my project is the line we're crashing on. This to me looks indicative of window.get_size being deprecated but I don't see any indication that it is.

I'm sure this is an obvious "oh you forgot to do X" that comes with learning any new technology but I'm honestly kind of stumped.

minor error in camera.follow documentation

options argument isn't optional as indicated by documentation, requires at least an empty dictionary.

camera.follow(camera_id, target, [options])

ERROR:SCRIPT: orthographic/camera.lua:499: attempt to index local 'options' (a nil value)
stack traceback:
  orthographic/camera.lua:499: in function follow
  screens/level/level.lua:16: in function load
  screens/level/level.script:6: in function <screens/level/level.script:4>

Drag to scroll

I'm having a hard time to implement a drag to scroll which works even if you change the window size and/or change the zoom value.
An example how to implement it together with this asset would be very helpful.

No world_to_window function

We were using world_to_screen() to convert world coordinates to gui position to spawn some gui particles, but it wasn't working correctly after window resize, even if we used the adust mode.

I noticed that there were window_to_world() and screen_to_world() functions, and screen_to_world() exists but not world_to_window()

I wrote something below which works perfectly for our purpose, but before I open a PR - am I misunderstanding something, and this could have been achieved with existing functionality?

function M.world_to_window(camera_id, world)
	local view = cameras[camera_id].view or MATRIX4
	local projection = cameras[camera_id].projection or MATRIX4
	local screen = M.project(view, projection, vmath.vector3(world))
	local scale_x = screen.x / (dpi_ratio * DISPLAY_WIDTH / WINDOW_WIDTH)
	local scale_y = screen.y / (dpi_ratio * DISPLAY_HEIGHT / WINDOW_HEIGHT)
	return vmath.vector3(scale_x, scale_y, 0)
end

Feature request: Query boundaries at a certain zoom level

I’d like to know the bounds of the screen before I zoom out and/or move camera, so I can set the correct offset based on screen size.

Maybe screen_to_world_bounds extra parameters:

camera.screen_to_world_bounds( camera_id, [camera_position], [zoom] )

Confusion regarding screen and window coordinates

Minor notice that screen coordinates use action.x instead of action.screen_x. This is a bit confusing, so unless there are other reasons to keep the current naming convention (other than backwards compatibility) it would be nice to switch the two names.

Documentation typo

follow_horizontal (boolean) - True if following the target along the horizontal axis.
follow_vertical (vector3) - True if following the target along the vertical axis.

Should be:

horizontal (boolean) - True if following the target along the horizontal axis.
vertical (boolean) - True if following the target along the vertical axis.

How to calculate MinX, MinY, MaxX, MaxY on real device when using FIXED_AUTO mode

Hi @britzl !
Thank so much for creating an awesome camera plugin
I am new in Defold and I want to control the game object base on the edge of the real device on runtime, via script.
So basically here is some input:
Display params on game.project: Width 640, Height 960
Projection mode: FIXED_AUTO
When running on real device it logs:
camera.get_display_size() 640 960
camera.get_window_size() 1080 1920

Now I want to calculate the MINX, MINY so when I set the position of a sprite (In the script, not in the editor), it is always on the bottom left of any real device.
(It is the bound of the real device - MinX, MinY, W, H or MinX, MinY, MaxX, MaxY)
(It is expected MinX, MinY to be a negative value on some device, it calculates base on display_size,window_size, zoom, and that value will relative with display_size)
Also, I notice that if using camera plugin, now 0,0 in editor convert to the center of the real device?

Thank so much!

Bounds are not working for me at all

All I see is a blinking like camera is moving back and forth very fast (each update)
Same happens in a sample scene when camera reaches bounds.

When switching between two collections containing Orthographic, the console returns errors.

When working with Orthographic I have realized that when switching between collections that both have Orthographic in them returns an error:

_

Error:Script: /orthographic/camera.lua:410: attempt to index a nil value
stack traceback:
/orthographic/camera.lua:410: in function 'send_view_projection'
/orthographic/camera.script:63: in function </orthographic/camera.script:60>

_

image

[QUESTION] What would be equivavlent to older version of camera.set_window_size() ?

I have an issue when using this library together with the lowrez template where the contents of the screen become super pixelated and unreadable even for lowrez.

I'm using custom render script that is copy pasta of the lowrez render script with added viewport setting in the update function that comes the new version of the lib.

The workaround is to set display width and height in game.project but that causes an issue when uploading to itch where the game is running in a super small window.

As far as I can see in the older versions that would have been done with set_window_size but what to do now?

Follow doesn't work when using camera directly

Works:

msg.post("camera", "follow", {target=ball,lerp=0.7,offset=vector3(0,0,0),horizontal=true,vertical=false})

Doesn't work:

camera.follow( camera_id,ball, {lerp=0.7,offset=vector3(0,0,0),horizontal=true,vertical=false} )

Error:

ERROR:SCRIPT: /orthographic/camera.lua:219: bad argument #2 to '__add' (vector3 expected, got number)
stack traceback:
	[C]: in function '__add'
	/orthographic/camera.lua:219: in function 'update'
	/orthographic/camera.script:68: in function </orthographic/camera.script:66>

Camera needed for GUI?

Not sure If this is by design now, but GUI elements now require there to be a camera object in the collection, otherwise it doesn't render.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.