00001 #include <stdio.h>
00002
00003 #include <vrs/lookat.h>
00004 #include <vrs/rotation.h>
00005
00006 #include <vrsode/CollisionBody.h>
00007
00008 #include <random_utils/Macros.h>
00009
00010 #include "ThirdPersonCameraHelper.h"
00011
00012 using namespace VRS;
00013 using namespace vrsode;
00014
00015
00016 namespace random_racer
00017 {
00018
00019 ThirdPersonCameraHelper::ThirdPersonCameraHelper(
00020 SO<LookAt> p_lookAt, SO<CollisionBody> p_hook,
00021 const VRS::Vector& p_initialDirection,
00022 double p_distX, double p_distY, double p_correctY,
00023 bool p_staticY, double p_smoothing, unsigned int p_delay)
00024 {
00025 m_lookAt = p_lookAt;
00026 m_hook = p_hook;
00027 m_distX = p_distX;
00028 m_distY = p_distY;
00029 m_correctY = p_correctY;
00030 m_staticY = p_staticY;
00031 m_delay = p_delay;
00032
00033 m_lastDirection = p_initialDirection;
00034 setSmoothing(p_smoothing);
00035
00036 startTimer(m_delay);
00037 }
00038
00039 void
00040 ThirdPersonCameraHelper::setSmoothing(double p_smoothing)
00041 {
00042 m_smoothing = random_utils::inRange(0.0, p_smoothing, 1.0);
00043 }
00044
00045 ThirdPersonCameraHelper::~ThirdPersonCameraHelper()
00046 {
00047 }
00048
00049 void ThirdPersonCameraHelper::updateLookAt()
00050 {
00051 Vector pos = m_hook->position(), v(m_distX, 0.0, 0.0);
00052
00053
00054 pos[1] += m_correctY;
00055
00056
00057 v = m_hook->rotation() * v;
00058
00059
00060 v[1] = (m_staticY) ? -m_distY : (v[1] - m_distY);
00061
00062
00063 v = v*(1.0-m_smoothing) + m_lastDirection*m_smoothing;
00064
00065
00066 m_lastDirection = v;
00067
00068
00069 m_lookAt->setTo(pos);
00070 m_lookAt->setFrom(pos - v);
00071 }
00072
00073 void
00074 ThirdPersonCameraHelper::setDelay(unsigned int p_msecs)
00075 {
00076 m_delay = p_msecs;
00077 startTimer(p_msecs);
00078 }
00079
00080 void
00081 ThirdPersonCameraHelper::timerTick()
00082 {
00083 updateLookAt();
00084 }
00085
00086 }