## Vector Math - :)

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Vector Math - :)

I'm a college dropout, so didn't get too far in math ( made it through HS trig, but that was about 20 years ago ). I bring this up to point out the fact I have no clue what the formulas you used to calculate point line collision, I barely understand the y-intercept formula, and the solution to HW 3 about figuring mx+b and wx+p or whatever, WHOOSH!! right over ma'head lol.

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

// Once you have determined collision, you can check if ball is traveling toward or away and do the reflection if needed.
{
Vec2 segHat = sedNormal.GetNormalized();
if( segHat * ball.GetVel() < 0.f )
{
Reflect( ball.GetVel(), segHat );
}
}
``````
I know that there are three normalize vector operations here thus three sqrt calls, which are slower than your method which I believe only requires one.

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.``````
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

krautersuppe
Posts: 80
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

### Re: Advance HW 3 question

albinopapa wrote: 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.
Yes, visualisation is what vectors are all about. Your definition of dot product is false. One thing that helps with visualisation better(at least in 2D ) is always using cartesian coordinates and drawing vectors (at least somewhat)respectively to scale.
I can cover your doubts and questions point by point tomorrow.
Kraut

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Advance HW 3 question

Mmm, not sure what you mean by false.

If neither vector is normalized, then really the only information I know of that can be gained is whether they point in generally the same or opposite direction or perfectly perpendicular.

Using the dot product on the same vector ( a dot a ) will give square length of a ||a||^2.

Using the dot product with one of the vectors normalized ( a dot bHat ) will give you the length of how much one vector will cast a shadow on another vector ( this was a description I found on the internet ). Otherwise stated, the projection of vector a onto bHat...I probably f'ed up my illustration and math on this one.

Using the dot product on two normalized vectors (aHat dot bHat ) will give the cosine between the vectors.

Using the dot product between A and aHat (a dot aHat ) will give you the length of A ||a||.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

krautersuppe
Posts: 80
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

### Re: Advance HW 3 question

albinopapa wrote:Mmm, not sure what you mean by false.
Well, dot product churns out a number that you can use to assess how 2 vectors scale to each other.
There are several special cases that you described where and how you can use that. However you can
derive these equations yourself if you understand how it works.
Vectors are written component wise, according to coordinate axis that one has chosen. You can split any vector in a sum of two other vectors(who are perpendicular to each other) and that's what you are using in your drawing and what chili discusses from here on onwards.
To be honest i don't like the "hat" notation as it might lead to confusion about what is vector and what is number(scalar).
Also i wanted to point out that vector component formula for dot product is derived using this knowledge(although chili didn't mention it in the video).
Are you able to follow and understand the annotation that i wrote?
Attachments
component_dot_product_proof.jpg
Kraut

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Advance HW 3 question

Damn son!
To be honest i don't like the "hat" notation as it might lead to confusion about what is vector and what is number(scalar).
Not sure what you meant by this, but assuming people have seen chili's video and/or have learned about vectors, they should realize "^" above a vector is that vector normalized. Unless you are referring to me labeling things "xHat", in which case there wasn't much I can do, since I'm not sure you can put a caret above letters in this forum.

I hate quadratic equations, you made the dot product look confusing as hell lol. It took me awhile to understand a few things about what you wrote. I'm not confused about the dot product as you can see, there's quite a few cases I know and while I don't understand how they work the way they do, I was able to reason about how to use them based on how I've seen it used in tutorials for things like lighting, ray tracing and experimentation.

The formula I got confused about and am still unsure of is the point/line intersection formula chili found on the internet. I don't like to do "monkey see-monkey do" especially when it comes to math, but at the same time, I find it very difficult to understand sites like Wikipedia or Wolfram Alpha because they describe math in terms that only someone who has been to university or fresh out of high school. My high school education it seems was sub-par. It has been over 20 years so I don't remember all the details of equations and formulas, my teacher never really explained what the equations were used for, so I only retained enough at the time to finish the homework and pass a test.

I'm not stupid, or so I'd like to think, but I'm not a genius either. I need a bit of hand holding and it's tough to find sites that explain math concepts to someone like me, and I know I'm not alone in this.

Anyway, thanks for your time and explanation. I'm going to say I probably missed something about your post and explanation, I'll keep looking it over and see if anything clicks.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Advance HW 3 question

Oh, when I say visualize, I don't mean derivations and expanding formulas.

As I mentioned in earlier post about the lasers and stopping at edge of screen, I had visual objects that I could see, which allowed me to realize that if the ship is 400 pixels from the left edge and the slope was -1.33 I could just multiply this slope by 400 and would get the Y distance of where the laser was going to end from the firing position. So for that situation, if I had to make a formula:

Calculating the end point for the left edge of screen
m = -1.33;
distanceToEdge = 400;
firePos = ( 400, 550 );
laserEndPos = firePos + ( -distanceToEdge, distanceToEdge * m );

distToEdgeX = 0 - 400 = -400;
distToEdgeY = 400 * -1.33 = -533;
laserEndPos.x = 400 + (-400) = 0;
laserEndPos.y = 550 + (-533) = 17;

I did end up using ( y = mx + b ), but it might not be obvious, or at least it wasn't to me. This is what I'm referring to by reasoning and visualizing. I play with the hard numbers first, then MAYBE I can deal in the abstract world of variables.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

krautersuppe
Posts: 80
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

### Re: Advance HW 3 question

I was just asking whether you are able to understand everything in the attachment. It combines several basic formulas so I posted it to check your understanding of the topic. If something isn't clear feel free to ask.
Line equation in vector maths has not that much to do with y = mx + b. I will come to that later.
Kraut

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Advance HW 3 question

Ah, ok.

Ok, so you used the FOIL method on the dot product: AxBx + AxBy + AyBx + AyBy. Then using the fact that using the dot product on the same normalized vector results in 1, while using the dot product on two perpendicular vectors results in 0 to cancel out the outer and inner expansions.

It really doesn't help me visualize anything though. You are relying on derivations of or proofs, which to me just starts making less sense the longer it goes on. If I could read/understand that shit, I'd be able to understand Wikipedia's and Wolfram Alpha's explanations.

In order to understand your expansions, you have to know that dotting xHat with xHat will equal 1 and xHat with yHat will equal 0. I say this because your expansion has dot products in the expansion of the dot product. Personally, if I had attempted the FOIL method on the dot product, I wouldn't have thought to also FOIL the dot products of the X and Y base vectors along with the vector components.

Long story short, I understand sections of what you did, but I'm guessing I'm not seeing the big picture; all the components as a whole. What you have done doesn't help me visualize or reason about how the dot product works. Some are good with abstractions such as letters and symbols, while others need a spatial visual model. While watching chili's more recent videos, my mind went blank while he was explaining how to get the reflection vector and the line normal. I think I understood his original video better ( Advance Tutorial 8 ).
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

krautersuppe
Posts: 80
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

### Re: Advance HW 3 question

albinopapa wrote: Long story short, I understand sections of what you did, but I'm guessing I'm not seeing the big picture; all the components as a whole. What you have done doesn't help me visualize or reason about how the dot product works.
Dot product is just a number that you calculate according to the formula( which was developed from observations and trial and error) to then use to calculate projection scaling, angles and so on.

Are you able to follow and understand stuff in this attachment?
Attachments
vectors.jpg
Kraut

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Advance HW 3 question

First off, thanks for taking the time.

I'm not sure about numbers 5) and 7)
In number 5) (P1 - A0) = ||B||cos( theta ), in this case is theta the angle of vector B? I thought dot product is: ||A|| * ||B|| * cos( theta )?

In number 7) ||B||cos( theta ) = A dot B / ||A||, what is this voodoo?

Holy crap, ||B||cos( theta ) theta being the angle between A and B is the same as A dot B / ||A||, who knew?

I had to take it into a spread sheet to see that result.

Ok, so number 5) you say P1 - A0 ( which will result in a vector, = ||B||cos( theta ) which will result in a scalar?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com