For me, one of the things that helps me with a subject ( math really ) is being able to visualize why something works and logically reason about how the formulas correspond to what I'm observing. As an example, the dot product of two vectors can be visualized by drawing a line from the tip of one vector perpendicular to the other. This is how I would have solved the is colliding issue:
Code: Select all
// First, get the vector from P0 -> P1 ( I know I didn't label everything ). Then, get the vector from P0 -> ball position
Vec2 a = P1 - P0;
Vec2 b = ball.GetPos() - P0;
Vec2 bHat = b.GetNormalized();
// Then, get dot product of a and bHat to get the distance from P0 that would be the point on the line where the ball would be if it was on the line segment.
float distToIntersection = a * bHat;
// Normalize A to get the direction of the vector and multiply by the distToIntersection
Vec2 pointOfInterest = a.GetNormalized() * distToIntersection;
// Now get the vector from pointOfInterest -> ball position
Vec2 segNormal = ball.GetPos() - pointOfInterest;
// Here, you don't need the normalized vector to test, just the square distance of the vector and the square radius to determine collision.
float sqDistToBall = sq( segNormal ); // Dot product with self is same as squaring distance
float sqRadius = sq( ball.GetRadius() );
// Once you have determined collision, you can check if ball is traveling toward or away and do the reflection if needed.
if( sqDistToBall < sqRadius )
{
Vec2 segHat = sedNormal.GetNormalized();
if( segHat * ball.GetVel() < 0.f )
{
Reflect( ball.GetVel(), segHat );
}
}
I suppose the question I had, regarding the title of the thread, is: Is there a way to visualize those algorithms ( formulas ) that you posted for point intersection?
As far as the y-intercept formula goes, I can visualize the line on a graph intersecting Y at B and having a slope M, and for every change in X by M, you get Y. I guess it's calculating B that I don't quite understand, then again I subconsciously pushed back against the use of this formula, so I'll have to look into it closer.
As an example, back when I was working on a space shooter, the ship fired lasers that we wanted to stop at the edge of the screen. Without knowing/understanding the y-intercept formula, I was able to accomplish this by just reasoning what I wanted to happen and with a little trial and error. This is what I mean by being able to visualize such a formula. This also helps me recall and rework how to piece it back together if I forget the formula later on.
This I can't visualize lol:
Code: Select all
Vec2 a = p0.y - p1.y;
Vec2 b = p1.x - p0.x;
// Almost looks like the cross product of p0 x p1, but isn't
Vec2 c = ( p0.x * p1.y ) - ( p1.x * p0.y );
// Not sure if it's a coincidence or not, but it I think it is the cross product of p1 x p0 ( difference is sign negation from p0 x p1 )
Using 2D cross product can find the "signed area" of a quadrilateral, but not sure what it's purpose is here.
The calculating a and b are kind of confusing also. Apparently, it's not just the delta or difference between p0 and p1, the signs of those differences play a role here as well.