Getting direction from point a to point b

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
BICstudios
Prole
Posts: 1
Joined: Sat Oct 09, 2021 8:47 am

Getting direction from point a to point b

Post 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. :nyu:
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Getting direction from point a to point b

Post 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)
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting direction from point a to point b

Post 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
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting direction from point a to point b

Post by Gunroar:Cannon() »

What does transposition do?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Getting direction from point a to point b

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Getting direction from point a to point b

Post by pgimeno »

It's unfortunately too frequent to see people normalize by calculating the sine and cosine of the arctangent.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Getting direction from point a to point b

Post 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*
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting direction from point a to point b

Post 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. :rofl:

Though the sentence confuses me :? Does it find the origin of a line moving in a certain direction?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Getting direction from point a to point b

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Getting direction from point a to point b

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], zingo and 3 guests