Gamedev Maths: distance from point to line

preview_player
Показать описание
In this video we determine an equation for the shortest distance between some point C, and the line passing through points A and B.
The equation is: dst = abs((C.x-A.x) * (-B.y+A.y) + (C.y-A.y) * (B.x-A.x)) / sqrt((-B.y+A.y)^2 + (B.x-A.x)^2)

We also see how this equation can be modified to determine if two points lie on the same side of a line.

Рекомендации по теме
Комментарии
Автор


I know this topic is a bit out of the blue, but it's one of several maths reference videos I'll be making to supplement future series. One series I currently have in mind is Delaunay Triangulation, so you'll probably be seeing a lot of triangle-related stuff in the near future.

SebastianLague
Автор

Always a delight to find you've uploaded a new video, Sebastian!

walterh
Автор

algebra? Dont mind if I do.
I am loving these, it's math education on point with game programming. Very useful, highly desired. (Just to figure out a way to let people know they want to see this, because anyone in game development wants to know this if they're serious to any extent.)

dabartos
Автор

Thanks for this!
You should do more math stuff, very useful.
I'd like to know how to find the intersection between a line and a vector, like a laser against a wall.

PINKiller
Автор

This is a great explanation of distance. I know all the info in this video but I've had to chase it down multiple other places. It's nice to see simple and clearly written algebra and trig.

FlareGunDebate
Автор

I imagine this will be extremely useful for more advanced AI. The first instinct I got after watching this was to make my AI for an enemy that has only close combat capabilities to see which direction and what speed a player is moving, figure out where they'll end up in the time the enemy could reach the player's current position and plot out a target point that would be the closest on that line to intercept the player on the way to the calculated player target point.

Or for a simpler version, have the AI look at where the player is, where the player needs to go and use this formula to figure out where the enemy needs to get to in order to intercept the player on the way.

morphman
Автор

This is EXACTLY the thing I was looking for!!! After trying to find an equation myself for like two hours, I also couldn't find anything useful anywhere on Google. But this hits the spot

lauchsalat
Автор

I like how you explain all the formulas and steps taken to reach the resulting value, although perhaps it's easier to name the similarities and differences using functions like Vector3.Distance, Vector3.DOT and Vector3.Cross, or even examples of the same cases on the video with those functions

kuteninja
Автор

Your channel is my most favorite channel.

Nastomeya
Автор

sebastian i love your videos but you definitely over complicated this. you were thorough in explaining the math behind this but i feel as though you could have added a little more to explain this a lot easier. When ever i teach programming math i often go over what you explained somewhat quickly and then use practical examples to help people understand the math that i just went over.
I found the easiest way to do this is to just explain everything in simple vectors and in pseudo code ( vector for direction = endpoint - startpoint, DOT product is the result of multiplying to vectors magnitude ( DOTPRODUCT( v1(2, 0, 0), v2(3, 0, 0) = 6.0f)), etc). just some constructive criticism, still love your content!

jamiesacco
Автор

or just use the cross product for the abs((C.x-A.x) * (-B.y+A.y) + (C.y-A.y) * (B.x-A.x)) part

hagusen
Автор

Looking forward to more episodes on gamedev maths!

KonstantinMagnus
Автор

Me: *Asks self a question*
_Two days later_
Sebastian: *uploads video answering question in perfect detail*

CJBurkey
Автор

I've learnt so much from you, thank you Sebastian!

jamesjones
Автор

Inverse square root next, with Quake's algorithm.

victornaut
Автор

Amazing, as much as I was unable to do maths when I was young due to ADHD, I once got induced into a "higherStateOfMind" and had this insight about maths and this is the exact same problem and solution it taught me except in words I could understand. Amazing how much biochemistry affects the ability to learn.

theGreaterAwareness
Автор

Im gona save ths for a year later, or two before i can comprehend it all.

jonhaasnoler
Автор

If you were to categorize the current math in the video, what category would it be categorized in ? I am asking this because I do not understand the content, therefore I would like to learn the topic from the beginning

noneofyourbusiness
Автор

I've literally had to solve a problem like this recently!

School trigonometry was a long time ago for me =/

AnimFlynny
Автор

Oh no, I wanted to find the distance from C to the line defined by point A, _normal_ B

wolfboyft