00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- 00002 this file is part of rcssserver3D 00003 Fri May 9 2003 00004 Copyright (C) 2003 Koblenz University 00005 $Id: fpscontroller.cpp,v 1.10 2005/01/04 10:56:51 rollmark Exp $ 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; version 2 of the License. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #include "fpscontroller.h" 00022 #include <oxygen/physicsserver/body.h> 00023 #include <zeitgeist/logserver/logserver.h> 00024 00025 using namespace boost; 00026 using namespace oxygen; 00027 using namespace salt; 00028 00029 FPSController::FPSController() : BodyController() 00030 { 00031 mHAngle = 0.0f; 00032 mVAngle = 0.0f; 00033 mForward = false; 00034 mBackward = false; 00035 mLeft = false; 00036 mRight = false; 00037 mUp = false; 00038 mDown = false; 00039 mAcceleration = 10; 00040 } 00041 00042 FPSController::~FPSController() 00043 { 00044 } 00045 00046 void FPSController::PrePhysicsUpdateInternal(float /*deltaTime*/) 00047 { 00048 if (mBody.get() == 0) 00049 { 00050 return; 00051 } 00052 00053 // determine force direction 00054 Vector3f vec(0.0f,0.0f,0.0f); 00055 00056 if (mForward) vec.y() += 1.0f; 00057 if (mBackward) vec.y() -= 1.0f; 00058 if (mRight) vec.x() += 1.0f; 00059 if (mLeft) vec.x() -= 1.0f; 00060 if (mUp) vec.z() += 1.0f; 00061 if (mDown) vec.z() -= 1.0f; 00062 00063 // constrain angles 00064 if(mVAngle > 88.0f) 00065 { 00066 mVAngle = 88.0f; 00067 } else if(mVAngle<-88.0f) 00068 { 00069 mVAngle = -88.0f; 00070 } 00071 00072 Matrix matrix; 00073 float hAngle = gDegToRad(-mHAngle); 00074 float vAngle = gDegToRad(-mVAngle); 00075 matrix.RotationZ(hAngle); 00076 matrix.RotateX(vAngle); 00077 mBody->SetRotation(matrix); 00078 00079 if (vec.SquareLength() > 0) 00080 { 00081 float force = mBody->GetMass() * mAcceleration; 00082 vec *= force; 00083 00084 Matrix fwd; 00085 fwd.RotationZ(hAngle); 00086 mBody->AddForce(vec.y() * fwd.Up()); 00087 00088 Matrix side; 00089 side.RotationX(vAngle); 00090 mBody->AddForce(vec.x() * fwd.Right()); 00091 00092 mBody->AddForce(vec.z() * Vector3f(0,0,1)); 00093 } 00094 } 00095 00096 void FPSController::AdjustHAngle(const float delta) 00097 { 00098 mHAngle += delta; 00099 } 00100 00101 void FPSController::AdjustVAngle(const float delta) 00102 { 00103 mVAngle += delta; 00104 } 00105 00106 void FPSController::Forward(const bool state) 00107 { 00108 mForward = state; 00109 } 00110 00111 void FPSController::Backward(const bool state) 00112 { 00113 mBackward = state; 00114 } 00115 00116 void FPSController::StrafeLeft(const bool state) 00117 { 00118 mLeft = state; 00119 } 00120 00121 void FPSController::StrafeRight(const bool state) 00122 { 00123 mRight= state; 00124 } 00125 00126 void FPSController::Up(const bool state) 00127 { 00128 mUp = state; 00129 } 00130 00131 void FPSController::Down(const bool state) 00132 { 00133 mDown = state; 00134 } 00135 00136 void FPSController::SetAcceleration(const float accel) 00137 { 00138 mAcceleration = accel; 00139 } 00140 00141 float FPSController::GetAcceleration() const 00142 { 00143 return mAcceleration; 00144 } 00145