Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/helicopter.cc @ 6827

Last change on this file since 6827 was 6805, checked in by bensch, 19 years ago

orxonox/trunk: hover works as it should

File size: 13.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "helicopter.h"
20
21#include "weapons/weapon_manager.h"
22#include "weapons/test_gun.h"
23#include "weapons/turret.h"
24#include "weapons/cannon.h"
25
26#include "factory.h"
27#include "key_mapper.h"
28#include "event_handler.h"
29#include "state.h"
30
31#include "graphics_engine.h"
32
33using namespace std;
34
35CREATE_FACTORY(Helicopter, CL_HELICOPTER);
36
37/**
38 *  creates the controlable Helicopter
39 */
40Helicopter::Helicopter()
41{
42  this->init();
43}
44
45/**
46 *  destructs the helicopter, deletes alocated memory
47 */
48Helicopter::~Helicopter ()
49{
50}
51
52/**
53 * loads a Helicopter information from a specified file.
54 * @param fileName the name of the File to load the helicopter from (absolute path)
55 */
56Helicopter::Helicopter(const char* fileName)
57{
58  this->init();
59  TiXmlDocument doc(fileName);
60
61  if(!doc.LoadFile())
62  {
63    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName);
64    return;
65  }
66
67  this->loadParams(doc.RootElement());
68}
69
70/**
71 *  creates a new Spaceship from Xml Data
72 * @param root the xml element containing spaceship data
73
74   @todo add more parameters to load
75*/
76Helicopter::Helicopter(const TiXmlElement* root)
77{
78  this->init();
79  if (root != NULL)
80    this->loadParams(root);
81
82  //weapons:
83  Weapon* wpRight = new TestGun(0);
84  wpRight->setName("testGun Right");
85  Weapon* wpLeft = new TestGun(1);
86  wpLeft->setName("testGun Left");
87  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
88
89  cannon->setName("BFG");
90
91  this->addWeapon(wpLeft, 1, 0);
92  this->addWeapon(wpRight,1 ,1);
93  this->addWeapon(cannon, 0, 6);
94
95  //this->addWeapon(turret, 3, 0);
96
97  this->getWeaponManager()->changeWeaponConfig(1);
98  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
99}
100
101
102/**
103 * initializes a Helicopter
104 */
105void Helicopter::init()
106{
107//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
108  this->setClassID(CL_HELICOPTER, "Helicopter");
109
110  PRINTF(4)("HELICOPTER INIT\n");
111
112  this->loadModel("models/ships/helicopter_#.obj", 1.0);
113
114  EventHandler::getInstance()->grabEvents(true);
115
116  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
117  bFire = false;
118  xMouse = yMouse = 0;
119  mouseSensitivity = 0.05;
120  controlVelocityX = 100;
121  controlVelocityY = 100;
122  //rotorspeed = 1;
123  //tailrotorspeed = 0;
124
125  //cycle = 0.0;
126
127  // cameraissue
128  this->cameraNode.setParent(this);
129  this->cameraNode.setParentMode(PNODE_ALL);
130
131
132  // rotors
133  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
134  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
135
136  this->topRotor.setParent(this);
137  this->tailRotor.setParent(this);
138
139  this->topRotor.setRelCoor(Vector(-0.877,0.627,0));
140  this->tailRotor.setRelCoor(Vector(-4.43,0.297,0.068));
141  this->tailRotor.setAbsDir(Quaternion(M_PI_2,Vector(1,0,0)));
142
143  this->loadModel("models/ships/rotor.obj",1.0,3);
144  this->loadModel("models/ships/rotor.obj",0.2,4);
145
146
147  //travelSpeed = 15.0;
148  this->velocity = Vector(0.0,0.0,0.0);
149  this->velocityDir = Vector(1.0,0.0,0.0);
150//   GLGuiButton* button = new GLGuiPushButton();
151//   button->show();
152//   button->setLabel("orxonox");
153//   button->setBindNode(this);
154
155  //add events to the eventlist
156  registerEvent(KeyMapper::PEV_UP);
157  registerEvent(KeyMapper::PEV_DOWN);
158  registerEvent(KeyMapper::PEV_LEFT);
159  registerEvent(KeyMapper::PEV_RIGHT);
160  //registerEvent(SDLK_q);
161  registerEvent(SDLK_e);
162  registerEvent(SDLK_c);
163  registerEvent(KeyMapper::PEV_FIRE1);
164  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
165  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
166  //registerEvent(SDLK_PAGEUP);
167  //registerEvent(SDLK_PAGEDOWN);
168  registerEvent(EV_MOUSE_MOTION);
169
170  this->getWeaponManager()->setSlotCount(7);
171
172  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
173  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
174
175  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
176  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
177
178  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
179  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
180
181  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
182  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
183
184  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
185  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
186
187  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
188  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
189//
190   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
191   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
192   //
193//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
194//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
195//
196//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
197//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
198
199  this->getWeaponManager()->getFixedTarget()->setParent(&(this->cameraNode));
200  this->getWeaponManager()->getFixedTarget()->setRelCoor(0,0,0);
201
202  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
203
204}
205
206/**
207 * loads the Settings of a Helicopter from an XML-element.
208 * @param root the XML-element to load the Spaceship's properties from
209 */
210void Helicopter::loadParams(const TiXmlElement* root)
211{
212  WorldEntity::loadParams(root);
213}
214
215void  Helicopter::attachCamera()
216{
217  State::getCamera()->setParentSoft(this->getWeaponManager()->getFixedTarget());
218  State::getCameraTarget()->setParentSoft(this->getWeaponManager()->getFixedTarget());
219
220}
221
222
223void Helicopter::enter()
224{
225  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
226  this->attachCamera();
227
228
229}
230
231void Helicopter::leave()
232{
233  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
234  this->detachCamera();
235
236}
237
238
239/**
240 *  effect that occurs after the Helicopter is spawned
241*/
242void Helicopter::postSpawn ()
243{
244  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
245}
246
247/**
248 *  the action occuring if the helicopter left the game
249*/
250void Helicopter::leftWorld ()
251{}
252
253/**
254 *  this function is called, when two entities collide
255 * @param entity: the world entity with whom it collides
256 *
257 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
258 */
259void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
260{
261}
262
263
264
265/**
266 *  the function called for each passing timeSnap
267 * @param time The timespan passed since last update
268*/
269void Helicopter::tick (float time)
270{
271  Playable::tick(time);
272
273  if( xMouse != 0 || yMouse != 0)
274   {
275    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
276    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
277    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
278    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
279  }
280
281  // rotorrotation
282  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
283  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
284
285  // spaceship controlled movement
286  this->calculateVelocity(time);
287
288  Vector move = (velocity)*time;
289
290  // this is the air friction (necessary for a smooth control)
291  if(velocity.len() != 0) velocity -= velocity*0.1;
292
293  //travelSpeed = velocity.len();
294
295  //physics: Gravity
296  /*this->shiftCoor(Vector(0,-1,0));
297
298  this->shiftCoor(getAbsDirY()*rotorspeed);
299  */
300
301  /*
302  //hoover effect
303  cycle += time;
304  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
305  */
306
307  //readjust
308 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
309  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
310
311  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
312
313  this->shiftCoor (move);
314  //this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
315}
316
317/**
318 *  calculate the velocity
319 * @param time the timeslice since the last frame
320*/
321void Helicopter::calculateVelocity (float time)
322{
323  Vector accel(0.0, 0.0, 0.0);
324  float rotValX = 0.0;
325  float rotValZ = 0.0;
326  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
327  /* calculate the direction in which the craft is heading  */
328
329  if( this->bUp )
330   {
331     //this->shiftCoor(this->getAbsDirX());
332     //accel -= this->getAbsDirY();
333
334     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
335     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
336   }
337   else
338   {
339       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1)));
340   }
341
342  if( this->bDown )
343   {
344     //this->shiftCoor((this->getAbsDirX())*-1);
345     //accel -= this->getAbsDirY();
346
347     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
348     rotValZ += time;
349   }
350   else
351   {
352         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
353   }
354
355  if( this->bLeft /* > -this->getRelCoor().z*2*/)
356  {
357    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
358    //accel -= this->getAbsDirY();
359    //velocityDir.normalize();
360
361    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
362    rotValX -= time;
363  }
364  else
365   {
366         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
367   }
368
369  if( this->bRight /* > this->getRelCoor().z*2*/)
370  {
371    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
372    //accel += this->getAbsDirY();
373    //velocityDir.normalize();
374
375    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
376    rotValX += time;
377  }
378  else
379   {
380         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
381   }
382
383  if( this->bRollL /* > -this->getRelCoor().z*2*/)
384  {
385    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
386    //accel -= rightDirection;
387    //velocityDir.normalize();
388    //rot +=Vector(1,0,0);
389    //rotVal -= .4;
390  }
391  if( this->bRollR /* > this->getRelCoor().z*2*/)
392  {
393    this->shiftDir(Quaternion(time, Vector(1,0,0)));
394
395    //accel += rightDirection;
396    //velocityDir.normalize();
397    //rot += Vector(1,0,0);
398    //rotVal += .4;
399  }
400  if (this->bAscend )
401  {
402    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
403
404    accel += this->getAbsDirY();
405    //rotorspeed += 0.05;
406    //if (rotorspeed >= 2) rotorspeed = 2;
407    //velocityDir.normalize();
408    //rot += Vector(0,0,1);
409    //rotVal += .4;
410  }
411  else
412  {
413    //if(rotorspeed >= 1.05) rotorspeed -= 0.05;
414  }
415
416  if (this->bDescend )
417  {
418    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
419
420    accel -= this->getAbsDirY();
421    //rotorspeed -= 0.05;
422    //if (rotorspeed <= 0) rotorspeed = 0;
423    //velocityDir.normalize();
424    //rot += Vector(0,0,1);
425    //rotVal -= .4;
426  }
427  else
428  {
429    //if(rotorspeed <= 0.05) rotorspeed += 0.05;
430  }
431
432  velocity += accel*3;
433  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
434  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
435}
436
437
438void Helicopter::draw() const
439{
440  WorldEntity::draw();
441
442  glMatrixMode(GL_MODELVIEW);
443    glPushMatrix();
444
445    /* translate */
446    glTranslatef (this->topRotor.getAbsCoor ().x,
447                  this->topRotor.getAbsCoor ().y,
448                  this->topRotor.getAbsCoor ().z);
449    Vector tmpRot = this->topRotor.getAbsDir().getSpacialAxis();
450    glRotatef (this->topRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
451    this->getModel(3)->draw();
452
453    glPopMatrix ();
454    glPushMatrix();
455
456    /* translate */
457    glTranslatef (this->tailRotor.getAbsCoor ().x,
458                  this->tailRotor.getAbsCoor ().y,
459                  this->tailRotor.getAbsCoor ().z);
460    tmpRot = this->tailRotor.getAbsDir().getSpacialAxis();
461    glRotatef (this->tailRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
462    this->getModel(4)->draw();
463
464    glPopMatrix ();
465}
466
467/**
468 * @todo switch statement ??
469 */
470void Helicopter::process(const Event &event)
471{
472  Playable::process(event);
473
474  if( event.type == KeyMapper::PEV_LEFT)
475      this->bLeft = event.bPressed;
476  else if( event.type == KeyMapper::PEV_RIGHT)
477      this->bRight = event.bPressed;
478  else if( event.type == SDLK_e)
479    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
480  else if( event.type == SDLK_c)
481    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
482  else if( event.type == KeyMapper::PEV_UP)
483    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
484  else if( event.type == KeyMapper::PEV_DOWN)
485    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
486  else if( event.type == EV_MOUSE_MOTION)
487  {
488    this->xMouse = event.xRel*mouseSensitivity;
489    this->yMouse = event.yRel*mouseSensitivity;
490
491    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
492
493    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
494
495
496    if ((this->cameraNode.getAbsDirY()).y < 0.5)
497    {
498     if((this->cameraNode.getAbsDirX()).y > 0)
499     {
500        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
501     }
502     else
503     {
504         if(yMouse < 0) this->cameraNode.shiftDir(yDir);
505     }
506    }
507    else this->cameraNode.shiftDir(yDir);;
508    }
509}
Note: See TracBrowser for help on using the repository browser.