Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r1]: src / psim / vehicle.h Maximize Restore History

Download this file

vehicle.h    381 lines (270 with data), 7.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// vehicle.h [psim]
// Copyright 2004-2006 Jasmine Langridge, jaz at positro.net
// License: GPL version 2 (see included gpl.txt)
// vehicle core types
#define VCTYPE_TANK 1
#define VCTYPE_HELICOPTER 2
#define VCTYPE_PLANE 3
#define VCTYPE_HOVERCRAFT 4
#define VCTYPE_CAR 5
// vehicle clip point types
#define VCLIP_BODY 10
#define VCLIP_DRIVE_LEFT 30
#define VCLIP_DRIVE_RIGHT 31
#define VCLIP_HOVER 40
// RPM = revolutions per minute
// RPS = radians per second
#define RPM_TO_RPS(x) ((x) * (PI / 30.0f))
#define RPS_TO_RPM(x) ((x) * (30.0f / PI))
// MPS = metres per second
// KPH = kilometres per hour
// MPH = miles per hour
#define MPS_TO_KPH(x) ((x) * 3.6f)
#define MPS_TO_MPH(x) ((x) * 2.23693629f) // thanks Google!
struct v_control_s {
// shared
float throttle;
float brake1,brake2;
vec3f turn;
vec2f aim;
// helicopter
float collective;
// -- utility --
void setZero() {
throttle = 0.0f;
brake1 = 0.0f;
brake2 = 0.0f;
turn = vec3f::zero();
aim = vec2f::zero();
collective = 0.0f;
}
void setDefaultRates() {
throttle = 10.0f;
brake1 = 10.0f;
brake2 = 10.0f;
turn = vec3f(10.0f,10.0f,10.0f);
aim = vec2f(10.0f,10.0f);
collective = 10.0f;
}
void clamp() {
CLAMP(throttle, -1.0f, 1.0f);
CLAMP(brake1, 0.0f, 1.0f);
CLAMP(brake2, 0.0f, 1.0f);
CLAMP(turn.x, -1.0f, 1.0f);
CLAMP(turn.y, -1.0f, 1.0f);
CLAMP(turn.z, -1.0f, 1.0f);
CLAMP(aim.x, -1.0f, 1.0f);
CLAMP(aim.y, -1.0f, 1.0f);
CLAMP(collective, -1.0f, 1.0f);
}
};
typedef v_control_s v_state_s;
class PDriveSystem {
private:
std::vector<vec2f> powercurve;
std::vector<vec2f> gear;
float gearch_first, gearch_repeat;
float minRPS, maxRPS;
protected:
float getPowerAtRPS(float rps);
public:
PDriveSystem() :
gearch_first(0.4f),
gearch_repeat(0.15f),
minRPS(10000000.0f),
maxRPS(0.0f) { }
void addPowerCurvePoint(float rpm, float power) {
if (rpm <= 0.0f) return;
float rps = RPM_TO_RPS(rpm);
powercurve.push_back(vec2f(rps, power));
if (minRPS > rps) minRPS = rps;
if (maxRPS < rps) maxRPS = rps;
}
void addGear(float ratio) {
if (hasGears()) {
if (ratio <= getLastGearRatio()) return;
} else {
if (ratio <= 0.0f) return;
}
gear.push_back(vec2f(ratio, 1.0f / ratio));
}
bool hasGears() { return !gear.empty(); }
float getLastGearRatio() { return gear.back().x; }
friend class PDriveSystemInstance;
};
class PDriveSystemInstance {
private:
PDriveSystem *dsys;
float rps;
int currentgear, targetgear_rel;
float gearch;
bool reverse;
float out_torque;
bool flag_gearchange;
public:
PDriveSystemInstance(PDriveSystem *system) :
dsys(system),
currentgear(0),
targetgear_rel(0),
gearch(0.0f),
reverse(false),
out_torque(0.0f),
flag_gearchange(false) { }
void tick(float delta, float throttle, float wheel_rps);
float getOutputTorque() { return out_torque; }
float getEngineRPS() { return rps; }
float getEngineRPM() { return RPS_TO_RPM(rps); }
int getCurrentGear() { return reverse ? -1 : currentgear; }
bool getFlagGearChange() {
bool ret = flag_gearchange;
flag_gearchange = false;
return ret;
}
void doReset() {
rps = dsys->minRPS;
currentgear = 0;
targetgear_rel = 0;
gearch = 0.0f;
out_torque = 0.0f;
}
};
struct vehicle_clip_s {
vec3f pt;
int type;
float force, dampening;
};
struct PVehicleTypeWheel {
vec3f pt;
float radius;
float drive, steer, brake1, brake2;
float force, dampening;
};
struct PVehicleTypePart {
std::string name, parentname;
int parent;
PReferenceFrame ref_local;
std::vector<vehicle_clip_s> clip;
std::vector<PVehicleTypeWheel> wheel;
std::vector<PReferenceFrame> flame;
float scale;
PModel *model;
};
class PVehicleType : public PResource {
public:
std::string proper_name;
int coretype;
float mass;
vec3f dims;
std::vector<PVehicleTypePart> part;
float wheelscale;
PModel *wheelmodel;
PDriveSystem dsys;
float inverse_drive_total;
float wheel_speed_multiplier;
public:
struct {
// shared
float speed;
vec3f turnspeed;
float turnspeed_a, turnspeed_b; // turnspeed = a + b * speed
vec3f drag;
float angdrag;
vec2f lift; // x = fin lift (hz), y = wing lift (vt)
vec2f fineffect; // x = rudder/fin (hz), y = tail (vt)
} param;
v_control_s ctrlrate;
public:
PVehicleType() { }
~PVehicleType() { unload(); }
public:
bool load(const std::string &filename, PSSModel &ssModel);
void unload();
};
struct PVehicleWheel {
float ride_pos, ride_vel; // ride = suspension travel
float spin_pos, spin_vel; // spin = driving axis rotation
float turn_pos; // turn = steering axis rotation
PReferenceFrame ref_world;
float skidding, dirtthrow;
vec3f dirtthrowpos, dirtthrowvec;
float bumplast, bumpnext, bumptravel;
PVehicleWheel() {
ride_pos = 0.0f;
ride_vel = 0.0f;
spin_pos = 0.0f;
spin_vel = 0.0f;
turn_pos = 0.0f;
bumplast = 0.0f;
bumpnext = 0.0f;
bumptravel = 0.0f;
}
};
struct PVehiclePart {
// ref_local is initted from vehicle type, but may change per-vehicle
PReferenceFrame ref_local, ref_world;
std::vector<PVehicleWheel> wheel;
};
class PVehicle {
//class PVehicle : public NetObject {
//typedef NetObject Parent;
public:
PSim &sim;
PVehicleType *type;
PRigidBody *body;
std::vector<PVehiclePart> part;
v_state_s state;
PDriveSystemInstance dsysi;
// helicopter-specific
float blade_ang1;
int nextcp;
// for vehicle resetting, after being flipped
float reset_trigger_time;
vec3f reset_pos;
quatf reset_ori;
float reset_time;
// for body crash/impact noises
float crunch_level, crunch_level_prev;
public:
v_control_s ctrl;
// info
float forwardspeed;
float wheel_angvel;
float wheel_speed;
float skid_level;
public:
/*
enum StateBits {
InitialBit = BIT(0),
KinematicsBit = BIT(1),
ControlBit = BIT(2),
};
*/
public:
PVehicle(PSim &sim_parent, PVehicleType *_type);
//~PVehicle() { unload(); } // body unloaded by sim
public:
PRigidBody &getBody() { return *body; }
/*
// NetObject stuff
void performScopeQuery(GhostConnection *connection);
U32 packUpdate(GhostConnection *connection, U32 updateMask, BitStream *stream);
void unpackUpdate(GhostConnection *connection, BitStream *stream);
*/
void tick(float delta);
void updateParts();
void doReset();
float getEngineRPM() { return dsysi.getEngineRPM(); }
int getCurrentGear() { return dsysi.getCurrentGear(); }
bool getFlagGearChange() { return dsysi.getFlagGearChange(); }
float getCrashNoiseLevel() {
if (crunch_level > crunch_level_prev) {
float tmp = crunch_level - crunch_level_prev;
crunch_level_prev = crunch_level;
return tmp;
} else {
return 0.0f;
}
}
float getWheelSpeed() { return wheel_speed; }
float getSkidLevel() { return skid_level; }
};