Code: Select all
{ magnitude * direction_of_x, magnitude * direction_of_y }
Code: Select all
{ magnitude * direction_of_x / magnitude, magnitude * direction_of_y / magnitude }
Code: Select all
{ magnitude * direction_of_x, magnitude * direction_of_y }
Code: Select all
{ magnitude * direction_of_x / magnitude, magnitude * direction_of_y / magnitude }
Code: Select all
distance = sqrt(dx*dx+dy*dy)
the sqrt don't working why?micha wrote: ↑Sat Apr 13, 2013 5:01 pmI'd do it the same way, with two further modification.Chroteus wrote:Code: Select all
function fish_move(dt) dx = (player.x - fish.x) * (fish.speed * dt) dy = (player.y - fish.y) * (fish.speed * dt) fish.x = fish.x + (dx * dt) fish.y = fish.y + (dy * dt) end
First, you put in dt twice. You only need it once for each coordinate. So either remove it in the first two lines or in the second two lines.
Second, you probably noticed, that the fish is faster, if it is further away from the player. This is because, dx and dy both become larger, the larger the distance is.
Maybe this is what you want. If not, here is how to fix it. Divide the distance vector by its length:Code: Select all
function fish_move(dt) dx = player.x - fish.x dy = player.y - fish.y distance = sqrt(dx*dx+dy*dy) fish.x = fish.x + (dx / distance * fish.speed * dt) fish.y = fish.y + (dy / distance * fish.speed * dt) end
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests