How do you extend a vector away from another vector? (SOLVED)

I have a player position and a touch position converted to world space. I want the touch position to be extended away from the player, like this:

I can’t just multiply the touch position by a scalar, since that would result in this:

How would you get that position described in the first image?

To get the vector between the “touch” and the “player”, you take the difference:

local diff = touch_pos - player_pos

To extend that vector, you can use multiplication:

local diff = (touch_pos - player_pos) * 1.5

If you need an exact length of the vector, you need to normalize it, and here you’ll need to make sure it isn’t a zero length vector (to avoid getting NaN’s):

local scale = 1.5
local diff = touch_pos - player_pos
local length = vmath.length(diff)
if diff < 0.001 then -- avoid NaN's, 
    diff = vmath.vector3(1,0,0) -- pick an arbitrary direction
else
    diff = vmath.normalize(diff)
end
-- diff is now of length 1
diff = diff * scale
4 Likes