Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/projectiles/swarm_projectile.cc @ 10159

Last change on this file since 10159 was 10153, checked in by nicolasc, 17 years ago

bump

File size: 9.0 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004-2006 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   ### File Specific
13   main-programmer: Marc Schaerrer, Nicolas Schlumberger
14   co-programmer:
15
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
18
19#include "swarm_projectile.h"
20
21#include "state.h"
22
23#include "particles/dot_emitter.h"
24#include "particles/sprite_particles.h"
25#include "space_ships/space_ship.h"
26#include "effects/trail.h"
27
28#include "debug.h"
29
30#include "class_id_DEPRECATED.h"
31
32#include "math/vector.h"
33
34ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE);
35CREATE_FAST_FACTORY_STATIC(SwarmProjectile);
36
37/**
38 *  standard constructor
39*/
40SwarmProjectile::SwarmProjectile () : Projectile()
41{
42
43/*  this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/
44  this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw())
45  this->loadExplosionSound("sound/explosions/explosion_4.wav");
46
47
48  this->setMinEnergy(1);
49  this->setHealthMax(10);
50  this->lifeSpan = 4.0;
51  this->agility = 3.5;
52
53  this->emitter = new DotEmitter(100, 5, M_2_PI);
54  this->emitter->setParent(this);
55  this->emitter->setSpread(M_PI, M_PI);
56
57  this->turningSpeed = 10;
58
59  this->physDamage = 200;
60  this->elecDamage = 0;
61
62  this->trail = new Trail(2.5,4,.2, this);
63  //this->trail->setParent( this);
64  this->trail->setTexture( "maps/laser.png");
65}
66
67
68/**
69 *  standard deconstructor
70*/
71SwarmProjectile::~SwarmProjectile ()
72{
73
74  if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1)
75  {
76    if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles))
77      delete SwarmProjectile::explosionParticles;
78    SwarmProjectile::explosionParticles = NULL;
79  }
80  // delete this->emitter;
81  delete this->trail;
82}
83
84SpriteParticles* SwarmProjectile::explosionParticles = NULL;
85
86
87
88void SwarmProjectile::activate()
89{
90  if (unlikely(SwarmProjectile::explosionParticles == NULL))
91  {
92    SwarmProjectile::explosionParticles = new SpriteParticles(200);
93    SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles");
94    SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
95    SwarmProjectile::explosionParticles->setLifeSpan(.5, .3);
96    SwarmProjectile::explosionParticles->setRadius(0.0, 10);
97    SwarmProjectile::explosionParticles->setRadius(.5, 15.0);
98    SwarmProjectile::explosionParticles->setRadius(1.0, 10.0);
99    SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1);
100    SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8);
101    SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
102    SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0);
103  }
104
105
106  this->updateNode(0);
107  this->emitter->setEmissionRate(50.0);
108  this->emitter->setEmissionVelocity(0.0);
109  this->emitter->setInheritSpeed(0);
110
111  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
112
113  this->maxVelocity = 300;
114
115  this->rotationSpeed = 360;
116  this->angle = 0;
117
118  this->curDir = this->lastDir = this->velocity;
119}
120
121
122void SwarmProjectile::deactivate()
123{
124  this->emitter->setSystem(NULL);
125  this->lifeCycle = 0.0;
126
127  this->toList(OM_DEAD);
128  this->removeNode();
129  SwarmProjectile::fastFactory->kill(this);
130}
131
132
133void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location)
134{
135  if (this->hitEntity != entity)
136    this->destroy( entity );
137  this->hitEntity = entity;
138  //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage());
139  this->destroy(this);
140}
141
142
143void SwarmProjectile::setTarget(PNode* target)
144{
145    this->target = target;
146}
147
148
149
150/**
151 *  this function gets called by tick to calculate the new flight direction
152 *  @param curDirection direction vector
153 *  @param estTargetDir target vector, pointing to where the target will be on hit
154 *  @param angle = tick * turningSpeed
155 *  @return (new) direction vector
156*/
157Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle)
158{
159  if (unlikely(curDirection.len() == 0))
160    return curDirection;
161  //printf("recalculating direction\n");
162  float tmp = angleRad ( curDirection, estTargetDir);
163  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
164//   printf("turning angle: %f\ntemp: %f\n", angle, tmp);
165
166  if( fabsf(angle) >  fabsf(tmp) ) 
167    angle = tmp;
168  else
169    angle *= tmp/fabsf(tmp);
170
171  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
172  d.normalize();
173  if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication
174
175  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
176  newDir.normalize();
177  newDir *= curDirection.len();
178  return newDir;
179}
180
181
182
183
184/**
185 *  signal tick, time dependent things will be handled here
186 * @param time since last tick
187*/
188void SwarmProjectile::tick (float time)
189{
190/*
191  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
192
193  {
194    speed = velocity.len();
195    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
196
197    if(velocity.dot(diffVector) != 0)
198    {
199      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
200
201      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
202        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
203      else if(velocity.dot(diffVector) > 0)
204        velocity += correctionVector;
205      else if (velocity.dot(diffVector) < 0)
206        velocity -= correctionVector;
207    }
208    else
209      velocity += diffVector * agility;
210
211    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
212  }
213
214  velocity *= maxVelocity/velocity.len();
215  Vector v = this->velocity * (time);
216  this->shiftCoor(v);*/
217
218
219/** old  guiding functuion*/
220
221  float projectileVelocity = this->getVelocity().len();
222  if (target != NULL){
223    Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
224    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
225  }
226  else
227    if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) )
228      this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max
229
230//   printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
231//   printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);
232
233  this->shiftCoor(this->velocity * time);
234
235
236/*
237  Vector pjV = this->getVelocity();
238  Vector tV = this->target->getVelocity();
239  Vector pT = this->target->getAbsCoor() - this->getAbsCoor(); // vector projectile target
240
241  Vector a = tV.getNormalized() * pT.dot(tV.getNormalized());
242
243  float A = 2 * pT.len() * pT.len();
244  float B = 2 * a.len() * tV.len();
245  float D = 2 * sqrt(B * B - 4 * pT.len() * pT.len() *(tV.len() * tV.len() - pjV.len() * pjV.len()));
246  float tti;
247
248  if (A != 0){
249    if ( B < D ) tti = ( B + D ) / A;
250    else tti = ( B + D ) / A;
251  }
252  else tti = 0;
253
254
255  Vector estTargetDir;
256  if (tti == 0)
257    estTargetDir = pT.getNormalized() * pjV.len();
258  else
259    estTargetDir = pT / tti + pjV;
260
261  this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
262
263  this->shiftCoor(this->velocity * (this->maxVelocity * time));*/
264
265  if(this->tickLifeCycle(time))
266    this->deactivate();
267
268  this->trail->tick(time);
269
270  this->angle += this->rotationSpeed * time;
271
272  this->lastDir = this->curDir;
273  this->curDir = this->velocity;
274/*
275  if( (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3)   // FIXME  Temp fake workaround for collision :)
276  {
277    dynamic_cast<WorldEntity*>(target)->destroy( this);
278    this->destroy( this);
279  }*/
280}
281
282/**
283 *  the function gets called, when the projectile is destroyed
284 */
285void SwarmProjectile::destroy (WorldEntity* killer)
286{
287
288//   printf("THIS SHOULD WORK!\n");
289
290  Projectile::destroy( killer );
291  PRINTF(5)("DESTROY SwarmProjectile\n");
292  this->lifeCycle = .95; //!< @todo calculate this usefully.
293  this->emitter->setSystem(SwarmProjectile::explosionParticles);
294
295  this->emitter->setEmissionRate(1000.0);
296  this->emitter->setEmissionVelocity(50.0);
297  this->deactivate();
298
299}
300
301
302void SwarmProjectile::draw () const
303{
304  glMatrixMode(GL_MODELVIEW);
305  glPushMatrix();
306
307  Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3;
308  tmpDir.slerpTo(this->lastDir, .4);
309
310  float matrix[4][4];
311  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
312  Vector tmpRot = this->getAbsCoor().cross(tmpDir);
313  glRotatef (angleDeg ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z );
314  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
315  this->getAbsDir().matrix (matrix);
316  glMultMatrixf((float*)matrix);
317  //glScalef(2.0, 2.0, 2.0);  // no double rescale
318  this->getModel()->draw();
319  glTranslatef(-.9,0,0);
320  this->trail->draw();
321  glPopMatrix();
322}
Note: See TracBrowser for help on using the repository browser.