Using trigonometry to calculate angle of movement using mouse position
snippet in lua

Using trigonometry to calculate angle of movement using mouse position

user9954

-- define the difference vector
vec = {}
vec.x = mouse.x - player.x
vec.y = mouse.y - player.y

-- compute its length, to normalize
vec_len = math.pow(math.pow(vec.x, 2) + math.pow(vec.y, 2), 0.5)

-- normalize
vec.x = vec.x / vec_len
vec.y = vec.y / vec_len

-- move the player
player.x = player.x + vec.x * player.velocity * delta_time
player.y = player.y + vec.y * player.velocity * delta_time