Getting direction giving odd directions


According to all found resources (and basic math), to find out what direction one must go to get to a target point, you subtract your position from the targets position. I am using this in a simple dash for my game. However, the problem is that, no matter if I base it off of screen or world coordinates, it always gives me two problems: first, no matter where I position the cursor relative to the player or the center of the screen (they are the same place), the X value of the direction is almost always negative, pushing my character to the left. Similarly, the Y value is often negative, despite the cursor being above the player.

Here is the code I am using (also, if it makes any difference, I am using the rendercam camera):

local dash_speed = 1200

local dash(self, mouse_x, mouse_y)
    -- i also prevent holding down the mouse

    self.velocity = vmath.normalize( vmath.vector3( mouse_x, mouse_y, 0 ) - go.get_world_position() ) * dash_speed

function on_input(self, action_id, action)
    -- some other inputs
    if action_id == input_dash then -- pre-hashed
        dash(self, action.x, action.y) -- i also have some code so you can't just hold the dash button

I have tried swapping out go.get_world_position for go.get_position and action.x, action.y for action.screen_x, action.screen_y.

Sorry if I am missing anything obvious (which is most likely the case), I am both new to Defold (although it is quite easy to learn) and bad with vectors. Thanks for any help!



Whoops, I meant

local function dash ...

The error is not there in my program, of course.



Are you converting the mouse position to world coordinates in your actual code? Action.x and action.y aren’t going to make any sense. With Rendercam you use rendercam.screen_to_world_2d (for a 2D camera).



That is so weird; that was the first thing I tried, and yet it didn’t work then but does now. I guess I forgot to get action.screen_x instead of just action.x. Thanks anyway!

1 Like