PyT is my method of using pythagorean theorem for obtaining speeds/magnitudes of vectors AND distances between points. I usually define an "origin" so that I can do the former, and simply plug in the two points if I'm after the later.
In this application, PyT only handles the V2 type, but in another of my projects it does both
R2 and
R3 vectors by accepting a third parameter telling it which to work on.
SELECT CASE var
'find distance/magnitude between 2D or 3D points PyT
= _HYPOT(var1.pX
- var2.pX
, var1.pY
- var2.pY
) PyT
= _HYPOT(_HYPOT(var1.pX
- var2.pX
, var1.pY
- var2.pY
), var1.pZ
- var2.pZ
)
In some speed testing I've done, I've found the _HYPOT command to be as fast or faster than more traditional methods. I'm thinking of trying to optimize that some more, by ditching the SELECT CASE part.
On the Dot Product topic, I only became aware of it a few months ago and was shocked to discover that it is an indispensable tool for game design. Getting at this sort of programming problem without it is like changing a water pump without wrenches. You might be able to use other tools, but the job will be exponentially harder.
Dot Product is a way of multiplying two vectors together which results in a non-vector number (scalar). The result gives one the ability to determine how much of one vector is "projected" onto another. In our case it is the ability to obtain the portion of red ball's force that is applied directly at cyan ball and what is directed toward red balls exit motion, and of course the same applied from cyan to red and cyan's exit.
[ You are not allowed to view this attachment ]
You needn't worry about the trig formula in the picture, that's mainly for obtaining a dot product when theta is a known quantity, which we're not concerned with in this topic. It might also be apparent, by the picture, that dot product is also closely related to the pythagorean theorem.
In ball collision we are primarily concerned with how much of the unit normal and unit tangent vectors are applied by a collision, which dot product supplies quite well.
Dot Product simply takes the components of two vectors, multiplies them together and sums the results. Example, two vectors
A and
B, dotted together are:
A dot
B = A.x * B.x + A.y * B.y '
and that doesn't get much simpler for what can be done with it...and if you're dealing in 3D:
A dot
B = A.x * B.x + A.y * B.y + A.z * B.z
Which is exactly what SUB VecDot does in my code, for our 2D representation. The order doesn't matter, but it is good to convert one of the vectors you're projecting into a unit vector, if vector projection is your main purpose. That's what my SUB VecNorm does to the "strike" (aka unit normal), and it's orthogonal (aka unit tangent). BTW, you were correct, I didn't need different strikes & orthogonals for each ball. One was sufficient.
The number that dot product returns will also tell you how the vectors are oriented to each other. A negative dot product indicates vectors angled away from each other (obtuse), a dot product of 0 indicates vectors that are perpendicular, and a positive dot product are vectors that subtend an acute angle. The closest thing to a mathematical multi-tool that I've seen so far. Once you learn it, you'll be looking for places to use it.
This guy does a pretty good job of presenting Dot Product:
&t=31s