SO3Engine
OgreNewt_PlayerController.cpp
Go to the documentation of this file.
7#include "CustomPlayerControllerManager.h"
8
9/*
10namespace OgreNewt
11{
12
13 PlayerController::PlayerController(OgreNewt::Body * child, Ogre::Real stairHeight, Ogre::Real kinematicCushion)
14 :Joint()
15 {
16 dMatrix globalFrame (dGetIdentityMatrix());
17 globalFrame.m_front = dVector (0.0f, 1.0f, 0.0f, 0.0f); // up direction in global Space
18 globalFrame.m_up = dVector (0.0f, 0.0f, -1.0f, 0.0f); // ogre front dir is the -z direction
19 globalFrame.m_right = globalFrame.m_front * globalFrame.m_up; // strafing direction in global Space
20
21 // create a newton player controller
22 CustomPlayerController* controller;
23 controller = new CustomPlayerController (globalFrame, child->getNewtonBody(), stairHeight, kinematicCushion);
24 SetSupportJoint(controller);
25 }
26
27 PlayerController::~PlayerController()
28 {
29
30 for (std::vector<DebugInfo>::iterator it (m_debugInfo.begin()); it != m_debugInfo.end(); it++) {
31 DebugInfo& info = (*it);
32
33 if (info.m_node->getParent()) {
34 info.m_node->getParent()->removeChild(info.m_node);
35 }
36 info.m_node->detachAllObjects();
37
38 // delete info.m_node;
39 delete info.m_visualDebug;
40
41 info.m_node = NULL;
42 info.m_visualDebug = NULL;
43 }
44 m_debugInfo.clear();
45 }
46
47 void PlayerController::getVelocity(Ogre::Real& forwardSpeed, Ogre::Real& sideSpeed, Ogre::Radian& heading) const
48 {
49 dFloat dir;
50
51 dir = 0.0f;
52 sideSpeed = 0.0f;
53 forwardSpeed = 0.0f;
54 CustomPlayerController* joint = (CustomPlayerController*) m_joint;
55 if (joint) {
56 joint->GetVelocity (forwardSpeed, sideSpeed, dir);
57 }
58
59 heading = dir;
60 }
61
62 void PlayerController::setVelocity(Ogre::Real forwardSpeed, Ogre::Real sideSpeed, Ogre::Radian heading)
63 {
64 CustomPlayerController* joint = (CustomPlayerController*) m_joint;
65 if (joint) {
66 joint->SetVelocity (forwardSpeed, sideSpeed, heading.valueRadians());
67 }
68 }
69
71 Ogre::Real PlayerController::getPlayerHeight() const
72 {
73 Ogre::Real height = 0.0f;
74 CustomPlayerController* joint = (CustomPlayerController*) m_joint;
75 if (joint) {
76 height = joint->GetPlayerHeight();
77 }
78 return height;
79 }
80
81
82 void PlayerController::showDebugData(Ogre::SceneNode* debugRootNode)
83 {
84 Body* playerBody = getBody0();
85 CustomPlayerController* joint = (CustomPlayerController*) m_joint;
86
87 Ogre::Vector3 pos;
88 Ogre::Quaternion orient;
89
90 // playerBody->getPositionOrientation(pos, orient);
91 playerBody->getVisualPositionOrientation (pos, orient);
92 dMatrix matrix (dQuaternion (orient.w, orient.x, orient.y, orient.z), dVector (pos.x, pos.y, pos.z, 1.0f));
93
94 if (!m_debugInfo.size()) {
95 DebugInfo newInfo;
96 std::ostringstream oss;
97 Ogre::Vector3 pos;
98 Ogre::Quaternion orient;
99
100 OgreNewt::Debugger& debug(playerBody->getWorld()->getDebugger());
101
102 oss << "__OgreNewt__Debugger__Lines__" << joint->GetSensorShape() << "__";
103 newInfo.m_visualDebug = new Ogre::ManualObject(oss.str());
104 newInfo.m_node = debugRootNode->createChildSceneNode();
105 newInfo.m_playershape = (void*) joint->GetSensorShape();
106
107 OgreNewt::Collision sensorCollision (joint->GetSensorShape(), playerBody->getWorld());
108 debug.buildDebugObjectFromCollision(newInfo.m_visualDebug, Ogre::ColourValue(1, 0, 0, 1), sensorCollision);
109
110 newInfo.m_node->attachObject(newInfo.m_visualDebug);
111
112 // append this debug info to the array
113 m_debugInfo.push_back(newInfo);
114
115
116 oss << "__OgreNewt__Debugger__Lines__" << joint->GetStairStepShape() << "__";
117 newInfo.m_visualDebug = new Ogre::ManualObject(oss.str());
118 newInfo.m_node = debugRootNode->createChildSceneNode();
119 newInfo.m_playershape = (void*) joint->GetSensorShape();
120
121 OgreNewt::Collision stairCollision (joint->GetStairStepShape(), playerBody->getWorld());
122 debug.buildDebugObjectFromCollision(newInfo.m_visualDebug, Ogre::ColourValue(1, 1, 0, 1), stairCollision);
123
124 newInfo.m_node->attachObject(newInfo.m_visualDebug);
125
126 // append this debug info to the array
127 m_debugInfo.push_back(newInfo);
128 }
129
130
131 DebugInfo& shapeInfo = m_debugInfo[0];
132 shapeInfo.m_node->setPosition(pos);
133 shapeInfo.m_node->setOrientation (orient);
134 if (!shapeInfo.m_node->getParent()) {
135 debugRootNode->addChild(shapeInfo.m_node);
136 }
137
138 pos.y += joint->GetPlayerStairHeight();
139 DebugInfo& stairInfo = m_debugInfo[1];
140 stairInfo.m_node->setPosition(pos);
141 stairInfo.m_node->setOrientation (orient);
142 if (!stairInfo.m_node->getParent()) {
143 debugRootNode->addChild(stairInfo.m_node);
144 }
145 }
146
147} // end NAMESPACE OgreNewt
148
149*/