Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

/Users/blackie/Documents/myRepository/phobosengine-vc2005/phobosengine/phobosengine/Camera.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "Camera.h"
00004 
00005 #include "mathutils.h"
00006 #include "Renderer.h"
00007 #include "Sphere.h"
00008 
00009 #include <math.h>
00010 
00011 
00012 namespace pge {
00013         //****************************************************************************
00014         //
00015         //
00016         //
00017         //****************************************************************************
00018         Camera::Camera(void) {
00019                 m_keyTurn = 0.1f;
00020                 m_turnSens = 0.1f;
00021                 m_xyMinAngle = -88.0f;
00022                 m_xyMaxAngle = 88.0f;
00023                 m_xzAngle = 0.0f;
00024                 m_xyAngle = 0.0f;
00025                 m_direction = Vector3f(0.0f, 0.0f, -1.0f);
00026                 m_speedUp = 100.0f;
00027                 m_slowDown = 0.1f;
00028                 m_mass = 1.0f;
00029                 m_force = Vector3f();
00030                 m_velocity = Vector3f();
00031 
00032                 m_boundingSphere = new Sphere(Vector3f(0.0f, 10.0f, 0.0f), 1.0f);
00033         }
00034 
00035 
00036         //****************************************************************************
00037         //
00038         //
00039         //
00040         //****************************************************************************
00041         Camera::~Camera(void) {
00042         }
00043 
00044 
00045         //****************************************************************************
00046         //
00047         //
00048         //
00049         //****************************************************************************
00050         void Camera::timer(unsigned int delay) {
00051                 updatePhysics(delay);
00052         }
00053 
00054 
00055         //****************************************************************************
00056         //
00057         //
00058         //
00059         //****************************************************************************
00060         Vector3f Camera::getPosition(void) {
00061                 return m_boundingSphere->m_center;
00062         }
00063 
00064 
00065         //****************************************************************************
00066         //
00067         //
00068         //
00069         //****************************************************************************
00070         void Camera::setPosition(Vector3f position) {
00071                 m_boundingSphere->m_center = position;
00072         }
00073 
00074 
00075         //****************************************************************************
00076         //
00077         //
00078         //
00079         //****************************************************************************
00080         void Camera::setXZAngle(float xzAngle) {
00081                 m_xzAngle = xzAngle;
00082         }
00083 
00084 
00085         //****************************************************************************
00086         //
00087         //
00088         //
00089         //****************************************************************************
00090         void Camera::setXYAngle(float xyAngle) {
00091                 m_xyAngle = xyAngle;
00092         }
00093 
00094 
00095         //****************************************************************************
00096         //
00097         //
00098         //
00099         //****************************************************************************
00100         void Camera::setDirection(Vector3f direction) {
00101                 m_direction = direction;
00102         }
00103 
00104 
00105         //****************************************************************************
00106         //
00107         //
00108         //
00109         //****************************************************************************
00110         void Camera::forward(void) {
00111                 m_force = m_force.add(m_direction.multiply(m_speedUp));
00112         }
00113 
00114 
00115         //****************************************************************************
00116         //
00117         //
00118         //
00119         //****************************************************************************
00120         void Camera::backward(void) {
00121                 m_force = m_force.add(m_direction.multiply(-1.0f * m_speedUp));
00122         }
00123 
00124 
00125         //****************************************************************************
00126         //
00127         //
00128         //
00129         //****************************************************************************
00130         void Camera::turnLeft(int delta) {
00131                 if(delta == -1) {
00132                         m_xzAngle += m_keyTurn;
00133                 } else {
00134                         m_xzAngle += (m_turnSens * (float)delta);
00135                 }
00136                 updateDirection();
00137         }
00138 
00139 
00140         //****************************************************************************
00141         //
00142         //
00143         //
00144         //****************************************************************************
00145         void Camera::turnRight(int delta) {
00146                 if(delta == -1) {
00147                         m_xzAngle -= m_keyTurn;
00148                 } else {
00149                         m_xzAngle -= (m_turnSens * (float)delta);
00150                 }
00151                 updateDirection();
00152         }
00153 
00154 
00155         //****************************************************************************
00156         //
00157         //
00158         //
00159         //****************************************************************************
00160         void Camera::turnUp(int delta) {
00161                 if(m_xyAngle < m_xyMaxAngle) {
00162                         // If delta is -1 we have a keystroke.
00163                         if(delta == -1) {
00164                                 m_xyAngle += m_keyTurn;
00165                         } else {
00166                                 m_xyAngle += (m_turnSens * (float)delta);
00167                         }
00168                 }
00169                 updateDirection();
00170         }
00171 
00172 
00173         //****************************************************************************
00174         //
00175         //
00176         //
00177         //****************************************************************************
00178         void Camera::turnDown(int delta) {
00179                 if(m_xyAngle > m_xyMinAngle) {
00180                         // If delta is -1 we have a keystroke.
00181                         if(delta == -1) {
00182                                 m_xyAngle -= m_keyTurn;
00183                         } else {
00184                                 m_xyAngle -= (m_turnSens * (float)delta);
00185                         }
00186                 }
00187                 updateDirection();
00188         }
00189 
00190 
00191         //****************************************************************************
00192         //
00193         //
00194         //
00195         //****************************************************************************
00196         void Camera::up(void) {
00197                 Vector3f force = Vector3f(0.0f, 1.0f, 0.0f);
00198 
00199                 force = force.multiply(m_speedUp);
00200                 m_force = m_force.add(force);
00201         }
00202 
00203 
00204         //****************************************************************************
00205         //
00206         //
00207         //
00208         //****************************************************************************
00209         void Camera::down(void) {
00210                 Vector3f force = Vector3f(0.0f, -1.0f, 0.0f);
00211 
00212                 force = force.multiply(m_speedUp);
00213                 m_force = m_force.add(force);
00214         }
00215 
00216 
00217         //****************************************************************************
00218         //
00219         //
00220         //
00221         //****************************************************************************
00222         void Camera::slideLeft(void) {
00223                 Vector3f force = Vector3f(0.0f, 0.0f, 0.0f);
00224                 float x;
00225                 float z;
00226 
00227                 x = (float)sin((90.0f + m_xzAngle) * PIOVER180);
00228                 z = (float)cos((90.0f + m_xzAngle) * PIOVER180);
00229 
00230                 force.m_v[0] = x;
00231                 force.m_v[2] = z;
00232                 force.normalize();
00233                 force = force.multiply(m_speedUp);
00234 
00235                 m_force = m_force.add(force);
00236         }
00237 
00238 
00239         //****************************************************************************
00240         //
00241         //
00242         //
00243         //****************************************************************************
00244         void Camera::slideRight(void) {
00245                 Vector3f force = Vector3f(0.0f, 0.0f, 0.0f);
00246                 float x;
00247                 float z;
00248 
00249                 x = (float)sin((270.0f + m_xzAngle) * PIOVER180);
00250                 z = (float)cos((270.0f + m_xzAngle) * PIOVER180);
00251 
00252                 force.m_v[0] = x;
00253                 force.m_v[2] = z;
00254                 force.normalize();
00255                 force = force.multiply(m_speedUp);
00256 
00257                 m_force = m_force.add(force);
00258         }
00259 
00260 
00261         //****************************************************************************
00262         //
00263         //
00264         //
00265         //****************************************************************************
00266         void Camera::applyCameraView(void) {
00267                 Vector3f view = m_boundingSphere->m_center.add(m_direction);
00268 
00269 
00270                 gluLookAt(m_boundingSphere->m_center.m_v[0], m_boundingSphere->m_center.m_v[1],
00271                         m_boundingSphere->m_center.m_v[2], view.m_v[0],
00272                         view.m_v[1], view.m_v[2], 0.0f, 1.0f, 0.0f);
00273         }
00274 
00275 
00276         //****************************************************************************
00277         //
00278         //
00279         //
00280         //****************************************************************************
00281         void Camera::applyCameraRotation(void) {
00282                 renderer::rotate(-m_xyAngle, 1.0f, 0.0f, 0.0f);
00283                 renderer::rotate(180.0f - m_xzAngle, 0.0f, 1.0f, 0.0f);
00284         }
00285 
00286 
00287         //****************************************************************************
00288         //
00289         //
00290         //
00291         //****************************************************************************
00292         void Camera::applyCameraPosition(void) {
00293                 renderer::translate(-m_boundingSphere->m_center.m_v[0], -m_boundingSphere->m_center.m_v[1],
00294                         -m_boundingSphere->m_center.m_v[2]);
00295         }
00296 
00297         
00298         //****************************************************************************
00299         //
00300         //
00301         //
00302         //****************************************************************************
00303         void Camera::revertCameraView(void) {
00304                 renderer::rotate(-(180.0f - m_xzAngle), 0.0f, 1.0f, 0.0f);
00305                 renderer::rotate(m_xyAngle, 1.0f, 0.0f, 0.0f);
00306                 renderer::translate(m_boundingSphere->m_center.m_v[0], m_boundingSphere->m_center.m_v[1], m_boundingSphere->m_center.m_v[2]);
00307         }
00308 
00309 
00310         //****************************************************************************
00311         //
00312         //
00313         //
00314         //****************************************************************************
00315         Sphere* Camera::getBoundingSphere(void) {
00316                 return m_boundingSphere;
00317         }
00318 
00319 
00320         //****************************************************************************
00321         //
00322         //
00323         //
00324         //****************************************************************************
00325         void Camera::updateDirection(void) {
00326                 float x;
00327                 float y;
00328                 float z;
00329                 x = (float)sin(mathutils::degreeToRadian(m_xzAngle));
00330                 y = (float)sin(mathutils::degreeToRadian(m_xyAngle));
00331                 z = (float)cos(mathutils::degreeToRadian(m_xzAngle));
00332 
00333                 m_direction.m_v[0] = x;
00334                 m_direction.m_v[1] = y;
00335                 m_direction.m_v[2] = z;
00336         }
00337 
00338 
00339         //****************************************************************************
00340         //
00341         //
00342         //
00343         //****************************************************************************
00344         void Camera::updatePhysics(unsigned int delay) {
00345                 //
00346                 // Variables
00347                 //
00348                 float deltaTime;
00349 
00350                 deltaTime = (float) delay / 1000.0f;
00351 
00352                 // Calculate new velocity
00353                 m_velocity = m_velocity.add((m_force.divide(m_mass)).multiply(deltaTime));
00354 
00355                 // Apply velocity to position
00356                 m_boundingSphere->m_center = m_boundingSphere->m_center.add(m_velocity.multiply(deltaTime));
00357 
00358                 // Reset force.
00359                 m_force.m_v[0] = 0.0f;
00360                 m_force.m_v[1] = 0.0f;
00361                 m_force.m_v[2] = 0.0f;
00362 
00363                 // Make the object loose velocity when there is no force added.
00364                 // We do this by subtracting the velocity vector multiplied by a constant
00365                 // smaller than 1.0f, so we do not subtract the full vector but only a smaller one.
00366                 m_velocity = m_velocity.subtract(m_velocity.multiply(m_slowDown));
00367         }
00368 };

Generated on Mon Oct 16 12:08:10 2006 for Phobosengine by doxygen 1.3.4