31.01.2014, 14:48
Hello. For anyone who has not studied euclidean vectors yet or simply doesn't know about them.
EDIT: This is about mathematical vectors and NOT vector sequence containers. Their implementation in PAWN can be found here.
Index
So that's all! Vectors are an important part of game designing. Also, designing stuff to use vectors is difficult on computers. Probably needs actually pen and paper-ing stuff and then programming. So respect Kalcor for giving us great functions because he's not only got to be a good programmer but also a good mathematician. Though, a good coder should be a good mathematician.
If there are any mistakes, do tell.
Thank you.
EDIT: This is about mathematical vectors and NOT vector sequence containers. Their implementation in PAWN can be found here.
Index
1) What are vectors?
(Magnitude is a word meaning length in geometrical sense)2) How are they represented?
Vectors are directional scalars. In layman terms, a scalar is nothing but pure magnitude without any direction.
Vectors are quantities with a defined direction. They help us, a lot, in defining the angles and inclinations of a quantity and they are a major part of mathematics.
Example - Temperature is a scalar. We know that it's 37 ° (magnitude) in the day. But do we know in what direction is the 37 ° in? No, it just is.
The exact opposite of that is a vector. Consider velocity. We know a car is going straight (direction) with a magnitude of 54 km/hr.
You can find out more about them, on this page.
Vectors are represented in the cartesian coordinate system. (or the polar system)3) Use in SA-MP
This picture is the most simple picture of the three axes.
It shows us three axes, namely X, Y and Z axes with the help of which we can pinpoint ANY spot in 3D space. A vector is represented using unit vectors.
Unit vectors, namely i, j & k, are vectors in the direction of the X, Y & Z axes respectively of magnitude 1 unit.
A standard vector can be shown by -
vec = ai + bj + ck
A vector's magnitude is -.......... ( 1 )Code:SquareRoot( (a * a) + (b * b) + (c * c) )
Using them, I can show a vector like:
v = 3i + 4j + 5k
means that this vector is 3 units along the X axis, 4 units along the Y axis and 5 along the Z.
u = -v
means that the vector 'u' is completely opposite in direction to 'v'.
Vectors are used in SA-MP a lot without even you realising you are using them. A lot of mathematics, you are saved from in SA-MP because there is no need for handling unit vectors in SA-MP and their dot/cross products, etc.4) Using vectors in SA-MP
The GTA: SA map uses blueberry as the origin - it has the co-ordinates as (0,0,0). Hence most of the times you give invalid co-ordinates or don't - or something buggy happens, you spawn there. (or fall there)
Player positions are represented by vectors in SA-MP. Suppose a player stands at (4, 6, -1) on the map.
GetPlayerPos() would return his vector logically - but it infact embeds the variables with 4, 6 and -1 respectively.
Logically, it looks like -The resultant 'PlayerPos' vector would be a vector whose endpoint will be on (4, 6, -1). However, due to absence of a lot of maths in SA-MP, you don't have to worry about that. Just pass the variables and get the position.Code:PlayerPos = 4i + 6j - 1k.
The closest you come to using a vector is this - I am sure you all know this -where fX, fY and fZ are the player's co-ordinates.Code:SquareRoot( (fX * fX) + (fY * fY) + (fZ * fZ) )
ORwhere vX, vY and vZ are vehicle velocity vectors.Code:SquareRoot( (vX * vX) + (fY * fY) + (fZ * fZ) )
You get the magnitude of the PlayerPos vector here. Refer to (1).
By magnitude - the result is the LENGTH of the vector from origin or SIMPLY the distance of the player in unit system.
Two functions I am going to cover in here are5) General mathematics regarding vectors1) GetPlayerLastShotVectors() is a function in layman terms, that gets you the position vector of the place where the player last shot his bullet. It solely gets the position relative to the GTA: SA map unlike OnPlayerWeaponShot which gets you co-ordinates in relation to four different things. (vehicles, etc)
- GetPlayerLastShotVectors(..)
- GetPlayerCameraFrontVector(..)
Examples - You can use it to create a checkpoint where ever you shoot.
Also, you can be very imaginative with this, create things likepawn Code:public OnPlayerWeaponShot(playerid, ....)
{
new Float: X, Float: Y, Float: Z;
GetPlayerLastShotVectors( playerid, X, Y, Z );
// Using mapandreas to get the CORRECT Z co-ordinate.
MapAndreas_FindZ_For2DCoord(X, Y, Z);
CreateDynamicCP(X, Y, Z, ...);
return 1;
}2) GetPlayerCameraFrontVector(..) is a very powerful function. It gives a vector 'v' relative to your POSITION of magnitude unit length in the direction of the player facing angle.
- Teleport guns
- Gravity guns
- Paintball guns
When the vector received is added to GetPlayerCameraPos(..) then you get the ACTUAL position of the 'v' in relation to the GTA - SA map. This map may explain it.
Example usage -Create a NPC, and then detect if a player's looking at it.
Using psuedo - code!
So there you are! Many useful things you can do with it!pawn Code:new NPC = createNpc(...);
SetNPCPos(2, 3, 5);
CreateDynamicSphere(2, 3, 5, /* size of sphere */, 10);
public OneSecondTimer(..)
{
new Float: x, Float: y, Float: z,
Float: cX, Float: cY, Float: cZ;
GetPlayerCameraPos(playerid, x, y, z );
GetPlayerCameraFrontVector(playerid, cX, cY, cZ);
x += cX;
y += cY;
z += cZ;
// x y z now contain co-ords of point where player is looking
if( IsPointInRangeOf( x, y, z, 2, 3, 5 ) )
{
// Player is looking at the NPC
}
return 1;
}
1) Magnitude of a vector
v = ai + bj + ck2) Two vectors' cross product resultant is a vector perpendicular to the original vector.Code:is √ (a*a) + (b*b) + (c*c)
3) Dot product gives you a single number (psuedovector - too long to explain here, see wikipedia)
4) Vectors can be used to denote things like velocity, acceleration or position, etc.
5) Difference of two vectors gives a vector joining the end points of the original two vectors.
6) Angle between two vectors -
Can be used in scenarios like getting angle between two player's angle's and then creating an arrow pointing towards the opposite player.Code:a) Find magnitude of each vector (Let the magnitudes be M1 & M2) b) Get dot product of the vectors (Let the dot product be 'D') c) cos θ = D/M1 * M2 d) Use acos to get the angle.
7) More to come.
So that's all! Vectors are an important part of game designing. Also, designing stuff to use vectors is difficult on computers. Probably needs actually pen and paper-ing stuff and then programming. So respect Kalcor for giving us great functions because he's not only got to be a good programmer but also a good mathematician. Though, a good coder should be a good mathematician.
If there are any mistakes, do tell.
Thank you.