Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Working SwarmMissile guidance system
CD/CR needs update

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