[7f9531]: thrust.c Maximize Restore History

Download this file

thrust.c    154 lines (134 with data), 4.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
/* Asteroids3D - a first person game of blowing up asteroids
* Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
* Copyright Š Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2003 - 2005
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Stuart Pomerantz
* 3935 Stonecliffe drive
* Monroeville, PA 15146
*/
#include <stdlib.h>
#include "asteroids3D.h"
#include "vecops.h"
// Maximum thrust per unit time
#define MAX_TPS 25.0
/*
* 1) thrust
* is the the vector which sums the thrust vectors which the user has input but
* which have not yet been applied. This is a "request".
*
* 2) velocity
* is the sum of the parts of the thrust vector which have been applied (by the
* engine) and are now acting (in a newtonian sense) to move the ship.
*/
Vector thrust, velocity;
void init_thrust(void) {
vec_zero(&thrust);
vec_zero(&velocity);
vec_zero(&rotation);
return;
}
void apply_thrust(int value) {
Vector t; /* thrust which is actually applied */
double delta_thrust;
/*
* Here's what's going on:
*
* dt is the time since the loop was last processed, "delta t"
*
* So if its been 0.25 seconds since the loop was last processed
* and MAX_TPS ( max thrust per second ) is 1 (m/s) then this
* time throught the loop take thrust 0.25 m/s and save the next 0.75
* for the next time around. Of course, if MAX_TPS is, say 5, then
* you have to take 5*0.25 to get the amount of thrust that can potentially
* be applied at this time.
*
* If it turns out that this amount of thrust is greater than what's left to
* be applied, then just apply what's left.
*
* and do this for all umpteen vectors.
*
* note that another approach could be to take these three if
* statements out of here -- performance ?! -- and put them in
* stop() below, because currently, that's the only time
* thrust may be > MAX_TINPUT.
*/
/* note also that since last_time = 0 zero on the very first run
* through this loop, there could be a huge blow up if somebody is
* holding a key down at the start of the game.
*/
delta_thrust = dt*MAX_TPS ;
if( abs(thrust.x) > delta_thrust ) {
if( thrust.x > 0 ) {
t.x = delta_thrust ;
thrust.x -= delta_thrust ;
} else {
t.x = -delta_thrust ;
thrust.x += delta_thrust ;
}
} else {
t.x = thrust.x ;
thrust.x = 0 ;
}
if( abs(thrust.y) > delta_thrust ) {
if( thrust.y > 0 ) {
t.y = delta_thrust ;
thrust.y -= delta_thrust ;
} else {
t.y = -delta_thrust ;
thrust.y += delta_thrust ;
}
} else {
t.y = thrust.y ;
thrust.y = 0 ;
}
if( abs(thrust.z) > delta_thrust ) {
if( thrust.z > 0 ) {
t.z = delta_thrust ;
thrust.z -= delta_thrust ;
} else {
t.z = -delta_thrust ;
thrust.z += delta_thrust ;
}
} else {
t.z = thrust.z ;
thrust.z = 0 ;
}
/* apply the thrust */
vec_add(&velocity, &velocity, &t);
return ;
}
void process_motion(int value) {
vec_addmul(&pos, &pos, dt, &velocity);
/* notice that after the rotation is done the rotational_vel is set to
* zero because it would absolutely suck to continue rotating after
* the users's rotation request is fullfilled. That's the way real life
* works, but who lives in reality anyway ?
*/
rotate_view_aboutx( rotation.x*dt ) ;
rotate_view_abouty( rotation.y*dt ) ;
rotate_view_aboutz( rotation.z*dt) ;
vec_zero(&rotation);
return ;
}
void stop(void) {
/* this applies thrust in the exact opposite
* direction of your current vector of velocity.
*/
vec_sub(&thrust, &ZERO_VEC, &velocity);
return ;
}