Page 1 of 2
Getting direction from point a to point b
Posted: Sat Oct 09, 2021 9:37 am
by BICstudios
Hello,
I encountered a problem while making a space shooter game. I need the spaceship to point to the mouse. As always, I searched for the solution on the internet. I found some from this forum, but none are working for me. It's just I'm the one copying it wrong or it's outdated. I would like the solution to be this function.
Code: Select all
function getDir(x1, y1, x2, y2):
-- Codes here
Hope you can help me, thanks for reading.
Re: Getting direction from point a to point b
Posted: Sat Oct 09, 2021 1:08 pm
by veethree
What you’re looking for is the angle between two points.
For that you need the atan2 function.
Code: Select all
angle = math.atan2(y2 - y1, x2 - x1)
Re: Getting direction from point a to point b
Posted: Fri Oct 29, 2021 7:36 pm
by Xii
The three most commonly needed functions for me.
Code: Select all
-- returns the direction from point x1,y1 to x2,y2 (in radians)
function direction(x1, y1, x2, y2)
return math.atan2(y2-y1, x2-x1)
end
-- returns the distance between points x1,y1 and x2,y2
function distance(x1, y1, x2, y2)
return math.sqrt(((x2-x1)^2)+((y2-y1)^2))
end
-- returns the point located at x,y moved in direction by distance
function transposition(x, y, direction, distance)
return distance*math.cos(direction)+x, distance*math.sin(direction)+y
end
Re: Getting direction from point a to point b
Posted: Fri Oct 29, 2021 9:04 pm
by Gunroar:Cannon()
What does transposition do?
Re: Getting direction from point a to point b
Posted: Fri Oct 29, 2021 9:10 pm
by darkfrei
Also you are need the normalization:
Code: Select all
-- player position:
local playerX, playerY = 100,200
-- mouse position:
local mouseX, mouseY = 315, 419
local dx = mouseX-playerX -- delta X
local dy = mouseY-playerY -- delta Y
local distance = (dx*dx+dy*dy)^0.5 -- the distance between player and mouse position
local nx, ny = dx/distance, dy/distance -- normal vector from player to mouse
--so, if your bullet has velocity
bulletV = 200
-- then the update is just
bulletX = bulletX + dt*bulletV*nx
bulletY = bulletY + dt*bulletV*ny
-- and it flies exactly between them with exactly this velocity
Re: Getting direction from point a to point b
Posted: Sat Oct 30, 2021 1:06 am
by pgimeno
It's unfortunately too frequent to see people normalize by calculating the sine and cosine of the arctangent.
Re: Getting direction from point a to point b
Posted: Sat Oct 30, 2021 2:19 pm
by GVovkiv
pgimeno wrote: ↑Sat Oct 30, 2021 1:06 am
It's unfortunately too frequent to see people normalize by calculating the sine and cosine of the arctangent.
*grumbling intensifies*
Re: Getting direction from point a to point b
Posted: Sat Oct 30, 2021 10:47 pm
by Gunroar:Cannon()
Because I searched it on Google and got AI related stuff.
Edit: Oh, I see.
Code: Select all
-- returns the point located at x,y moved in direction by distance
I browse in mobile so I'm sorry, it's easy for me to miss small details like this.
Though the sentence confuses me
Does it find the origin of a line moving in a certain direction?
Re: Getting direction from point a to point b
Posted: Sun Oct 31, 2021 9:50 am
by darkfrei
Gunroar:Cannon() wrote: ↑Sat Oct 30, 2021 10:47 pm
Does it find the origin of a line moving in a certain direction?
You can notice the abs(dx) and abs(dy) and if it's more than one step before, then it goes away from the target. Remove the particle or do what you need.
Re: Getting direction from point a to point b
Posted: Sun Oct 31, 2021 10:46 am
by pgimeno
If the direction is a unit vector instead of an angle, as it should, transposition can be as simple as:
Code: Select all
function transposition(x, y, direction_x, direction_y, distance)
return distance*direction_x+x, distance*direction_y+y
end
In fact, it's intuitive enough for not needing a function for it, you can just place the formulas inline where needed. IMO the function would be counter-productive clarity wise.
You would find direction by normalizing the difference between target and origin:
Code: Select all
function normalize(x, y)
local length = distance(0, 0, x, y)
if length == 0 then
return 1, 0
end
length = 1 / length
return x * length, y * length
end
function direction(x1, y1, x2, y2)
return normalize(x2 - x1, y2 - y1)
end
That saves a sine and cosine every time you need the direction vector.
You only need atan2 because unfortunately, Löve doesn't accept a direction vector as input for the rotation argument in love.draw & co.