[21] | 1 | /* |
---|
| 2 | SimpleScenes_Joints.h |
---|
| 3 | --------------------- |
---|
| 4 | A demo using Ogre and the OgreOde wrapper to demonstrate joints. |
---|
| 5 | */ |
---|
| 6 | #ifndef _SIMPLESCENES_JOINTS_H_ |
---|
| 7 | #define _SIMPLESCENES_JOINTS_H_ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | The joints test extends the base test class |
---|
| 11 | */ |
---|
| 12 | class SimpleScenes_Joints:public SimpleScenes |
---|
| 13 | { |
---|
| 14 | public: |
---|
| 15 | |
---|
| 16 | // Create the scene, just a load of differently hinged doors |
---|
| 17 | SimpleScenes_Joints(OgreOde::World *world) : |
---|
| 18 | SimpleScenes(world) |
---|
| 19 | { |
---|
| 20 | left = right = up = false; |
---|
| 21 | |
---|
| 22 | // Create the player's ball |
---|
| 23 | Entity* entity = _mgr->createEntity("Ball","ball.mesh"); |
---|
| 24 | entity->setNormaliseNormals(true); |
---|
| 25 | entity->setCastShadows(true); |
---|
| 26 | |
---|
| 27 | SceneNode* node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName()); |
---|
| 28 | |
---|
| 29 | node->attachObject(entity); |
---|
| 30 | node->setScale(0.2,0.2,0.2); |
---|
| 31 | node->setPosition(0,1.2,0); |
---|
| 32 | |
---|
| 33 | OgreOde::EntityInformer ei(entity,Matrix4::getScale(node->getScale())); |
---|
| 34 | ball_body = ei.createSingleDynamicSphere(5.0,_world, _space); |
---|
| 35 | _last_node = static_cast<SceneNode*>(ball_body->getParentNode()); |
---|
| 36 | _bodies.push_back(ball_body); |
---|
| 37 | _geoms.push_back(ball_body->getGeometry(0)); |
---|
| 38 | |
---|
| 39 | // Make the ball stay in the X/Y plane |
---|
| 40 | #ifdef OGREODE_PLANARJOINT |
---|
| 41 | OgreOde::PlanarJoint* joint2d = new OgreOde::PlanarJoint(); |
---|
| 42 | joint2d->attach(ball_body); |
---|
| 43 | _joints.push_back(joint2d); |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | // Create the free standing door |
---|
| 47 | entity = _mgr->createEntity("Free_Door","door.mesh"); |
---|
| 48 | entity->setNormaliseNormals(true); |
---|
| 49 | entity->setCastShadows(true); |
---|
| 50 | |
---|
| 51 | node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName()); |
---|
| 52 | |
---|
| 53 | node->attachObject(entity); |
---|
| 54 | node->setPosition(15,4,0); |
---|
| 55 | node->setOrientation(Quaternion(Degree(90),Vector3(0,1,0))); |
---|
| 56 | node->setScale(2,2,2); |
---|
| 57 | |
---|
| 58 | OgreOde::EntityInformer ei1(entity,Matrix4::getScale(node->getScale())); |
---|
| 59 | OgreOde::Body* body = ei1.createSingleDynamicBox(20.0, _world, _space); |
---|
| 60 | _bodies.push_back(body); |
---|
| 61 | _geoms.push_back(body->getGeometry(0)); |
---|
| 62 | |
---|
| 63 | // Create a door hinged at the left hand side |
---|
| 64 | entity = _mgr->createEntity("Left_Door","door.mesh"); |
---|
| 65 | entity->setNormaliseNormals(true); |
---|
| 66 | entity->setCastShadows(true); |
---|
| 67 | |
---|
| 68 | node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName()); |
---|
| 69 | |
---|
| 70 | node->attachObject(entity); |
---|
| 71 | node->setPosition(-10,4.01,1.5); |
---|
| 72 | node->setOrientation(Quaternion(Degree(-90),Vector3(0,1,0))); |
---|
| 73 | node->setScale(2,2,2); |
---|
| 74 | |
---|
| 75 | OgreOde::EntityInformer ei2(entity,Matrix4::getScale(node->getScale())); |
---|
| 76 | body = ei2.createSingleDynamicBox(20.0,_world, _space); |
---|
| 77 | body->setDamping(0,20); |
---|
| 78 | |
---|
| 79 | OgreOde::HingeJoint* joint = new OgreOde::HingeJoint(_world); |
---|
| 80 | joint->attach(body); |
---|
| 81 | joint->setAxis(Vector3::UNIT_Y); |
---|
| 82 | joint->setAnchor(Vector3(-10,4.01,3.5)); |
---|
| 83 | |
---|
| 84 | _bodies.push_back(body); |
---|
| 85 | _geoms.push_back(body->getGeometry(0)); |
---|
| 86 | _joints.push_back(joint); |
---|
| 87 | |
---|
| 88 | // Create a door hinged at the right hand side |
---|
| 89 | entity = _mgr->createEntity("Right_Door","door.mesh"); |
---|
| 90 | entity->setNormaliseNormals(true); |
---|
| 91 | entity->setCastShadows(true); |
---|
| 92 | |
---|
| 93 | node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName()); |
---|
| 94 | |
---|
| 95 | node->attachObject(entity); |
---|
| 96 | node->setPosition(-15,4.01,-1.5); |
---|
| 97 | node->setOrientation(Quaternion(Degree(90),Vector3(0,1,0))); |
---|
| 98 | node->setScale(2,2,2); |
---|
| 99 | |
---|
| 100 | OgreOde::EntityInformer ei3(entity,Matrix4::getScale(node->getScale())); |
---|
| 101 | body = ei3.createSingleDynamicBox(20.0,_world, _space); |
---|
| 102 | body->setDamping(0,20); |
---|
| 103 | |
---|
| 104 | joint = new OgreOde::HingeJoint(_world); |
---|
| 105 | joint->attach(body); |
---|
| 106 | joint->setAxis(Vector3::UNIT_Y); |
---|
| 107 | joint->setAnchor(Vector3(-15,4.01,-3.5)); |
---|
| 108 | |
---|
| 109 | _bodies.push_back(body); |
---|
| 110 | _geoms.push_back(body->getGeometry(0)); |
---|
| 111 | _joints.push_back(joint); |
---|
| 112 | |
---|
| 113 | // Create a door hinged at the top |
---|
| 114 | entity = _mgr->createEntity("Top_Door","door.mesh"); |
---|
| 115 | entity->setNormaliseNormals(true); |
---|
| 116 | entity->setCastShadows(true); |
---|
| 117 | |
---|
| 118 | node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName()); |
---|
| 119 | |
---|
| 120 | node->attachObject(entity); |
---|
| 121 | node->setPosition(10,4.1,0); |
---|
| 122 | node->setOrientation(Quaternion(Degree(90),Vector3(0,1,0))); |
---|
| 123 | node->setScale(2,2,2); |
---|
| 124 | |
---|
| 125 | OgreOde::EntityInformer ei4(entity,Matrix4::getScale(node->getScale())); |
---|
| 126 | body = ei4.createSingleDynamicBox(20.0,_world, _space); |
---|
| 127 | body->setDamping(0,20); |
---|
| 128 | |
---|
| 129 | joint = new OgreOde::HingeJoint(_world); |
---|
| 130 | joint->attach(body); |
---|
| 131 | joint->setAxis(Vector3::UNIT_Z); |
---|
| 132 | joint->setAnchor(Vector3(10,8.1,0)); |
---|
| 133 | |
---|
| 134 | _bodies.push_back(body); |
---|
| 135 | _geoms.push_back(body->getGeometry(0)); |
---|
| 136 | _joints.push_back(joint); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // Standard destructor |
---|
| 140 | virtual ~SimpleScenes_Joints(){} |
---|
| 141 | |
---|
| 142 | // Return our name for the test application to display |
---|
| 143 | virtual const String& getName() |
---|
| 144 | { |
---|
| 145 | static String name = "Test Joints"; |
---|
| 146 | return name; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | // Return a description of the keys that the user can use in this test |
---|
| 150 | virtual const String& getKeys() |
---|
| 151 | { |
---|
| 152 | static String keys = "I - Bounce, J - Left, L - Right"; |
---|
| 153 | return keys; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // Gets called every time the world is stepped, so we can apply forces every time step |
---|
| 157 | virtual void addForcesAndTorques() |
---|
| 158 | { |
---|
| 159 | // Apply a torque about the Z axis to roll the ball left |
---|
| 160 | if(left) |
---|
| 161 | { |
---|
| 162 | ball_body->wake(); |
---|
| 163 | ball_body->addTorque(Vector3(0,0,50)); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // Apply a torque about the Z axis to roll the ball right |
---|
| 167 | if(right) |
---|
| 168 | { |
---|
| 169 | ball_body->wake(); |
---|
| 170 | ball_body->addTorque(Vector3(0,0,-50)); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // Apply an upwards force, or at least opposite to gravity |
---|
| 174 | if(up) |
---|
| 175 | { |
---|
| 176 | ball_body->wake(); |
---|
| 177 | ball_body->addForce(_world->getGravity() * -10.0); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | #if (OGRE_VERSION_MINOR < 4) |
---|
| 182 | // Handle the user's key presses |
---|
| 183 | virtual void frameEnded(Real time,InputReader* input) |
---|
| 184 | { |
---|
| 185 | // Do default key handling |
---|
| 186 | SimpleScenes::frameEnded(time,input); |
---|
| 187 | #else |
---|
| 188 | |
---|
| 189 | virtual void frameEnded(Real time, OIS::Keyboard* input, OIS::Mouse* mouse) |
---|
| 190 | { |
---|
| 191 | // Do default processing |
---|
| 192 | SimpleScenes::frameEnded(time, input, mouse); |
---|
| 193 | #endif |
---|
| 194 | |
---|
| 195 | // Set the key flags so we can apply forces at the right time |
---|
| 196 | left = input->isKeyDown(KC_J); |
---|
| 197 | right = input->isKeyDown(KC_L); |
---|
| 198 | up = input->isKeyDown(KC_I); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | // Set up the collision properties |
---|
| 202 | virtual bool collision(OgreOde::Contact* contact) |
---|
| 203 | { |
---|
| 204 | contact->setBouncyness(0.8); |
---|
| 205 | contact->setCoulombFriction(OgreOde::Utility::Infinity); |
---|
| 206 | contact->setFrictionMode(OgreOde::Contact::Flag_FrictionPyramid); |
---|
| 207 | |
---|
| 208 | return true; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | protected: |
---|
| 212 | OgreOde::Body *ball_body; |
---|
| 213 | bool left,right,up; |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | #endif |
---|