From d8a69e6abeea2034c17d84ef74009b137faa06cc Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 30 Mar 2011 22:30:35 +0200 Subject: Initial commit. --- scene.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scene.cpp (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp new file mode 100644 index 0000000..47b76bf --- /dev/null +++ b/scene.cpp @@ -0,0 +1,46 @@ +#include "scene.h" + +#include + +#include + +#define inrange(a, b, c) ((a) >= (b) && (a) <= (c)) + +void Scene::lookat() { + /* calculate cartesian coordinates for the center vector where yaw is Φ and pitch is θ + * x = cos Φ sin θ + * y = cos θ + * z = sin Φ sin θ + */ + Vector3 center(sinf(pitch) * cosf(yaw), cosf(pitch), sinf(pitch) * sinf(yaw)); + center += pos; + center.y += 1; + //Vector3 up(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch)); + Vector3 up(-cosf(pitch) * cosf(yaw), sinf(pitch), -cosf(pitch) * sinf(yaw)); + gluLookAt(pos.x, pos.y+1, pos.z, + center.x, center.y, center.z, + up.x, up.y, up.z); +} + +void Scene::move(float forward, float right, int steps) { + Vector2 dir; + dir.x += forward*cosf(yaw); + dir.y += forward*sinf(yaw); + + dir.x += right*cosf(yaw+M_PI_2); + dir.y += right*sinf(yaw+M_PI_2); + + // ensure that the vector length is 1.0 + dir /= dir.length(); + dir *= 0.005; + dir *= steps; + + float x = pos.x + dir.x; + //if(inrange(x, -2, 2)) + pos.x = x; + float z = pos.z + dir.y; + //if(inrange(z, -2, 2)) + pos.z = z; + //pos.x += dir.x; + //pos.z += dir.y; +} -- cgit v1.2.3