Common Vector Operations

BlitzMax Forums/BlitzMax Programming/Common Vector Operations

Armitage 1982(Posted 2008) [#1]
Hi

I'm wondering if anybody could explain in simple terms and concrete orientate game examples the most common math operations with vector.

I suspect very efficient optimisation or simplification can be done with operations like : cross product / dot product / Scalar multiplication / Normalization / vector projection / squared length / etc.

2D vector would be enough for me but if there's specific operation for 3D, why not.

Since it's a math question it's very possible there isn't only one usage of those operations.
But most comment usages would be very helpful !

Thanks for any help ;)


slenkar(Posted 2008) [#2]
I use vector code in the archives to get the angle between 2 points


Warpy(Posted 2008) [#3]
Very quickly:

- cross product gets you a vector that is at right-angles to two other vectors. Obviously this only makes sense in 3d. You might want this for working out the normal of a polygon, for the purposes of collision or shading.

- dot product can either get you the angle between two vectors, or lets you 'project' one vector on to another. You might want to do that to constrain an object's movement onto a given line, for example.

-Scalar multiplication is just the mulitplication of every component of a vector by some fixed number. you might multiply a velocity vector by a small number to get a friction effect.

-Normalization gives you a vector of length 1 which is still pointing in the same direction as the original vector. This is used in collision detection, or for all sorts of reasons.



Honestly, you don't need to just memorise these and their uses off by heart. If you come up against something you need and you don't know how to do it, just look up how to do it and you'll learn that way.


Armitage 1982(Posted 2008) [#4]
Thank you very much Warpy and Jeremy !

It's actually the way I'm working with specific behaviour, when I need something I search on the net for answer.
But it's always nice to be aware of those operations.


Grey Alien(Posted 2008) [#5]
great description Warpy!


Walla(Posted 2008) [#6]
I have used cross in '2d' when small projectiles had to impart rotational acceleration on larger fixed masses as they passed, the sign of the cross told we which direction to rotate. I am an idiot though so this may have been wrong.


Warpy(Posted 2008) [#7]
No, that's right.


EOF(Posted 2008) [#8]
> lets you 'project' one vector on to another

That's one the the areas I can't get to grips with. I have seen terms such as, "we need to project vec1 onto vec2 and use the resulting 'normal' to do ... blah"
Other times its, "We project vec1 onto vec2 and use the resulting projection vector to do ... blah"

Example. If the RED and GREEN lines are vectors and the yellow lines represent the vector normals, what would the results be for projecting RED's normal onto GREEN's?



Warpy(Posted 2008) [#9]
I've had an Information Technology Disaster after upgrading ubuntu and my scanner and tablet are knackered, so here's a hand-drawn explanation:



Note: when you project one vector onto another, you pretend they're both starting from the same origin. As normals don't really have a position in space, this isn't an unreasonable leap to make. Remember vectors are only relative to the co-ordinate system you're using them in. Understanding this makes working with vectors a lot easier on the noggin.


Yahfree(Posted 2008) [#10]
here's my yah.tvec2 object/module... uses OOP to represent vectors... was very fun to write: