Calculating right distance between 2 points

Hey guys,

i want to calculate distance between node and mouse, which i use as size of line (rectangle sprite) to connect (visually) two nodes. I know it is simple goniometry but there is something wrong. So my code looks like this:

local x1, y1 = action.screen_x, action.screen_y
local pos2 = gui.get_screen_position(some_node)

-- basic distance formula in 2d space
local size = math.sqrt(math.pow(pos2.x - x1, 2) + math.pow(pos2.y - y1, 2))

i am using RenderCam asset for ortho camera and game is fully scalable. The problem is that calculated size is nowhere close to what i want to achieve (set size of node from some_node to mouse). Am i missing something important?
Thanks…

The length calculation seems ok to me (although I’d probably do dx*dx instead of using math.pow since that’s a much “heavier” function)

Also, you might want to use the vmath module for this:

local p0 = vmath.vector3(action.screen_x, action.screen_y)
local p1 = gui.get_screen_position(some_node)
local length = vmath.length(p1 - p0)
2 Likes

What exactly are you doing with the distance afterwards? Even if it is correct, if you are trying to display it in a different coordinate space then it won’t look right.

rendercam.screen_to_gui() might help you?
Or, if you’re doing something in world-space with it you’ll need rendercam.screen_to_world_2d().

1 Like

Thank you for replies guys. As i said i’m using calculated size to set size of node so it look like this:

gui.set_size(node, vmath.vector3(sx, size, 0))

where node have set pivot to North and whole point of it is that node is representing line which connect 2 another nodes. As ross.grams said, i’ve probably doing this in wrong space so i will try do some workaround about it.