CS 420/686 Game Engineering

Practice Problems #1

Solutions

Practice Problems #1

Solutions

Assume the following for all problems:

- Use row vectors (except question 10!)
- Use a right-handed coordiate system.
- All models point down the z axis in their object space (with "up" poining along y axis in object space)

- Spaceship 1 has the rotation matrix (in world space):

m _{11}m _{12}m _{13}m _{21}m _{22}m _{23}m _{31}m _{32}m _{33}

And the position (in world space) [p

_{x}, p_{y}, p_{z}]You want to place Spaceship 2 directly in front of Spaceship 1 (in Spaceship 1's reference frame), 20 units away, pointing towards Spaceship 1. Spaceship 1 and spaceship 2 should have the same up vector. Give the rotation matrix (in world space) and the position (in world space) of Spaceship 2

**Solution**Spaceship 2 should share the same up vector as Spaceship 1, but be pointed in the opposite direction. Thus, we just need to invert the x and z axes to get the position:

-m _{11}-m _{12}-m _{13}m _{21}m _{22}m _{23}-m _{31}-m _{32}-m _{33}Note that if we had just inverted the z axis (and not changed the X axis), then we would have not only turned the ship around, but also mirrored it.

The position of ship 2 is just the position of ship 1 plus the distance times the facing vector of ship 1:

[p_{x}, p_{y}, p_{z}] + 20 [m_{31}, m_{32}, m_{33}] - A character in your game has the rotation matrix (in world
space)

m _{11}m _{12}m _{13}m _{21}m _{22}m _{23}m _{31}m _{32}m _{33}

And the position (in world space) [p_{x}, p_{y}, p_{z}]

You wish to place a camera 10 units directly to the right of the character, pointing at the character (that is, looking at the character's right shoulder), with the same up vector as the character.

Give the rotation matrix (in world space) and the position (in world space) of the camera.

**Solution**The up vector of the camera is the same as the up vector of the character, so the first part of the rotation matrix is easy enough:

m _{21}m _{22}m _{23}Next, consider the facing vector of the camera. The camera is pointing at the right shoulder -- in the same direction as the left vector of the character.

m _{21}m _{22}m _{23}m _{11}m _{12}m _{13}At this point, we could use a cross product to get the left vector (up cross facing == left), or we could note that the left vector of the camera is pointing in the opposite direction of the facing vector of the character:

-m _{31}-m _{32}-m _{33}m _{21}m _{22}m _{23}m _{11}m _{12}m _{13}Now, for the position of the camera -- it is just the position of the character + the scalar distance * -(left vector)

[p

_{x}, p_{y}, p_{z}] - 10 [m_{11}, m_{12}, m_{13}] - A character in your game has the rotation matrix (in world
space)

m _{11}m _{12}m _{13}m _{21}m _{22}m _{23}m _{31}m _{32}m _{33}

And the position (in world space) [p_{x}, p_{y}, p_{z}]

The ground is perpendicular to the world up vector. You wish to place a camera 10 units away from the character, along the line you get when you project the x axis to the ground plane. The camera should have the same up vector as as the world, and be pointed at the character. (See diagram) You can assume that [m

_{11,}m_{12, }m_{13}] != [0, 1, 0] and [m_{11,}m_{12, }m_{13}] != [0, -1, 0] (so that x has a non-zero projection to the ground plane).First off, the x vector of the object is:

**v**_{x}= [m_{11,}m_{12, }m_{13}]. If we project this onto the ground plane, we get [m_{11,}0, m_{13}]. Normalizing this vector gives us [m_{11}/d, 0, m_{13}/d], where d = sqrt(m_{11}^{2}+ m_{13}^{2}). If we add 10 * this normalized vector to the original point, we get the position of the camera:Camera position = [p

_{x}, p_{y}, p_{z}] + 10 * [m_{11}/d, 0, m_{13}/d], where d = sqrt(m_{11}^{2}+ m_{13}^{2})Now, let's look at the orientation of the camera. The up vector of the camera is the same as the up vector of the world: [0, 1, 0]. The z vector of the camera is the opposite of the projected X vector: [-m

_{11}/d, 0, -m_{13}/d]. So we have0 1 0 -m _{11}/d0 -m _{13}/dWhat about the x vector of the camera? Well, we have the y and z vectors, and the cross product y × z = x, so x = [0, 1, 0] × [-m

_{11}/d, 0, -m_{13}/d] = [-m_{13}/d, 0, m_{11}/d]. So, we finally have:-m _{13}/d0 m _{11}/d0 1 0 -m _{11}/d0 -m _{13}/d - An object initially has the transformation vector

m _{11}m _{12}m _{13}m _{21}m _{22}m _{23}m _{31}m _{32}m _{33}

after applying a transform M, we are left with:

-m _{11}-m _{12}-m _{13}-m _{31}-m _{32}-m _{33}m _{21}m _{22}m _{23}

Does the transformation M contain a reflection?

**Solution**The transformation inverts an axis (reflection) then inverts another axis (undoing reflection) then inverts a third axis (reflection) then swaps two axes (undoing the reflection). No reflection.

- Recall that quaternions are used to represent displacements
(though they can also be used to represent absolute orientation, as a
displacement from a reference orientation). For each of the
following pairs of quaternions, describe how the displacement for
quaternion 1 differes from the displacement for quaternion 2

- [w, (x, y, z)] and [-w, (x, y, z)]
OK, so this one problably should have been 3rd instead of 1st, Go look at the solutions to the next two problems, and the come back. So, if we negate the vector value, that results in rotating -Θ degrees around the same axis. If we then negate the entire quaternion, we are left with the same displacement (rotating -Θ degrees around the original axis). So, the second quaternion rotates around the same axis as the first quaternion, by the same amount, but in the opposite direction.

- [w, (x, y, z)] and [w, (-x, -y, -z)]
- [w, (x, y, z)] and [-w, (-x, -y, -z)]
- [w, (x, y, z)] and [w, (-x, y, z)]
- [w, (x, y, z)] and [w, (x, -y, z)]
- [w, (x, y, z)] and [w, (x, y, -z)]
- [w, (x, y, z)] and [w, (-x, -y, z)]

We can get from quaternion 1 to quaternion 2 by leaving the angle Θ the same, and inverting the x and y coordinates of the axis we are rotating around. So, if quaternion 1 repersents rotation by Θ degres around a vector n, quaternion 2 represents rotating by Θ degrees around the vector n', which is just n reflected first about the yz-plane, then around the zx-plane

So we have negated the vector portion of the quatenrion, while leaving the real portion alone. So we are rotating by the same angle, but the vector representing the rotation axis has flipped. Thus if the first quaternion represents a rotation of Θ degrees around the axis n, then the second quaternion represents a rotation of -Θ degrees around the same axis n

Recall that negating a quaternion does not change the orientation represented by the quaternion -- so these two quaternions represent the same angular displacement. Looking at it another way, we are both inverting the axis (which would be the same axis in the opposite direction) and also inverting the direction of rotation, leaving the same displacement

We can get from quaternion 1 to quaternion 2 by leaving the angle Θ the same, and inverting the x coordinate of the axis we are rotating around. So, if quaternion 1 repersents rotation by Θ degres around a vector n, quaternion 2 represents rotating by Θ degrees around the vector n', which is just n reflected about the yz-plane.

We can get from quaternion 1 to quaternion 2 by leaving the angle Θ the same, and inverting the y coordinate of the axis we are rotating around. So, if quaternion 1 repersents rotation by Θ degres around a vector n, quaternion 2 represents rotating by Θ degrees around the vector n', which is just n reflected about the zx-plane.

We can get from quaternion 1 to quaternion 2 by leaving the angle Θ the same, and inverting the z coordinate of the axis we are rotating around. So, if quaternion 1 repersents rotation by Θ degres around a vector n, quaternion 2 represents rotating by Θ degrees around the vector n', which is just n reflected about the xy-plane.

- [w, (x, y, z)] and [-w, (x, y, z)]
- Describe the change in orientation described by the
following quaternions:

- [1, (0, 0, 0)]
- [0, (0, 1, 0)]
- [1/sqrt(2), (1/2, 1/2, 0)]

cos (Θ/2) = 1/sqrt(2) ==> Θ/2 = 45 degrees (or π/4 radians), so Θ = 90 degrees (or π/2 radians). Both the x and z components of

**n**have the same positive value (with the z component == 0), so**n**(normalized) must be the vector [1/sqrt(2), 1/sqrt(2), 0]. (Alternate method for calculating**n**: Θ/2 = 45 degrees, so sin(Θ/2) = 1/sqrt(2) So, 1/sqrt(2) ***n**= [1/2, 1/2, 0], so**n**= [1/sqrt(2), 1/sqrt(2), 0].

This is the identity quaternion -- represents rotating 360 degrees around (any) axis, which is equivalent to no rotation at all.

cos (Θ/2) = 0 ==> Θ/2 = 90 degrees (or π/2 radians), so Θ = 180 degrees (or π radians). Since the x and z components of n are both 0, this is then a rotation of 180 degrees (or π radians) around the y axis.

- You have a spaceship whose position and orientation ore defined by the Ogre variables:

Ogre::Vector3 shipPos; Ogre::Quaternion shipOrientation;

The spaceship has an orbiting satelite whose position and orientation (relative to the spaceship) are:

Ogre::Vector3 satelitePosLocal; Ogre::Quaternion sateliteOrientationLocal;

- Give code to calculate the position and orientation of the satelite in world space:
Ogre::Vector3 satelitePosGlobal = ... Ogre::Quaternion sateliteOrientationGlobal = ...

**Solution**

Ogre::Vector3 satelitePosGlobal = shipOrentation * satelitePosLocal + shipPos; Ogre::Quaternion sateliteOrientationGlobal = shipOrientation * sateliteOrentation;

- You have a point in satelite space:
Ogre::Vector3 pointInSateliteSpace;

Give code to transform that point into world space:Ogre::Vector3 pointInWorldSpace =

**Solution**Ogre::Vector3 pointInWorldSpace = shipPosition + shipOrentation * (sateliteOrientationLocal * pointInSateliteSpace + satelitePos)

- You have a point in world space:
Ogre::Vector3 pointInWorldSpace;

Give code to transform that point into satelite space:Ogre::Vector3 pointInSateliteSpace = ...

**Soluton**Ogre::Vector3 pointInSateliteSpace = sateliteOrientation.inverse() * (shipOrentation.inverse() * (pointInWordSpace - shipPosition) - satelitePos)

- Give code to calculate the position and orientation of the satelite in world space:
- Given the following bit of code:

class foo { foo(int a, char *b, char *c) : x(a) { strcpy(y,b); z = new char[strlen(c) + 1]; strcpy(z,c); } int x; char y[10]; char *z; }; class bar { bar(int g, char * h, char *i) : d(g, h, i) { e = new foo(g, h, i); } foo d; foo *e; }; int main() { bar v(3, "hello", "there"); bar *vPtr = new bar(5, "test", "string"); // Point A }

Show the contents of the stack and heap at Point A

- Give the output of the following bit of code (WARNING:
This is tricky. Trickier than you might think ...):

#include

class A { public: void P1() {printf("P1 in A\n");} virtual void P2() {printf("P2 in A\n");} virtual void P3() {printf("P3 in A\n");} }; class B : public A { public: void P1() {printf("P1 in B\n");} void P2() {printf("P2 in B\n");} }; class C : public B { public: void P1() {printf("P1 in C\n");} void P2() {printf("P2 in C\n");} void P3() {printf("P3 in C\n");} }; int main() { A aVar; B bVar; C cVar; A *aPtr = new C(); B *bPtr = new C(); aVar.P1(); aVar.P2(); aVar.P3(); printf("--------\n"); bVar.P1(); bVar.P2(); bVar.P3(); cVar.P1(); cVar.P2(); cVar.P3(); printf("--------\n"); aPtr->P1(); aPtr->P2(); aPtr->P3(); bPtr->P1(); bPtr->P2(); bPtr->P3(); printf("--------\n"); aVar = cVar; aVar.P1(); aVar.P2(); aVar.P3(); printf("--------\n"); bVar = cVar; aPtr = &(bVar); aPtr->P1(); aPtr->P2(); aPtr->P3(); } **Solution**P1 in A P2 in A P3 in A -------- P1 in B P2 in B P3 in A P1 in C P2 in C P3 in C -------- P1 in A P2 in C P3 in C P1 in B P2 in C P3 in C -------- P1 in A P2 in A P3 in A -------- P1 in A P2 in B P3 in A

- You have calcuated a series of transformations for a transforming a vector
v (assuming v is a row vector) to get the result:

r = vABC

However, you now realize that your system uses column vectors instead of row vectors! Give a formula for the equivalent column vector r' (in terms of the column vector v', and the transformation matrices A, B, and C),

r' = C

^{T}B^{T}A^{T}v'