7#include <CustomDGRayCastCar.h>
11 RaycastVehicle::Tire::Tire(RaycastVehicle* vehicle, Ogre::Node* node,
const bool steer,
const bool driving,
const int id)
20 m_debug.m_visualDebug = 0;
23 RaycastVehicle::Tire::~Tire()
25 if (m_debug.m_node != 0)
27 if (m_debug.m_node->getParent())
28 m_debug.m_node->getParent()->removeChild(m_debug.m_node);
30 m_debug.m_node->detachAllObjects();
32 delete m_debug.m_visualDebug;
35 m_debug.m_visualDebug = 0;
39 void RaycastVehicle::Tire::showDebug(Ogre::SceneNode* debugRootNode)
41 Body* carBody = m_vehicle->getCarBody();
43 Ogre::Quaternion orient;
45 carBody->getVisualPositionOrientation(pos, orient);
46 dMatrix carMatrix(dQuaternion (orient.w, orient.x, orient.y, orient.z), dVector (pos.x, pos.y, pos.z, 1.0f));
48 CustomDGRayCastCar::Tire& tire = m_vehicle->GetTire(m_tireId);
50 if (m_debug.m_node == 0)
52 std::ostringstream oss;
53 oss <<
"__OgreNewt__Debugger__Lines__" << &tire <<
"__";
54 m_debug.m_visualDebug =
new Ogre::ManualObject(oss.str());
55 m_debug.m_node = debugRootNode->createChildSceneNode();
58 debug.buildDebugObjectFromCollision(m_debug.m_visualDebug, Ogre::ColourValue(0, 0, 1, 1), tire.m_shape);
60 m_debug.m_node->attachObject(m_debug.m_visualDebug);
63 dMatrix tireBaseMatrix (m_vehicle->CalculateSuspensionMatrix(m_tireId, tire.m_posit) * m_vehicle->GetChassisMatrixLocal() * carMatrix);
66 m_debug.m_node->setPosition(pos);
67 m_debug.m_node->setOrientation(orient);
68 if (!m_debug.m_node->getParent())
69 debugRootNode->addChild(m_debug.m_node);
72 void RaycastVehicle::Tire::getTireMatrix(dMatrix &outMatrix)
74 outMatrix = m_vehicle->CalculateTireMatrix(m_tireId);
77 void RaycastVehicle::Tire::updateNodePosition()
80 getTireMatrix(tireMat);
82 Ogre::Quaternion quat;
87 Ogre::Node* m_nodeParent = m_node->getParent();
89 m_nodeParent->_update(
true,
true);
91 Ogre::Vector3 gscale = m_nodeParent->_getDerivedScale();
93 if (gscale.x == 0.0f || gscale.y == 0.0f || gscale.z == 0.0f)
96 m_node->setPosition((m_nodeParent->_getDerivedOrientation().Inverse() * (pos - m_nodeParent->_getDerivedPosition()) / gscale));
97 m_node->setOrientation((m_nodeParent->_getDerivedOrientation().Inverse() * quat));
110 for (
unsigned int i = 0; i < m_lTires.size(); i ++)
112 Tire* tire = m_lTires.at(i);
126 if (index < m_lTires.size())
127 m_lTires.at(index)->getTireMatrix(outMatrix);
132 for (
unsigned int i = 0; i < m_lTires.size(); i ++)
134 Tire* tire = m_lTires.at(i);
135 if (tire->isDriving())
136 SetTireTorque(tire->getId(), torque);
142 for (
unsigned int i = 0; i < m_lTires.size(); i ++)
144 Tire* tire = m_lTires.at(i);
146 SetTireSteerAngleForce(tire->getId(), angle, 0.5f);
152 for (
unsigned int i = 0; i < m_lTires.size(); i ++)
154 Tire* tire = m_lTires.at(i);
155 SetTireBrake(tire->getId(), brakeTorque);
159 void RaycastVehicle::addTire(Ogre::Node* node,
const Ogre::Real mass,
const Ogre::Real radius,
const Ogre::Real width,
const Ogre::Real friction,
const Ogre::Real susLength,
const Ogre::Real susSpring,
const Ogre::Real susShock,
const bool steer,
const bool driving)
163 if (GetTiresCount() > 16)
166 Ogre::Quaternion quat = node->_getDerivedOrientation();
167 Ogre::Vector3 pos = node->_getDerivedPosition();
171 AddSingleSuspensionTire(nodeMat, mass, radius, width, friction, susLength, susSpring, susShock, 1);
172 int tireId = GetTiresCount() - 1;
174 Tire* ntire =
new Tire(
this, node, steer, driving, tireId);
195 m_lTires.push_back(ntire);
200 if (index < m_lTires.size())
201 return m_lTires[index];
206 void RaycastVehicle::setTireTransformCallback(
OgreNewt::Body* carBody)
208 for (
unsigned int i = 0; i < m_lTires.size(); i ++)
210 Tire* tire = m_lTires.at(i);
211 tire->updateNodePosition();
217 for (
unsigned int i = 0; i < m_lTires.size(); i ++)
219 Tire* tire = m_lTires.at(i);
220 tire->showDebug(debugRootNode);
226 if (index < m_lTires.size())
228 CustomDGRayCastCar::Tire& tire = GetTire(m_lTires.at(index)->getId());
229 return (tire.m_tireIsOnAir == 1) ? true :
false;
237 if (index < m_lTires.size())
240 CustomDGRayCastCar::Tire& tire = GetTire(m_lTires.at(index)->getId());
241 omega = tire.m_angularVelocity;
252 for (
unsigned int i = 0; i < m_lTires.size() && (index == -1); i ++)
254 Tire* tire = m_lTires.at(i);
255 if (tire->getNode() == node)
main class for all Rigid Bodies in the system.
void setNodeUpdateJointNotify(NodeUpdateNotifyCallback callback)
void setNodeUpdateNotify(NodeUpdateNotifyCallback callback)
set a custom node update notify.
For viewing the Newton rigid bodies visually.
void ApplySteering(dFloat angle)
void ApplyTorque(dFloat torque)
RaycastVehicle::Tire * getTire(unsigned int index)
RaycastVehicle(const dMatrix carMatrix, OgreNewt::Body *carBody)
constructor
void getTireMatrix(unsigned int index, dMatrix &outMatrix)
void addTire(Ogre::Node *node, const Ogre::Real mass, const Ogre::Real radius, const Ogre::Real width, const Ogre::Real friction, const Ogre::Real susLength, const Ogre::Real susSpring, const Ogre::Real susShock, const bool steer, const bool driving)
Ogre::Real getTireAngularVelocity(unsigned int index)
void ApplyBrake(dFloat brakeTorque)
int getTireIndexByNode(Ogre::Node *node)
bool getTireOnAir(unsigned int index)
OgreNewt::Body * getCarBody()
virtual ~RaycastVehicle()
destructor
void showDebugData(Ogre::SceneNode *debugRootNode)
show joint visual debugging data
_OgreNewtExport void MatrixToQuatPos(const dFloat *matrix, Ogre::Quaternion &quat, Ogre::Vector3 &pos)
Take a Newton matrix and create a Quaternion + Position_vector.
_OgreNewtExport void QuatPosToMatrix(const Ogre::Quaternion &quat, const Ogre::Vector3 &pos, dFloat *matrix)
Take a Quaternion and Position Matrix and create a Newton-happy float matrix!