Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogreode/demos/SimpleScenes/include/SimpleScenes_Buggy.h @ 21

Last change on this file since 21 was 21, checked in by nicolasc, 16 years ago

added ogreode and Colladaplugin

File size: 6.1 KB
Line 
1/*
2SimpleScenes_Buggy.h
3-------------------
4A reimplementation of the ODE test Buggy application using
5the OgreOde wrapper for Ogre
6*/
7#ifndef _SIMPLESCENES_BUGGY_H_
8#define _SIMPLESCENES_BUGGY_H_
9
10#include "OgreOde_Prefab.h"
11
12//------------------------------------------------------------------------------------------------
13static const String carNames[] = {
14        "Jeep",
15        "JeepSway",
16        "Subaru"
17};
18static const String carFileNames[] = {
19        "jeep.ogreode",
20        "jeep.ogreode",
21        "subaru.ogreode"
22};
23static int sSelectedCar = 1;
24static int maxNumCar = 3;
25
26/*
27Buggy test extends from the base test class
28*/
29class SimpleScenes_Buggy:public SimpleScenes
30{
31public:
32        //------------------------------------------------------------------------------------------------
33        // Constructor also needs to create the scene
34    SimpleScenes_Buggy(OgreOde::World *world) : 
35      SimpleScenes(world),
36          vehicle(0)
37        {
38                // Create the vehicle from the config file
39                changeCar();
40       
41                // Move the vehicle
42                vehicle->setPosition(Vector3(0,0.82898,0));
43
44                // Initially (i.e. in the config file) it's rear wheel drive
45                _drive = 'R';
46                setInfoText(carNames[sSelectedCar] + "Rear wheel drive");
47
48                // Create a box to jump over, the visual version
49                Entity* entity = _mgr->createEntity("Jump", "crate.mesh");
50                entity->setNormaliseNormals(true);
51                entity->setCastShadows(true);
52
53                SceneNode* node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName());
54                node->attachObject(entity);
55                node->setPosition(Vector3(0,0.3,-5));
56                node->setOrientation(Quaternion(Radian(0.4),Vector3(1,0,0)));
57                node->setScale(0.3,0.1,0.4);
58
59                // Create the physical version (just static geometry, it can't move so
60                // it doesn't need a body) and keep track of it
61                OgreOde::EntityInformer ei(entity,Matrix4::getScale(node->getScale()));
62                OgreOde::Geometry *geom = ei.createSingleStaticBox(_world, _space);
63        entity->setUserObject(geom);
64        _geoms.push_back(geom);
65
66                // The car is what we'll want to look at
67                _last_node = vehicle->getSceneNode();
68        }
69
70        //------------------------------------------------------------------------------------------------
71        // Override the destructor since there's some special deleting to do
72        virtual ~SimpleScenes_Buggy()
73        {
74                delete vehicle;
75
76                // Destroy the jump manually since it's not associated with
77                // any body it won't get deleted automatically
78                _mgr->destroySceneNode("Jump");
79                _mgr->destroyEntity("Jump");
80
81                // Geometries and Joints will get deleted by the base class
82        }
83
84        //------------------------------------------------------------------------------------------------
85        // Return our name
86        virtual const String& getName()
87        {
88                static String name = "Test Buggy";
89                return name;
90        }
91
92        //------------------------------------------------------------------------------------------------
93        // Tell the user what keys they can use
94        virtual const String& getKeys()
95        {
96                static String keys = "I/K - Accelerate/Brake, J/L - Turn, X - Change drive mode, N - Change Car";
97                return keys;
98        }
99
100
101        //------------------------------------------------------------------------------------------------
102        void changeCar()
103        {
104                sSelectedCar = (sSelectedCar + 1) % maxNumCar;
105
106                delete vehicle;
107                vehicle = static_cast <OgreOde_Prefab::Vehicle *> (dotOgreOdeLoader->loadObject (carFileNames[sSelectedCar], carNames[sSelectedCar]));
108
109
110                // Move the vehicle
111                vehicle->setPosition(Vector3(0,0.82898,0));
112        }
113        //------------------------------------------------------------------------------------------------
114#if (OGRE_VERSION_MINOR < 4)
115    // Handle the user's key presses   
116    virtual void frameEnded(Real time,InputReader* input)
117    {
118        // Do default key handling
119        SimpleScenes::frameEnded(time,input);
120#else
121
122    virtual void frameEnded(Real time, OIS::Keyboard* input, OIS::Mouse* mouse)
123    {
124        // Do default processing
125        SimpleScenes::frameEnded(time, input, mouse);
126#endif
127                // Tell the vehicle what digital inputs are being pressed; left, right, power and brake
128                // There are equivalent methods for analogue controls, current you can't change gear so
129                // you can't reverse!
130                vehicle->setInputs(input->isKeyDown(KC_J),input->isKeyDown(KC_L),input->isKeyDown(KC_I),input->isKeyDown(KC_K));
131
132                // Update the vehicle, you need to do this every time step
133                vehicle->update(time);
134                if (_key_delay > SimpleScenes::KEY_DELAY)
135                {
136
137                        if ((input->isKeyDown(KC_N)))
138                        {
139                                changeCar();
140                                _key_delay = 0.0;
141                        }
142                        // Change the drive mode between front, rear and 4wd
143                        if ((input->isKeyDown(KC_X)))
144                        {
145                                switch(_drive)
146                                {
147                                        // Switch from rear to front
148                                case 'R':
149                                        _drive = 'F';
150
151                                        vehicle->getWheel(0)->setPowerFactor(1);
152                                        vehicle->getWheel(1)->setPowerFactor(1);
153                                        vehicle->getWheel(2)->setPowerFactor(0);
154                                        vehicle->getWheel(3)->setPowerFactor(0);
155
156                                        setInfoText(carNames[sSelectedCar] + "Front wheel drive");
157                                        break;
158
159                                        // Switch from front to all
160                                case 'F':
161                                        _drive = '4';
162
163                                        vehicle->getWheel(0)->setPowerFactor(0.6);
164                                        vehicle->getWheel(1)->setPowerFactor(0.6);
165                                        vehicle->getWheel(2)->setPowerFactor(0.4);
166                                        vehicle->getWheel(3)->setPowerFactor(0.4);
167
168                                        setInfoText(carNames[sSelectedCar] + "All wheel drive");
169                                        break;
170
171                                        // Switch from all to rear
172                                case '4':
173                                        _drive = 'R';
174
175                                        vehicle->getWheel(0)->setPowerFactor(0);
176                                        vehicle->getWheel(1)->setPowerFactor(0);
177                                        vehicle->getWheel(2)->setPowerFactor(1);
178                                        vehicle->getWheel(3)->setPowerFactor(1);
179
180                                        setInfoText(carNames[sSelectedCar] + "Rear wheel drive");
181                                        break;
182                                }
183                                _key_delay = 0.0;
184                        }
185                }
186        }
187
188        //------------------------------------------------------------------------------------------------
189        // Override the collision callback to set our own parameters
190        bool collision(OgreOde::Contact* contact)
191        {
192                // If the base class doesn't think the collision needs to
193                // happen then we won't do anything either
194                if (!SimpleScenes::collision(contact)) 
195                        return false;
196
197                if (!OgreOde_Prefab::Vehicle::handleTyreCollision(contact))
198                {
199                        // Set the floor to be a bit slippy
200                        contact->setCoulombFriction(12.0);
201                }
202                return true;
203        }
204
205protected:
206        // Keep track of the things we need to delete manually or change according to user input
207        OgreOde_Prefab::Vehicle *vehicle;
208        char _drive;
209};
210
211#endif
Note: See TracBrowser for help on using the repository browser.