Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/SpaceShip.cc @ 626

Last change on this file since 626 was 626, checked in by rgrieder, 16 years ago
  • thruster effects are now only shown when the ship accelerates
File size: 15.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      ...
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "SpaceShip.h"
29
30#include "../../tinyxml/tinyxml.h"
31#include "../../misc/String2Number.h"
32#include "../core/CoreIncludes.h"
33#include "../Orxonox.h"
34
35#include "OgreCamera.h"
36#include <OgreRenderWindow.h>
37
38namespace orxonox
39{
40    CreateFactory(SpaceShip);
41
42    SpaceShip::SpaceShip()
43    {
44        RegisterObject(SpaceShip);
45
46        SetConfigValue(bInvertMouse_, true);
47
48        this->setMouseEventCallback_ = false;
49
50        this->w = NULL;
51        this->tt = NULL;
52
53        this->moveForward_ = 0;
54        this->rotateUp_ = 0;
55        this->rotateDown_ = 0;
56        this->rotateRight_ = 0;
57        this->rotateLeft_ = 0;
58        this->loopRight_ = 0;
59        this->loopLeft_ = 0;
60        this->brakeForward_ = 0;
61        this->brakeRotate_ = 0;
62        this->brakeLoop_ = 0;
63        this->speedForward_ = 0;
64        this->speedRotateUpDown_ = 0;
65        this->speedRotateRightLeft_ = 0;
66        this->speedLoopRightLeft_ = 0;
67        this->maxSpeedForward_ = 0;
68        this->maxSpeedRotateUpDown_ = 0;
69        this->maxSpeedRotateRightLeft_ = 0;
70        this->maxSpeedLoopRightLeft_ = 0;
71        this->accelerationForward_ = 0;
72        this->accelerationRotateUpDown_ = 0;
73        this->accelerationRotateRightLeft_ = 0;
74        this->accelerationLoopRightLeft_ = 0;
75
76        this->speed = 250;
77        this->loop = 100;
78        this->rotate = 10;
79        this->mouseX = 0;
80        this->mouseY = 0;
81        this->maxMouseX = 0;
82        this->minMouseX = 0;
83        this->moved = false;
84
85        this->brakeRotate(rotate*10);
86        this->brakeLoop(loop);
87
88        COUT(3) << "Info: SpaceShip was loaded" << std::endl;
89    }
90
91    SpaceShip::~SpaceShip()
92    {
93        if (w)
94            delete w;
95        if (tt)
96            delete tt;
97    }
98
99    void SpaceShip::setMaxSpeedValues(float maxSpeedForward, float maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float maxSpeedLoopRightLeft)
100    {
101        this->maxSpeedForward_ = maxSpeedForward;
102        this->maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
103        this->maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
104        this->maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
105    }
106
107    void SpaceShip::loadParams(TiXmlElement* xmlElem)
108    {
109        Model::loadParams(xmlElem);
110
111        w = new particle::ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"schuss" + this->getName(),"Orxonox/schuss");
112        w->getParticleSystem()->setParameter("local_space","true");
113        w->newEmitter();
114/*
115        w->setDirection(Vector3(0,0,1));
116        w->setPositionOfEmitter(0, Vector3(10,10,0));
117        w->setPositionOfEmitter(1, Vector3(-10,10,0));
118*/
119        w->setDirection(Vector3(1,0,0));
120        w->setPositionOfEmitter(0, Vector3(0,10,10));
121        w->setPositionOfEmitter(1, Vector3(0,-10,10));
122
123        emitterRate_ = w->getRate();
124
125        Ogre::SceneNode* node1 = this->getNode()->createChildSceneNode(this->getName() + "particle1");
126        node1->setInheritScale(false);
127        w->addToSceneNode(node1);
128
129
130
131        tt = new particle::ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
132        tt->getParticleSystem()->setParameter("local_space","true");
133        tt->newEmitter();
134/*
135        tt->setDirection(Vector3(0,0,1));
136        tt->setPositionOfEmitter(0, Vector3(20,-1,-15));
137        tt->setPositionOfEmitter(1, Vector3(-20,-1,-15));
138*/
139        tt->setDirection(Vector3(-1,0,0));
140        tt->setPositionOfEmitter(0, Vector3(-15,20,-1));
141        tt->setPositionOfEmitter(1, Vector3(-15,-20,-1));
142        tt->setVelocity(50);
143
144        Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2");
145        node2->setInheritScale(false);
146        tt->addToSceneNode(node2);
147
148
149
150        if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft"))
151        {
152            std::string forwardStr = xmlElem->Attribute("forward");
153            std::string rotateupdownStr = xmlElem->Attribute("rotateupdown");
154            std::string rotaterightleftStr = xmlElem->Attribute("rotaterightleft");
155            std::string looprightleftStr = xmlElem->Attribute("looprightleft");
156
157            String2Number<float>(this->maxSpeedForward_, forwardStr);
158            String2Number<float>(this->maxSpeedRotateUpDown_, rotateupdownStr);
159            String2Number<float>(this->maxSpeedRotateRightLeft_, rotaterightleftStr);
160            String2Number<float>(this->maxSpeedLoopRightLeft_, looprightleftStr);
161
162            COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl;
163        }
164
165        if (xmlElem->Attribute("camera"))
166        {
167            Ogre::Camera *cam = Orxonox::getSingleton()->getSceneManager()->createCamera("ShipCam");
168            Ogre::SceneNode *node = this->getNode()->createChildSceneNode("CamNode");
169/*
170//            node->setInheritOrientation(false);
171            cam->setPosition(Vector3(0,50,-150));
172            cam->lookAt(Vector3(0,20,0));
173            cam->roll(Degree(0));
174*/
175
176            cam->setPosition(Vector3(-150,0,50));
177//            cam->setPosition(Vector3(0,-350,0));
178            cam->lookAt(Vector3(0,0,20));
179            cam->roll(Degree(-90));
180
181            node->attachObject(cam);
182            Orxonox::getSingleton()->getOgrePointer()->getRoot()->getAutoCreatedWindow()->addViewport(cam);
183        }
184    }
185
186    bool SpaceShip::mouseMoved(const OIS::MouseEvent &e)
187    {
188        this->mouseX += e.state.X.rel;
189        if (this->bInvertMouse_)
190            this->mouseY += e.state.Y.rel;
191        else
192            this->mouseY -= e.state.Y.rel;
193
194//        if(mouseX>maxMouseX) maxMouseX = mouseX;
195//        if(mouseX<minMouseX) minMouseX = mouseX;
196//        cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
197
198        this->moved = true;
199
200        return true;
201    }
202
203    void SpaceShip::tick(float dt)
204    {
205        if (!this->setMouseEventCallback_)
206        {
207            if (Orxonox::getSingleton()->getMouse())
208            {
209                Orxonox::getSingleton()->getMouse()->setEventCallback(this);
210                this->setMouseEventCallback_ = true;
211            }
212        }
213
214        WorldEntity::tick(dt);
215
216        OIS::Keyboard* mKeyboard = Orxonox::getSingleton()->getKeyboard();
217        OIS::Mouse* mMouse = Orxonox::getSingleton()->getMouse();
218
219        mKeyboard->capture();
220        mMouse->capture();
221
222        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
223            this->moveForward(speed);
224        else
225            this->moveForward(0);
226
227        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
228            this->brakeForward(speed);
229        else
230            this->brakeForward(speed/10);
231
232        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
233            this->loopRight(loop);
234        else
235            this->loopRight(0);
236
237        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
238            this->loopLeft(loop);
239        else
240            this->loopLeft(0);
241
242        if(moved)
243        {
244            if (mouseY<=0)
245                this->rotateUp(-mouseY*rotate);
246            if (mouseY>0)
247                this->rotateDown(mouseY*rotate);
248            if (mouseX>0)
249                this->rotateRight(mouseX*rotate);
250            if (mouseX<=0)
251                this->rotateLeft(-mouseX*rotate);
252
253            mouseY = 0;
254            mouseX = 0;
255            moved = false;
256        }
257        else
258        {
259            this->rotateUp(0);
260            this->rotateDown(0);
261            this->rotateRight(0);
262            this->rotateLeft(0);
263        }
264
265        if(moveForward_ > 0)
266        {
267            accelerationForward_ = moveForward_;
268            if(speedForward_ < maxSpeedForward_)
269                speedForward_ += accelerationForward_*dt;
270            if(speedForward_ > maxSpeedForward_)
271                speedForward_ = maxSpeedForward_;
272        }
273
274        if(moveForward_ <= 0)
275        {
276            accelerationForward_ = -brakeForward_;
277            if(speedForward_ > 0)
278                speedForward_ += accelerationForward_*dt;
279            if(speedForward_ < 0)
280                speedForward_ = 0;
281        }
282
283        if(rotateUp_ > 0)
284        {
285            accelerationRotateUpDown_ = rotateUp_;
286            if(speedRotateUpDown_ < maxSpeedRotateUpDown_)
287                speedRotateUpDown_ += accelerationRotateUpDown_*dt;
288            if(speedRotateUpDown_ > maxSpeedRotateUpDown_)
289            speedRotateUpDown_ = maxSpeedRotateUpDown_;
290        }
291
292        if(rotateDown_ > 0)
293        {
294            accelerationRotateUpDown_ = rotateDown_;
295            if(speedRotateUpDown_ > -maxSpeedRotateUpDown_)
296                speedRotateUpDown_ -= accelerationRotateUpDown_*dt;
297            if(speedRotateUpDown_ < -maxSpeedRotateUpDown_)
298                speedRotateUpDown_ = -maxSpeedRotateUpDown_;
299        }
300
301        if(rotateUp_ == 0 && rotateDown_ == 0)
302        {
303            accelerationRotateUpDown_ = brakeRotate_;
304            if(speedRotateUpDown_ > 0)
305                speedRotateUpDown_ -= accelerationRotateUpDown_*dt;
306            if(speedRotateUpDown_ < 0)
307                speedRotateUpDown_ += accelerationRotateUpDown_*dt;
308            if(fabs(speedRotateUpDown_) < accelerationRotateUpDown_*dt)
309                speedRotateUpDown_ = 0;
310        }
311
312        if(rotateRight_ > 0)
313        {
314            accelerationRotateRightLeft_ = rotateRight_;
315            if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_)
316                speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt;
317            if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_)
318                speedRotateRightLeft_ = -maxSpeedRotateRightLeft_;
319        }
320
321        if(rotateLeft_ > 0)
322        {
323            accelerationRotateRightLeft_ = rotateLeft_;
324            if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_)
325                speedRotateRightLeft_ += accelerationRotateRightLeft_*dt;
326            if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_)
327                speedRotateRightLeft_ = maxSpeedRotateRightLeft_;
328        }
329
330        if(rotateRight_ == 0 && rotateLeft_ == 0)
331        {
332            accelerationRotateRightLeft_ = brakeRotate_;
333            if(speedRotateRightLeft_ > 0)
334                speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt;
335            if(speedRotateRightLeft_ < 0)
336                speedRotateRightLeft_ += accelerationRotateRightLeft_*dt;
337            if(fabs(speedRotateRightLeft_) < accelerationRotateRightLeft_*dt)
338                speedRotateRightLeft_ = 0;
339        }
340
341        if(loopRight_ > 0)
342        {
343            accelerationLoopRightLeft_ = loopRight_;
344            if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_)
345                speedLoopRightLeft_ += accelerationLoopRightLeft_*dt;
346            if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_)
347                speedLoopRightLeft_ = maxSpeedLoopRightLeft_;
348        }
349
350        if(loopLeft_ > 0)
351        {
352            accelerationLoopRightLeft_ = loopLeft_;
353            if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_)
354                speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt;
355            if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_)
356                speedLoopRightLeft_ = -maxSpeedLoopRightLeft_;
357        }
358
359        if(loopLeft_ == 0 && loopRight_ == 0)
360        {
361            accelerationLoopRightLeft_ = brakeLoop_;
362            if(speedLoopRightLeft_ > 0)
363                speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt;
364            if(speedLoopRightLeft_ < 0)
365                speedLoopRightLeft_ += accelerationLoopRightLeft_*dt;
366            if(fabs(speedLoopRightLeft_) < accelerationLoopRightLeft_*dt)
367                speedLoopRightLeft_ = 0;
368        }
369
370        Vector3 transVector = Vector3::ZERO;
371/*
372        transVector.z = 1;
373        this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL);
374        this->pitch(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL);
375        this->yaw(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL);
376        this->roll(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL);
377*/
378
379        transVector.x = 1;
380        this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL);
381        this->yaw(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL);
382        this->roll(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL);
383        this->pitch(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL);
384
385        if (accelerationForward_ > 25.0)
386        {
387          this->tt->setRate(emitterRate_);
388        }
389        else
390        {
391          this->tt->setRate(0);
392        }
393
394    }
395
396    void SpaceShip::moveForward(float moveForward) {
397        moveForward_ = moveForward;
398    }
399
400    void SpaceShip::rotateUp(float rotateUp) {
401        rotateUp_ = rotateUp;
402    }
403
404    void SpaceShip::rotateDown(float rotateDown) {
405        rotateDown_ = rotateDown;
406    }
407
408    void SpaceShip::rotateLeft(float rotateLeft) {
409        rotateLeft_ = rotateLeft;
410    }
411
412    void SpaceShip::rotateRight(float rotateRight) {
413        rotateRight_ = rotateRight;
414    }
415
416    void SpaceShip::loopLeft(float loopLeft) {
417        loopLeft_ = loopLeft;
418    }
419
420    void SpaceShip::loopRight(float loopRight) {
421        loopRight_ = loopRight;
422    }
423
424    void SpaceShip::brakeForward(float brakeForward) {
425        brakeForward_ = brakeForward;
426    }
427
428    void SpaceShip::brakeRotate(float brakeRotate) {
429        brakeRotate_ = brakeRotate;
430    }
431
432    void SpaceShip::brakeLoop(float brakeLoop) {
433        brakeLoop_ = brakeLoop;
434    }
435
436    void SpaceShip::maxSpeedForward(float maxSpeedForward) {
437        maxSpeedForward_ = maxSpeedForward;
438    }
439
440    void SpaceShip::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) {
441        maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
442    }
443
444    void SpaceShip::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) {
445        maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
446    }
447
448    void SpaceShip::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) {
449        maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
450    }
451}
Note: See TracBrowser for help on using the repository browser.