chance defold input action pos in such a way that 0.0 point was in middle of app
- User
chance defold input action pos in such a way that 0.0 point was in middle of app
- Assistant
To adjust the input action position in Defold such that the point (0.0) is considered to be in the middle of the app (typically for mouse or touch input), you should remap the input coordinates received from input actions. This is because, by default, Defold's coordinate system for input (like mouse positions) starts from the bottom-left corner of the screen or window, with (0, 0) at that point and positive values increasing to the right and upwards.
Here is a general approach to re-centering the input coordinates:
-
Capture the Input: First, ensure you have an input listener set up to capture the mouse or touch input. This usually involves setting up an input method in a script attached to a game object.
-
Get the Screen Size: You'll need to know the current size of the window or screen to properly calculate the centered coordinates. You can get this size using
render.get_window_width()andrender.get_window_height()for the width and height, respectively. -
Calculate Centered Coordinates: When you receive an input action, adjust the coordinates from the action such that the center of the screen is treated as the origin (0,0).
Here is a code snippet that demonstrates this technique:
local function transform_input_to_center_coordinates(action) -- Get the dimensions of the window/screen local width = render.get_window_width() local height = render.get_window_height() -- Calculate the centered coordinates -- x and y are the original input coordinates local centered_x = (action.x - (width / 2)) / (width / 2) local centered_y = (action.y - (height / 2)) / (height / 2) -- Return the transformed coordinates return centered_x, centered_y end function on_input(self, action_id, action) if action_id == hash("your_input_action") and action.pressed then local centered_x, centered_y = transform_input_to_center_coordinates(action) -- Now you can use centered_x and centered_y as needed end endThis code effectively remaps the input position so that the center of the screen/app window becomes (0,0). The calculations also normalize the positions to a -1 to 1 range based on the width and height for easier use, assuming the screen is centered. You might need to adjust the calculation if the aspect ratio of the input should be considered differently or according to your specific requirements.
Always make sure to register your input listener and properly define your input actions in your game project settings.
-
- User
middle point is -0.5 -0.5 not 0 0
- Assistant
If you want to adjust input positions so that the middle of the app window is considered to be at (-0.5, -0.5) rather than (0, 0), we'll need to slightly modify the approach used to transform input coordinates. However, it's worth noting that in most cases and conventions, the center point is represented as (0,0) in normalized coordinates, especially in graphics programming and game development.
Using (-0.5, -0.5) as the center is unconventional and might lead to confusion or errors in calculations, especially if you're interacting with other libraries or systems expecting a more standard coordinate system. But if you have a specific use case or reason for wanting the center to be at (-0.5, -0.5), you could technically adjust the normalized coordinates accordingly.
Assuming you still want to proceed with this setup, let's go through the process of adjusting the input action's position:
local function transform_input_to_custom_center(action, width, height) -- Calculate the centered coordinates -- Adjust these calculations as per your requirements local centered_x = (action.x / width) - 0.5 local centered_y = (action.y / height) - 0.5 -- Return the transformed coordinates return centered_x, centered_y end function on_input(self, action_id, action) if action_id and action.pressed then -- Get the dimensions of the window/screen local width = render.get_window_width() local height = render.get_window_height() local centered_x, centered_y = transform_input_to_custom_center(action, width, height) -- Now you can use centered_x and centered_y as needed, though they are offset according to your specifications end endThis script adjusts input positions based on the entire window's width and height but then shifts them so that the midpoint is effectively (-0.5, -0.5) in the normalized coordinate system derived from the window dimensions. It's important to remember that this setup might not work straightforwardly with other engines, libraries, or parts of your game expecting a more standard approach to coordinates.
Important Note: Always consider the implications of using a non-standard coordinate system like this, as it may introduce complexity or confusion in the development process, especially when dealing with graphics, physics engines, or UI elements expecting the center as (0,0).
Please re-evaluate the need for setting the center point to (-0.5, -0.5) and consider sticking with more conventional norms unless there's a very specific requirement or reason for this choice.
- User
now 0 0 in right top corner
- Assistant
If you want to adjust the input coordinates such that the point (0,0) is at the top-right corner of the app or game window in Defold, you'll need a different approach when transforming the input coordinates. Here's how you can achieve this:
Given that originally, in Defold, the bottom-left corner is (0,0) and the coordinate system follows a conventional screen space (with positive Y going upwards and positive X going to the right), moving the origin (0,0) to the top-right corner involves flipping the Y coordinate and shifting the X coordinate based on the window's width.
Here's how you can implement this:
local function transform_input_to_top_right_origin(action, width, height) -- Flip the Y coordinate and move the origin to the top-right local transformed_x = width - action.x -- This will move the origin to the right local transformed_y = action.y -- Since Defold's Y=0 starts from the bottom, we were actually needing to flip Y. Considering typical use, to adjust the coordinate system to common expectations, subtract from height. -- Normalize the coordinates if needed, otherwise, it's in pixels local normalized_x = transformed_x / width local normalized_y = transformed_y / height -- Invert the Y-axis to have 0,0 at the top right normalized_y = 1.0 - normalized_y return normalized_x, normalized_y end function on_input(self, action_id, action) if action_id and action.pressed then -- Get the dimensions of the window/screen local width = render.get_window_width() local height = render.get_window_height() local transformed_x, transformed_y = transform_input_to_top_right_origin(action, width, height) -- Use transformed_x and transformed_y as needed end endIn this script:
transform_input_to_top_right_origincalculates the new coordinates with (0,0) at the top-right by first finding the mirrored positionwidth - action.xfor the x-coordinate and using the given y-coordinate as it is. This effectively moves the origin to the top-right corner without flipping the y-axis yet.- It then normalizes these coordinates to a 0.0 - 1.0 range by dividing by the window's width and height, assuming
action.xandaction.yare in pixels. - After normalization, we make a final adjustment to
normalized_yby inverting it (1.0 - normalized_y) to correctly position (0,0) at the top-right with Y increasing downwards, matching a conventional top-origin screen space used in some systems.
Remember, this script assumes
action.xandaction.yare the pixel coordinates of your input (e.g., mouse position) relative to the window. The normalization step is optional and depends on whether you want to work with pixel coordinates or normalized coordinates (0.0 to 1.0 range) within your game logic.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.