| 1 |  | 
|---|
| 2 |  | 
|---|
| 3 | /*  | 
|---|
| 4 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 5 |  | 
|---|
| 6 |    Copyright (C) 2004 orx | 
|---|
| 7 |  | 
|---|
| 8 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 9 |    it under the terms of the GNU General Public License as published by | 
|---|
| 10 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 11 |    any later version. | 
|---|
| 12 |  | 
|---|
| 13 |    ### File Specific: | 
|---|
| 14 |    main-programmer: Christian Meyer | 
|---|
| 15 |    co-programmer: ... | 
|---|
| 16 | */ | 
|---|
| 17 |  | 
|---|
| 18 | #include "track.h" | 
|---|
| 19 |  | 
|---|
| 20 | using namespace std; | 
|---|
| 21 |  | 
|---|
| 22 | /** | 
|---|
| 23 |         \brief creates a null Track part | 
|---|
| 24 | */ | 
|---|
| 25 | Track::Track () | 
|---|
| 26 | { | 
|---|
| 27 |         ID = 0; | 
|---|
| 28 |         offset = NULL; | 
|---|
| 29 |         end = NULL; | 
|---|
| 30 |         nextID = 0; | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 |         \brief creates a functional base Track part | 
|---|
| 35 |         \param number: the ID if this Track part | 
|---|
| 36 |         \param next: the ID of the next Track part | 
|---|
| 37 |         \param start: pointer to an anchor point (Vector) representing the offset of this part | 
|---|
| 38 |         \param finish: pointer to an anchor point (Vector) representing the end of this part | 
|---|
| 39 | */ | 
|---|
| 40 | Track::Track (Uint32 number, Uint32 next, Vector* start, Vector* finish) | 
|---|
| 41 | { | 
|---|
| 42 |         ID = number; | 
|---|
| 43 |         offset = start; | 
|---|
| 44 |         end = finish; | 
|---|
| 45 |         nextID = next; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 |         \brief removes the Track part from memory | 
|---|
| 50 | */ | 
|---|
| 51 | Track::~Track () | 
|---|
| 52 | { | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void Track::init() | 
|---|
| 56 | { | 
|---|
| 57 |    | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /** | 
|---|
| 62 |    \brief calculate a camera Placement from a "look at"-Location | 
|---|
| 63 |    \param lookat: the Location the camera should be centered on | 
|---|
| 64 |    \param camplc: pointer to a buffer where the new camera Placement should be put into | 
|---|
| 65 |     | 
|---|
| 66 |    Theoretically you can place the camera wherever you want, but for the sake of  | 
|---|
| 67 |    common sense I suggest that you at least try to keep the thing that should be looked | 
|---|
| 68 |    at inside camera boundaries. | 
|---|
| 69 | */ | 
|---|
| 70 | void Track::map_camera (Location* lookat, Placement* camplc) | 
|---|
| 71 | { | 
|---|
| 72 |   Line trace(*offset, *end - *offset); | 
|---|
| 73 |   float l = trace.len (); | 
|---|
| 74 |    | 
|---|
| 75 |   //    camplc->r = *offset + Vector(0,0,0.5); | 
|---|
| 76 |   //    camplc->w = Quaternion (trace.a, Vector(0,0,1)); | 
|---|
| 77 |   float r = (lookat->dist)*PI / l; | 
|---|
| 78 |   camplc->r = trace.r + (trace.a * ((lookat->dist-10.0) / l)) + Vector(0,0,5.0); | 
|---|
| 79 |    | 
|---|
| 80 |   Vector w(0.0,0.0,0.0); | 
|---|
| 81 |   w=Vector(0,0,0) - ((trace.r + (trace.a * ((lookat->dist) / l)) - camplc->r)); | 
|---|
| 82 |   //Vector up(0.0,sin(r),cos(r)); // corrupt... | 
|---|
| 83 |   Vector up(0.0, 0.0, 1.0); | 
|---|
| 84 |  | 
|---|
| 85 |   camplc->w = Quaternion(w, up); | 
|---|
| 86 |  | 
|---|
| 87 |   //printf("\n------\nup vector: [%f, %f, %f]\n", up.x, up.y, up.z); | 
|---|
| 88 |   //printf("direction: [%f, %f, %f]\n", w.x, w.y, w.z); | 
|---|
| 89 |   //printf("quaternion: w[ %f ], v[ %f, %f, %f ]\n", camplc->w.w, camplc->w.v.x, camplc->w.v.y, camplc->w.v.z); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | /** | 
|---|
| 93 |    \brief calculate a Placement from a given Location | 
|---|
| 94 |    \param loc: the Location the entity is in | 
|---|
| 95 |    \param plc: a pointer to a buffer where the corresponding Placement should be put | 
|---|
| 96 |    into | 
|---|
| 97 |    \return: true if track changes - false if track stays | 
|---|
| 98 |     | 
|---|
| 99 |    There are no limitations to how you transform a Location into a Placement, but for  | 
|---|
| 100 |    the sake of placement compatibility between track parts you should make sure that  | 
|---|
| 101 |    the resulting Placement at dist == 0 is equal to the offset Vector and the Placement | 
|---|
| 102 |    at dist == len() is equal to the end Vector. Elseway there will be ugly artifacts  | 
|---|
| 103 |    when transfering between track parts. | 
|---|
| 104 | */ | 
|---|
| 105 | bool Track::map_coords (Location* loc, Placement* plc) | 
|---|
| 106 | { | 
|---|
| 107 |         Line trace(*offset, *end - *offset); | 
|---|
| 108 |         float l = trace.len (); | 
|---|
| 109 |          | 
|---|
| 110 |         /* change to the next track? */ | 
|---|
| 111 |         if( loc->dist > l) | 
|---|
| 112 |         { | 
|---|
| 113 |                 loc->dist -= l; | 
|---|
| 114 |                 loc->part = nextID; | 
|---|
| 115 |                 //FIXME: loc->track = this; | 
|---|
| 116 |                 return true; | 
|---|
| 117 |         } | 
|---|
| 118 |          | 
|---|
| 119 |         /* this quaternion represents the rotation from start-vector (0,0,1) to the direction of | 
|---|
| 120 |          * the track */ | 
|---|
| 121 |         Quaternion dir(trace.a, Vector(0,0,1));  | 
|---|
| 122 |  | 
|---|
| 123 |         plc->r = trace.r + (trace.a * ((loc->dist) / l)) + /*dir.apply*/(loc->pos); | 
|---|
| 124 |         plc->w = dir * loc->rot; | 
|---|
| 125 |          | 
|---|
| 126 |         return false; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | /** | 
|---|
| 130 |         \brief this is called when a WorldEntity enters a Track part | 
|---|
| 131 |         \param entity: pointer to the WorldEntity in question | 
|---|
| 132 |          | 
|---|
| 133 |         You can do stuff like add or remove effects, do some coordinate finetuning  | 
|---|
| 134 |         or whatever in here. | 
|---|
| 135 | */ | 
|---|
| 136 | void Track::post_enter (WorldEntity* entity) | 
|---|
| 137 | { | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | /** | 
|---|
| 141 |         \brief this is called when a WorldEntity leaves a Track part | 
|---|
| 142 |         \param entity: pointer to the WorldEntity in question | 
|---|
| 143 |          | 
|---|
| 144 |         You can do stuff like add or remove effects, do some coordinate finetuning  | 
|---|
| 145 |         or whatever in here. | 
|---|
| 146 | */ | 
|---|
| 147 | void Track::post_leave (WorldEntity* entity) | 
|---|
| 148 | { | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /** | 
|---|
| 152 |         \brief this is called every frame | 
|---|
| 153 |         \param deltaT: amount of time passed since the last frame in seconds | 
|---|
| 154 |          | 
|---|
| 155 |         Do time based or polling scripts here.  | 
|---|
| 156 | */ | 
|---|
| 157 | void Track::tick (float deltaT) | 
|---|
| 158 | { | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|