Quellcode durchsuchen

Added section about input detection on objects

Björn Ritzl vor 6 Jahren
Ursprung
Commit
0257b21c45
2 geänderte Dateien mit 35 neuen und 5 gelöschten Zeilen
  1. 10 5
      docs/en/manuals/camera.md
  2. 25 0
      docs/en/manuals/input.md

+ 10 - 5
docs/en/manuals/camera.md

@@ -5,10 +5,7 @@ brief: This manual describes the functionality of the Defold camera component.
 
 # Cameras
 
-A camera in Defold is a component that changes the viewport and projection of the game world. The camera component defines a bare bones perspective or orthographic camera that provides a view and projection matrix to the render script. A perspective camera is typically used for 3D games while an orthographic camera is used for 2D games. If you need advanced features like chasing, zooming, shake etc you will need to implement it. There are a few library camera solutions that implements common camera features. They are available from the Defold community assets portal:
-
-- [Rendercam](https://www.defold.com/community/projects/84064/) (2D & 3D) by Ross Grams.
-- [Ortographic camera](https://www.defold.com/community/projects/76573/) (2D only) by Björn Ritzl.
+A camera in Defold is a component that changes the viewport and projection of the game world. The camera component defines a bare bones perspective or orthographic camera that provides a view and projection matrix to the render script. A perspective camera is typically used for 3D games while an orthographic camera is used for 2D games. If you need advanced features like chasing, zooming, shake etc you will need to implement it yourself (see section on [third-party camera solutions](https://www.defold.com/manuals/camera/#_third_party_camera_solutions) below).
 
 ## Creating a camera
 
@@ -87,7 +84,7 @@ An alternative way is to update the position of the game object the camera compo
 When the camera has panned, zoomed or changed it's projection from the default orthographic Stretch projection the mouse coordinates provided in the on_input() lifecycle function will no longer match to the world coordinates of your game objects. You need to manually account for the change in view or projection. Converting from mouse/screen coordinates to world coordinates from the default render script is done like this:
 
 ::: sidenote
-Both the RenderCam and Orthographic camera extensions mentioned at the top of the document provides functions for converting to and from screen coordinates.
+The [third-party camera solutions mentioned in this manual](/manuals/camera/#_third_party_camera_solutions) provides functions for converting to and from screen coordinates.
 :::
 
 ```Lua
@@ -136,3 +133,11 @@ msg.post("@render:", "use_camera_projection")
 ```
 
 Learn more about the render script in the [Render manual](/manuals/render/#_perspective_projection).
+
+
+## Third-party camera solutions
+
+There are a few library camera solutions that implements common camera features such as game object follow, screen to world coordinate conversion and so on. They are available from the Defold community assets portal:
+
+- [Rendercam](https://www.defold.com/community/projects/84064/) (2D & 3D) by Ross Grams.
+- [Ortographic camera](https://www.defold.com/community/projects/76573/) (2D only) by Björn Ritzl.

+ 25 - 0
docs/en/manuals/input.md

@@ -261,6 +261,31 @@ function on_input(self, action_id, action)
 end
 ```
 
+
+## Detecting click or tap on objects
+
+Detecting when the user has clicked or tapped on a visual component is a very common operation that is needed in many games. It could be the user interaction with a button or other UI element or the interaction with a game object such as a player controlled unit in a strategy game, some loot on a level in a dungeon crawler or a quest giver in an RPG. The approach to use varies depending on the type of visual component.
+
+### Detecting interaction with GUI nodes
+
+For UI elements there is the `gui.pick_node(node, x, y)` function that will return true or false depending on if the specified coordinate is within the bounds of the gui node or not. Refer to the [API docs](/ref/gui/#gui.pick_node:node-x-y), the [pointer over example](https://www.defold.com/examples/gui/pointer_over/) or the [button example](https://www.defold.com/examples/gui/button/) to learn more.
+
+### Detecting interaction with game objects
+For game objects it is more complicated to detect input since things such as camera translation and render script projection will impact the required calculations. There are two general approaches to detecting interaction with game objects:
+
+  1. Track the position and size of game objects the user can interact with and check if the mouse or touch coordinate is within the bounds of any of the objects.
+  2. Attach collision objects to game objects the user can interact with and one collision object that follows the mouse or finger and check for collisions between them.
+
+::: sidenote
+A ready to use solution for using collision objects to detect user input can be found in the [Defold-Input library asset](https://github.com/britzl/defold-input).
+:::
+
+In both cases there is a need to convert from the screen space coordinates of the mouse or touch event and the world space coordinates of the game objects:
+
+  * Manually keep track of which view and projection that is used by the render script and use this to convert to and from world space. See the [camera manual for an example of this](/manuals/camera/#_converting_mouse_to_world_coordinates).
+  * Use a [third-party camera solution](/manuals/camera/#_third_party_camera_solutions) and make use of the provided screen to world conversion functions.
+
+
 ## Gamepads settings file
 
 Gamepad mappings for specific hardware gamepads are set in a *gamepads* file. Defold ships with a built in gamepads file with settings for common gamepads: