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.
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 »

Gunroar:Cannon() wrote: Sat Oct 30, 2021 10:47 pm Though the sentence confuses me :? Does it find the origin of a line moving in a certain direction?
Let's say you want to find the point halfway between a and b.

Code: Select all

angl = direction(a.x, a.y, b.x, b.y)
dist = distance(a.x, a.y, b.x, b.y)
halfway.x, halfway.y = transposition(a.x, a.y, angl, dist/2)
transposition means "Move this point in this direction by this distance, and give me the new coordinates."

It tells you where you are going to be, if you start here and go this way that far.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Getting direction from point a to point b

Post by darkfrei »

Xii wrote: Wed Nov 03, 2021 11:49 pm
Gunroar:Cannon() wrote: Sat Oct 30, 2021 10:47 pm Though the sentence confuses me :? Does it find the origin of a line moving in a certain direction?
Let's say you want to find the point halfway between a and b.
Over 9000 times faster:

Code: Select all

halfway.x, halfway.y = b.x-a.x, b.y-a.y
With normalization:

Code: Select all

local dx, dy = b.x-a.x,  b.y-a.y
local length = (dx*dx + dy*dy)^0.5
local nx, ny = dx/length, dy/length 
halfwayX, halfwayY = a.x + 0.5*length*nx, a.y + 0.5*length*ny
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
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 »

darkfrei wrote: Thu Nov 04, 2021 9:23 am Over 9000 times faster:

Code: Select all

halfway.x, halfway.y = b.x-a.x, b.y-a.y
This gives you the difference (distance) on each axis. You cannot get a halfway without dividing by 2 somewhere. It'd be more like this:

Code: Select all

halfway.x, halfway.y = a.x + (b.x - a.x) / 2, a.y + (b.y - a.y) / 2
Anyway that's besides the point. There's always a better way to do any given thing. I was just illustrating how the function works.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting direction from point a to point b

Post by Gunroar:Cannon() »

Oh, so that last one is the best option?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Getting direction from point a to point b

Post by pgimeno »

The best option, for best clarity, is probably to lerp. Lerping is an operation that should be in every programmer's toolbox. Lerp is short for linear interpolation; you pass it the origin value and the destination value, and then a value between 0 (for which it returns the origin) and 1 (for which it returns the destination).

Code: Select all

function lerp(a, b, t)
  return a + (b - a) * t
end
Due to floating point rounding issues, however, it does not always return the destination when passing t = 1; in that case it's better to split it as follows:

Code: Select all

function lerp(a, b, t)
  return t < 0.5 and a + (b - a) * t or b + (a - b) * (1 - t)
end
Never use a formulation like a * (1 - t) + b * t because that's not monotonic, which causes serious issues in some circumstances; see https://math.stackexchange.com/question ... erpolation

Applied to the case at hand:

Code: Select all

halfway.x, halfway.y = lerp(a.x, b.x, 0.5), lerp(a.y, b.y, 0.5)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 50 guests