Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

solved problem, by setting target = NULL in constructor

File size: 8.4 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 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: Silvan Nellen
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
26#include "debug.h"
27
28#include "class_id_DEPRECATED.h"
29
30ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE);
31CREATE_FAST_FACTORY_STATIC(SwarmProjectile);
32
33/**
34 *  standard constructor
35*/
36SwarmProjectile::SwarmProjectile () : Projectile()
37{
38
39/*  this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/
40  this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw())
41  this->loadExplosionSound("sound/explosions/explosion_4.wav");
42
43
44  this->setMinEnergy(1);
45  this->setHealthMax(10);
46  this->lifeSpan = 2.0;
47  this->agility = 3.5;
48  this->maxVelocity = 100;
49
50  this->emitter = new DotEmitter(100, 5, M_2_PI);
51  this->emitter->setParent(this);
52  this->emitter->setSpread(M_PI, M_PI);
53
54  this->target = NULL;
55
56  this->rotationSpeed = 100;
57  this->angle = 0;
58}
59
60
61/**
62 *  standard deconstructor
63*/
64SwarmProjectile::~SwarmProjectile ()
65{
66  // delete this->emitter;
67
68  /* this is normaly done by World.cc by deleting the ParticleEngine */
69  if (SwarmProjectile::trailParticles != NULL && SwarmProjectile::objectList().size() <= 1)
70  {
71    if (ParticleSystem::objectList().exists(SwarmProjectile::trailParticles))
72      delete SwarmProjectile::trailParticles;
73    SwarmProjectile::trailParticles = NULL;
74  }
75  if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1)
76  {
77    if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles))
78      delete SwarmProjectile::explosionParticles;
79    SwarmProjectile::explosionParticles = NULL;
80  }
81
82}
83
84SpriteParticles* SwarmProjectile::trailParticles = NULL;
85SpriteParticles* SwarmProjectile::explosionParticles = NULL;
86
87
88
89void SwarmProjectile::activate()
90{
91  if (unlikely(SwarmProjectile::trailParticles == NULL))
92  {
93    SwarmProjectile::trailParticles = new SpriteParticles(2000);
94    SwarmProjectile::trailParticles->setName("BoomerangProjectileTrailParticles");
95    SwarmProjectile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
96    SwarmProjectile::trailParticles->setLifeSpan(1.0, .3);
97    SwarmProjectile::trailParticles->setRadius(0.0, .5);
98    SwarmProjectile::trailParticles->setRadius(0.2, 4.0);
99    SwarmProjectile::trailParticles->setRadius(.5, 1.5);
100    SwarmProjectile::trailParticles->setRadius(1.0, 1.5);
101    SwarmProjectile::trailParticles->setColor(0.0, 1,0,0,.7);
102    SwarmProjectile::trailParticles->setColor(0.2, .8,.8,0,.5);
103    SwarmProjectile::trailParticles->setColor(0.5, .8,.8,.8,.8);
104    SwarmProjectile::trailParticles->setColor(1.0, .8,.8,.8,.0);
105  }
106  if (unlikely(SwarmProjectile::explosionParticles == NULL))
107  {
108    SwarmProjectile::explosionParticles = new SpriteParticles(200);
109    SwarmProjectile::explosionParticles->setName("BoomerangProjectileExplosionParticles");
110    SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
111    SwarmProjectile::explosionParticles->setLifeSpan(.5, .3);
112    SwarmProjectile::explosionParticles->setRadius(0.0, 10);
113    SwarmProjectile::explosionParticles->setRadius(.5, 15.0);
114    SwarmProjectile::explosionParticles->setRadius(1.0, 10.0);
115    SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1);
116    SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8);
117    SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
118    SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0);
119  }
120
121  this->emitter->setSystem(SwarmProjectile::trailParticles);
122
123  this->updateNode(0);
124  this->emitter->setEmissionRate(45.0);
125  this->emitter->setEmissionVelocity(0.0);
126
127  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
128}
129
130
131void SwarmProjectile::deactivate()
132{
133  this->emitter->setSystem(NULL);
134  this->lifeCycle = 0.0;
135
136  this->toList(OM_DEAD);
137  this->removeNode();
138  SwarmProjectile::fastFactory->kill(this);
139}
140
141
142void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location)
143{
144  if (this->hitEntity != entity)
145    this->destroy( entity );
146  this->hitEntity = entity;
147}
148
149
150
151/**
152 *  this function gets called by tick to calculate the new flight direction
153 *  @param curDirection direction vector
154 *  @param estTargetDir target vector, pointing to where the target will be on hit
155 *  @param angle = tick * turningSpeed
156 *  @return (new) direction vector
157*/
158Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle)
159{
160  if (unlikely(curDirection.len() == 0))
161    return curDirection;
162  printf("recalculating direction\n");
163  float tmp = angleDeg ( curDirection, estTargetDir);
164  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
165
166  if( fabsf(angle) >  fabsf(tmp) ) { angle = tmp; }
167
168  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
169  d.normalize();
170  if( unlikely( angle == 90)) { return d; } //avoid complication
171
172  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
173  newDir.normalize();
174  newDir *= curDirection.len();
175  return newDir;
176}
177
178
179
180
181/**
182 *  signal tick, time dependent things will be handled here
183 * @param time since last tick
184*/
185void SwarmProjectile::tick (float time)
186{
187/*
188  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
189
190  {
191    speed = velocity.len();
192    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
193
194    if(velocity.dot(diffVector) != 0)
195    {
196      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
197
198      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
199        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
200      else if(velocity.dot(diffVector) > 0)
201        velocity += correctionVector;
202      else if (velocity.dot(diffVector) < 0)
203        velocity -= correctionVector;
204    }
205    else
206      velocity += diffVector * agility;
207
208    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
209  }
210
211  velocity *= maxVelocity/velocity.len();
212  Vector v = this->velocity * (time);
213  this->shiftCoor(v);*/
214
215  float projectileVelocity = this->velocity.len();
216  if (target != NULL){
217    float tti;  //Time To Impact
218    float targetVelocity = this->target->getVelocity().len();
219    if (unlikely(projectileVelocity == 0 && targetVelocity == 0)) // TODO calculate this reasonably
220      tti = 1; // we do have a problem....
221    else
222      tti = this->target->getRelCoor().len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity);
223    Vector estTargetDir = (this->target->getRelCoor() + this->target->getVelocity()) * tti;
224    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
225  }
226  else
227    if (likely(projectileVelocity != 0))
228      this->velocity *= maxVelocity / projectileVelocity; // set speed to max
229
230
231  this->shiftCoor(velocity * time);
232
233  if(this->tickLifeCycle(time))
234    this->deactivate();
235
236  this->updateAngle(time);
237}
238
239/**
240 *  the function gets called, when the projectile is destroyed
241 */
242void SwarmProjectile::destroy (WorldEntity* killer)
243{
244
245  printf("THIS SHOULD WORLk\n");
246
247  Projectile::destroy( killer );
248  PRINTF(5)("DESTROY SwarmProjectile\n");
249  this->lifeCycle = .95; //!< @todo calculate this usefully.
250  this->emitter->setSystem(SwarmProjectile::explosionParticles);
251
252  this->emitter->setEmissionRate(1000.0);
253  this->emitter->setEmissionVelocity(50.0);
254  //  this->deactivate();
255
256}
257
258void SwarmProjectile::updateAngle(float time)
259{
260  this->angle += this->rotationSpeed * time;
261}
262
263void SwarmProjectile::draw () const
264{
265  glMatrixMode(GL_MODELVIEW);
266  glPushMatrix();
267
268  float matrix[4][4];
269  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
270  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
271  this->getAbsDir().matrix (matrix);
272  glMultMatrixf((float*)matrix);
273  //glScalef(2.0, 2.0, 2.0);  // no double rescale
274  this->getModel()->draw();
275
276  glPopMatrix();
277
278}
279
Note: See TracBrowser for help on using the repository browser.