Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

updated drawing function

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