## Moving around in a virtual city

So far we have developed several techniques that we can use to make our games look interesting. Techniques that will allow us to add spectacular visual effects which will definitely attract the user's attention. We don't always need to use sophisticated effects though. Simple things can make our games look impressive if they are applied after good and thorough design.

Take for instance a simple box. If you apply a simple texture that looks like wood then all you have is a simple box. On the other hand if you apply the image of a building you actually draw a building. In the sample that comes with this article I use some simple textures I created with image pieces I found over the internet. The effect is dramatic. The selection of colors for the road and the sidewalks along with the textures for the buildings have added a lot of realism in the final program.

This sample was intended to show the use of quaternions for rotation. So it is a first person tourist walking around in a virtual city. He can change the direction he is going or simply turn his head to the left, to the right, up and down to look around as he walks by.

All the calculations required to make him change direction or turn his head are performed using quaternions. Simple collision detection between the person and the buildings is performed. He can step on the sidewalks but cannot go through the buildings. The collision detection is very simple and I would like to note that it is a good practice to create simplified version of complex algorithms that are adequate for our games. This little sample can be used as a basis for a driving game, replacing the walking man with a car.

At the moment our man can turn left or right using the 'Z' and 'X' keys, turn his head left and right with the 'N' and 'M' keys and up down with the 'H' and 'B' keys and return to front looking position with the 'J' key.

This sample was created as a sequel to the 'Quaternions' article and to make it a little more interesting I added this virtual walk. As you can see in the theoretical article the quaternions are very simple. The code is in the 'clf_quaternion.h' file and all the functions are inline for speed. You can construct a quaternion with any data you may have plus all the operators are provided for easy use.

class clf_quaternion { public: float n; // number (scalar) part clf_vector3D v; // vector part: v.x, v.y, v.z clf_quaternion(void); clf_quaternion(float scalar, float i, float j, float k); float magnitude(void); clf_vector3D get_vector(void); float get_scalar(void); clf_quaternion operator+=(clf_quaternion q); clf_quaternion operator-=(clf_quaternion q); clf_quaternion operator*=(float s); clf_quaternion operator/=(float s); clf_quaternion operator~(void) const; }; inline clf_quaternion operator+(clf_quaternion q1, clf_quaternion q2); inline clf_quaternion operator-(clf_quaternion q1, clf_quaternion q2); inline clf_quaternion operator*(clf_quaternion q1, clf_quaternion q2); inline clf_quaternion operator*(clf_quaternion q, float s); inline clf_quaternion operator*(float s, clf_quaternion q); inline clf_quaternion operator*(clf_quaternion q, clf_vector3D v); inline clf_quaternion operator*(clf_vector3D v, clf_quaternion q); inline clf_quaternion operator/(clf_quaternion q, float s); inline float q_get_angle(clf_quaternion q); inline clf_vector3D q_get_axis(clf_quaternion q); inline clf_quaternion q_rotate(clf_quaternion q1, clf_quaternion q2); inline clf_vector3D qv_rotate(clf_quaternion q, clf_vector3D v); inline clf_quaternion make_q_from_euler_angles(float x, float y, float z); inline clf_vector3D make_euler_angles_from_q(clf_quaternion q);

You can download the sample and walk around in the virtual city.