Vector functions by ichaelm19th Feb 2007 15:59
|
---|
Summary Some functions that work with vectors (2.0) Description When programming complicated physics into a game, you may notice that, unlike computers, physics uses vectors (direction and magnitude) instead of normal x and y values. The following functions work with vectors. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com `===============VECTOR FUNCTONS=============== `When programming complicated physics into a game, you may notice that, unlike computers, physics uses vectors (direction and magnitude) instead of cartesian (x and y) values. The following functions work with vectors. `Note: Positive y values are down, just like in DB. `VECTOR MAGNITUDE `This tells you the magnitude of a vector based on x and y coordinates. function vector magnitude#(x#,y#) dist#=abs(((x#^2)+(y#^2))^0.5) endfunction dist# `VECTOR DIRECTION `This tells you the direction of a vector based on x and y coordinates. function vector direction#(x#,y#) y#=y#*-1 direction#=atanfull(x#,y#) endfunction direction# `X COORDINATE `This tells you the x coordinate of a vector based on magnitude and direction. function x coordinate#(magnitude#,direction#) x#=magnitude#*(sin(direction#)) endfunction x# `Y COORDINATE `This tells you the y coordinate of a vector based on magnitude and direction. function y coordinate#(magnitude#,direction#) y#=magnitude#*(cos(direction#)) y#=y#*-1 endfunction y# `ANGLED VECTOR MAGNITUDE `This one is a little more complicated. It takes a vector and finds how much force (magnitude) it has in a different direction. The best way to understand it is to look at the code. function angled vector magnitude#(magnitude#,direction#,newdirection#) newmagnitude#=magnitude#*cos(newdirection#-direction#) endfunction newmagnitude# `MAGNITUDE OF ADDED VECTORS `Self-explanatory. function magnitude of added vectors#(magnitude1#,direction1#,magnitude2#,direction2#) x1#=x coordinate#(magnitude1#,direction1#) y1#=y coordinate#(magnitude1#,direction1#) x2#=x coordinate#(magnitude2#,direction2#) y2#=y coordinate#(magnitude2#,direction2#) x#=x1#+x2# y#=y1#+y2# magnitude#=vector magnitude#(x#,y#) endfunction magnitude# `DIRECTION OF ADDED VECTORS `Self-explanatory. function direction of added vectors#(magnitude1#,direction1#,magnitude2#,direction2#) x1#=x coordinate#(magnitude1#,direction1#) y1#=y coordinate#(magnitude1#,direction1#) x2#=x coordinate#(magnitude2#,direction2#) y2#=y coordinate#(magnitude2#,direction2#) x#=x1#+x2# y#=y1#+y2# direction#=vector direction#(x#,y#) endfunction direction# |