CO1301: Games Concepts
Weeks 17 - 18: Dot Product
Do note that this worksheet introduces a number of new concepts. However, take it steady and you will have no problems with it. Please also note that worksheet cover two weeks.
Exercise 1
· I have provided you with several models and you can choose the two you want to use. You need to have a guard: use the man (Casual_A.x) or the marine (marine.x).
· You also need to the player character: use the woman (sierra.x) or the biker (Bikergirl.x).
· The idea of this exercise is that we want to allow the player to sneak up behind the guard. The guard cannot see the player if the player is behind him.
· Load up a model for the floor plus the player and the guard models.
· Set up the W, A, S and keys to move the player, e.g.
if( myEngine->KeyHeld( Key_W ) )
{
player->MoveLocalZ( kPlayerSpeed );
}
· In each frame you will have to calculate the vector from the guard to the player. In order to do this you take the location of the player and subtract the location of the guard from it.
· In order to calculate the facing vector of a model you have to carry out some work with its matrix. I want to leave matrix work for a few weeks so I've provided you with a function that will calculate the facing vector from a given model:
void facingVector( IModel *model, float &x, float &y, float &z)
{
float matrix[16];
model->GetMatrix( matrix );
x = matrix[8];
y = matrix[9];
z = matrix[10];
}
· Imagine that the model of the guard is called "guard" and we have three floating point variables ready to store the facing vector of the guard:
float x;
float y;
float z;
IModel* guard;
· You would then call the function like this:
facingVector( guard, x, y, z );
· Now write a function that will calculate the dot product of two vectors. The function should return the dot product. The function declaration (or function prototype) looks like this:
float dotProduct( float Vx, float Vy, float Yz, float Wx, float Wy, float Wz );
· The function prototype specifies the name of the function, the parameter list (i.e. the variables inside the round brackets) and the return type. The return type is void if the function does not return a value. When you write the actual function you need to:
o delete the semi-colon
o add curly brackets (braces) and place the body of the code within the curly brackets.
float dotProduct( float Vx, float Vy, float Yz, float Wx, float Wy, float Wz )
{
// body of code here
}
· The function prototype specifies that you need to supply the function with two vectors. The body of the function needs to calculate the dot product of the two vectors. The dot product is calculated using the following formula:
· In order to find out whether the guard can see the player you:
o Use the facingVector() function to find out the facing vector of the guard.
o Calculate the vector from the guard to the player.
o Calculate the dot product between these two vectors.
· Having done these steps you finally apply the following test:
· Let the facing vector (the orientation) of the guard be v.
· Let the vector from the guard to the player be w.
· If v w 0 then the angle is less that 90 degrees, if v w < 0 then the angle is greater that 90 degrees, and if v w = 0 then the angle is exactly 90 degrees. See Van Verth, pp.30-32.
· Therefore if v w < 0 then the player is behind the guard and not visible to the guard.
· For this particular test the length of the vectors doesn't matter so you don't have to normalise the vectors.
CO1301 - Games Concepts / Gareth Bellaby / Weeks 17 & 18 / Page 1