00001 #include "KX_PhysicsController.h"
00002 #include "SG_Spatial.h"
00003 #include "SM_Scene.h"
00004 #include "KX_GameObject.h"
00005
00006
00007 void KX_PhysicsController::do_me()
00008 {
00009 if (m_suspendDynamics)
00010 return;
00011
00012 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00013 spobj->SetLocalPosition(m_sumoObj->getPosition());
00014 spobj->SetLocalOrientation(m_sumoObj->getOrientation());
00015
00016
00017
00018
00019
00020 }
00021
00022 void KX_PhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
00023 {
00024 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00025 if (m_sumoObj && spobj)
00026 {
00027 MT_Point3 newpos = m_sumoObj->getPosition();
00028 newpos += (local ? spobj->GetWorldOrientation() * dloc : dloc);
00029 m_sumoObj->setPosition(newpos);
00030 }
00031 }
00032 void KX_PhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
00033 {
00034
00035 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00036 if (m_sumoObj && spobj)
00037 {
00038 m_sumoObj->setOrientation(m_sumoObj->getOrientation()*(local ?
00039 drot.getRotation() : (spobj->GetWorldOrientation().inverse() * drot * spobj->GetWorldOrientation())).getRotation());
00040 }
00041
00042 }
00043
00044 void KX_PhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
00045 {
00046 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00047 if (m_sumoObj && spobj)
00048 {
00049 m_sumoObj->setLinearVelocity(local ?
00050 spobj->GetWorldOrientation() * lin_vel :
00051 lin_vel);
00052 }
00053 }
00054
00055 void KX_PhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
00056 {
00057 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00058 if (m_sumoObj && spobj)
00059 {
00060 m_sumoObj->setAngularVelocity(local ?
00061 spobj->GetWorldOrientation() * ang_vel :
00062 ang_vel);
00063
00064 }
00065 }
00066
00067 MT_Vector3 KX_PhysicsController::GetVelocity(const MT_Point3& pos)
00068 {
00069 MT_Vector3 result(0,0,0);
00070 if (m_sumoObj)
00071 {
00072
00073 result = m_sumoObj->getVelocity(pos);
00074 }
00075 return result;
00076 }
00077
00078 MT_Vector3 KX_PhysicsController::GetLinearVelocity()
00079 {
00080 MT_Vector3 result(0,0,0);
00081 if (m_sumoObj)
00082 {
00083
00084 result = m_sumoObj->getLinearVelocity();
00085 }
00086 return result;
00087
00088 }
00089 void KX_PhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
00090 {
00091 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00092 if (m_sumoObj && spobj)
00093 {
00094 m_sumoObj->applyTorque(local ?
00095 spobj->GetWorldOrientation() * torque :
00096 torque);
00097
00098 }
00099 }
00100
00101 void KX_PhysicsController::ApplyForce(const MT_Vector3& force,bool local)
00102 {
00103 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00104 if (m_sumoObj && spobj)
00105 {
00106 m_sumoObj->applyCenterForce(local ?
00107 spobj->GetWorldOrientation() * force :
00108 force);
00109
00110 }
00111 }
00112
00113 bool KX_PhysicsController::Update(double time)
00114 {
00115
00116 return false;
00117
00118 }
00119
00120 void KX_PhysicsController::SetSimulatedTime(double time)
00121 {
00122
00123 }
00124
00125 void KX_PhysicsController::SetSumoTransform()
00126 {
00127 if (m_sumoObj)
00128 {
00129 SG_Spatial* spobj = (SG_Spatial*) m_pObject;
00130 m_sumoObj->setPosition(spobj->GetWorldPosition());
00131 if (m_bDyna)
00132 {
00133 m_sumoObj->setScaling(MT_Vector3(1,1,1));
00134 } else
00135 {
00136 m_sumoObj->setScaling(spobj->GetWorldScaling());
00137 }
00138 m_sumoObj->setOrientation(spobj->GetWorldOrientation().getRotation());
00139 m_sumoObj->calcXform();
00140 }
00141 }
00142
00143 void KX_PhysicsController::SuspendDynamics()
00144 {
00145 m_suspendDynamics=true;
00146
00147 if (m_sumoObj)
00148 {
00149 m_sumoObj->suspendDynamics();
00150 m_sumoObj->setLinearVelocity(MT_Vector3(0,0,0));
00151 m_sumoObj->setAngularVelocity(MT_Vector3(0,0,0));
00152 m_sumoObj->calcXform();
00153 }
00154 }
00155
00156 void KX_PhysicsController::RestoreDynamics()
00157 {
00158 m_suspendDynamics=false;
00159
00160 if (m_sumoObj)
00161 {
00162 m_sumoObj->restoreDynamics();
00163 }
00164 }
00165
00166 SG_Controller* KX_PhysicsController::GetReplica()
00167 {
00168
00169 KX_PhysicsController* physicsreplica = new KX_PhysicsController(*this);
00170
00171
00172
00173
00174 if (m_sumoObj)
00175 {
00176 DT_ShapeHandle shape = DT_Sphere(0.0);
00177 physicsreplica->m_sumoObj = new SM_Object(shape, m_sumoObj->getMaterialProps(),
00178 m_sumoObj->getShapeProps(), NULL);
00179 double radius = m_sumoObj->getMargin();
00180 physicsreplica->m_sumoObj->setMargin(m_sumoObj->getMargin());
00181 physicsreplica->m_sumoObj->setPosition(m_sumoObj->getPosition());
00182 physicsreplica->m_sumoObj->setOrientation(m_sumoObj->getOrientation());
00183 m_sumoScene->add(* (physicsreplica->m_sumoObj));
00184 }
00185
00186 physicsreplica->m_pObject = NULL;
00187
00188
00189 return physicsreplica;
00190 }
00191
00192
00193 void KX_PhysicsController::SetObject (SG_IObject* object)
00194 {
00195 SG_Controller::SetObject(object);
00196
00197
00198 KX_GameObject* gameobj = (KX_GameObject*) object->GetSGClientObject();
00199 gameobj->m_pPhysicsController = this;
00200 m_sumoObj->setClientObject(gameobj->m_pClient_info);
00201
00202 m_sumoObj->registerCallback(*this);
00203
00204 }