FS2_Open
Open source remastering of the Freespace 2 engine
physics.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef _PHYSICS_H
13 #define _PHYSICS_H
14 
15 #include "math/vecmat.h"
16 
17 
18 #define PF_ACCELERATES (1 << 1)
19 #define PF_USE_VEL (1 << 2) // Use velocity present in physics_info struct, don't call physics_sim_vel.
20 #define PF_AFTERBURNER_ON (1 << 3) // Afterburner currently engaged.
21 #define PF_SLIDE_ENABLED (1 << 4) // Allow descent style sliding
22 #define PF_REDUCED_DAMP (1 << 5) // Allows reduced damping on z (for death, shockwave) (CAN be reset in physics)
23 #define PF_IN_SHOCKWAVE (1 << 6) // Indicates whether object has recently been hit by shockwave (used to enable shake)
24 #define PF_DEAD_DAMP (1 << 7) // Makes forward damping same as sideways (NOT reset in physics)
25 #define PF_AFTERBURNER_WAIT (1 << 8) // true when afterburner cannot be used. replaces variable used in afterburner code
26 #define PF_CONST_VEL (1 << 9) // Use velocity in phys_info struct. Optimize weapons in phys_sim
27 #define PF_WARP_IN (1 << 10) // Use when ship is warping in
28 #define PF_SPECIAL_WARP_IN (1 << 11) // Use when ship is warping in and we want to slow the ship faster than normal game physics
29 #define PF_WARP_OUT (1 << 12) // Use when ship is warping out
30 #define PF_SPECIAL_WARP_OUT (1 << 13) // Use when ship is warping out and we want to slow the ship faster than normal game physics
31 #define PF_BOOSTER_ON (1 << 14)
32 #define PF_GLIDING (1 << 15)
33 #define PF_FORCE_GLIDE (1 << 16)
34 
35 //information for physics sim for an object
36 typedef struct physics_info {
37  uint flags; //misc physics flags
38 
39  float mass; //the mass of this object
40  vec3d center_of_mass; // Goober5000 - this is never ever used by physics; currently physics assumes the center of an object is the center of mass
41  matrix I_body_inv; // inverse moment of inertia tensor (used to calculate rotational effects)
42 
43  float rotdamp; //rotational velocity damping
44  float side_slip_time_const; // time const for achieving desired velocity in the local sideways direction
45  // value should be zero for no sideslip and increase depending on desired slip
46 
47  float delta_bank_const; //const that heading is multiplied by. 0 means no delta bank.
48 
49  vec3d max_vel; //maximum foward velocity in x,y,z
50  vec3d afterburner_max_vel; // maximum foward velocity in x,y,z while afterburner engaged
52  vec3d max_rotvel; //maximum p,b,h rotational velocity
53  float max_rear_vel; //maximum velocity in the backwards Z direction
54 
55  // Acceleration rates. Only used if flag PF_ACCELERATES is set
56  // starting from rest time to reach .50 v_max 0.69 time const
57  // time to reach .75 v_max 1.39 time const
58  //
59  float forward_accel_time_const; // forward acceleration time const
60  float afterburner_forward_accel_time_const; // forward acceleration time const while afterburner engaged
62  float forward_decel_time_const; // forward deceleration time const
63  float slide_accel_time_const; // slide acceleration time const
64  float slide_decel_time_const; // slide deceleration time const
65  float shockwave_shake_amp; // amplitude of shockwave shake at onset
66 
67  // These get changed by the control code. The physics uses these
68  // as input values when doing physics.
69  vec3d prev_ramp_vel; // follows the user's desired velocity
70  vec3d desired_vel; // in world coord
71  vec3d desired_rotvel; // in local coord
72  float forward_thrust; // How much the forward thruster is applied. 0-1.
73  float side_thrust; // How much the forward thruster is +x. 0-1.
74  float vert_thrust; // How much the forward thruster is +y. 0-1.
75 
76  // Data that changes each frame. Physics fills these in each frame.
77  vec3d vel; // The current velocity vector of this object
78  vec3d rotvel; // The current rotational velecity (angles)
79  float speed; // Yes, this can be derived from velocity, but that's expensive!
80  float fspeed; // Speed in the forward direction.
81  float heading;
82  vec3d prev_fvec; // Used in AI for momentum.
83  matrix last_rotmat; // Used for moving two objects together and for editor.
84 
85  int afterburner_decay; // timestamp used to control how long ship shakes after afterburner released
86  int shockwave_decay; // timestamp used to control how long ship affected after hit by shockwave
87  int reduced_damp_decay; // timestamp used to control how long ship ship has reduced damp physics
88 
89  float glide_cap; //Backslash - for 'newtonian'-style gliding, the cap on velocity (so that something can't accelerate to ridiculous speeds... unless allowed to)
90  float cur_glide_cap; //SUSHI: Used for dynamic glide cap, so we can use the ramping function on the glide cap
91  float glide_accel_mult; //SUSHI: The acceleration multiplier for glide mode. A value < 0 means use glide ramping instead
92  bool use_newtonian_damp; //SUSHI: Whether or not to use newtonian dampening
93  float afterburner_max_reverse_vel; //SparK: This is the reverse afterburners top speed vector
94  float afterburner_reverse_accel; //SparK: Afterburner's acceleration on reverse mode
95 } physics_info;
96 
97 // All of these are numbers from -1.0 to 1.0 indicating
98 // what percent of full velocity you want to go.
99 typedef struct control_info {
100  float pitch; // -1.0 to 1.0
101  float vertical;
102  float heading;
103  float sideways;
104  float bank;
105  float forward;
106  float forward_cruise_percent; // percentage used for forward cruising
107  // This is a special case from -100 to 100
108 
109  // below is information that are used by the player controls for firing information.
113  int fire_debug_count; // should this be around an NDEBUG #if/#endif?
114 
115  // afterburner control information
118 
119 } control_info;
120 
121 extern int physics_paused; // Set means don't do physics, except for player.
122 
123 // To use the "Descent-ship" physics:
124 // controls_read_all(&ci, FrameSecs );
125 // physics_read_flying_controls( &ViewerOrient, &ViewerPhysics, FrameSecs, &ci );
126 // physics_sim(&ViewerPos, &ViewerOrient, &ViewerPhysics, FrameSecs );
127 extern void physics_init( physics_info * pi );
128 extern void physics_read_flying_controls( matrix * orient, physics_info * pi, control_info * ci, float sim_time, vec3d *wash_rot=NULL);
129 extern void physics_sim(vec3d *position, matrix * orient, physics_info * pi, float sim_time );
130 extern void physics_sim_editor(vec3d *position, matrix * orient, physics_info * pi, float sim_time);
131 
132 extern void physics_sim_vel(vec3d * position, physics_info * pi, float sim_time, matrix * orient);
133 extern void physics_sim_rot(matrix * orient, physics_info * pi, float sim_time );
134 extern void physics_apply_whack(vec3d *force, vec3d *pos, physics_info *pi, matrix *orient, float mass);
135 extern void physics_apply_shock(vec3d *direction_vec, float pressure, physics_info *pi, matrix *orient, vec3d *min, vec3d *max, float radius);
136 extern void physics_collide_whack(vec3d *impulse, vec3d *delta_rotvel, physics_info *pi, matrix *orient, bool is_landing);
138 
139 
140 
141 // functions which use physics calcs to predict position and velocity
142 void physics_predict_pos(physics_info *pi, float delta_time, vec3d *predicted_pos);
143 void physics_predict_vel(physics_info *pi, float delta_time, vec3d *predicted_vel);
144 void physics_predict_pos_and_vel(physics_info *pi, float delta_time, vec3d *predicted_vel, vec3d *predicted_pos);
145 
146 // If physics_set_viewer is called with the viewer's physics_info, then
147 // this variable tracks the viewer's bank. This is used for g3_draw_rotated_bitmap.
148 extern float Physics_viewer_bank;
149 
150 // If you would like Physics_viewer_bank to be tracked (Which is needed
151 // for rotating 3d bitmaps) call this and pass it a pointer to the
152 // viewer's physics info.
153 #define PHYSICS_VIEWER_FRONT 0
154 #define PHYSICS_VIEWER_LEFT 1
155 #define PHYSICS_VIEWER_RIGHT 2
156 #define PHYSICS_VIEWER_REAR 3
157 #define PHYSICS_VIEWER_UP 4
158 void physics_set_viewer( physics_info * p, int dir );
159 
160 //WMC - apply_physics
161 void apply_physics( float damping, float desired_vel, float initial_vel, float t, float * new_vel, float * delta_pos );
162 
163 //WMC - camera code type stuff
164 //Acceleration, constant Velocity, Deceleration (to another speed, so maybe not deceleration)
166 {
167 private:
168  //Current
169  float Pc; //Current position
170  float Vc; //Current velocity
171 
172  //Initial
173  int TSi; //Initial timestamp <-- note TIMESTAMP
174  float Pi; //Initial position
175  float Vi; //Initial velocity
176 
177  //Given
178  float Pf; //Final position
179  float Tf; //Final duration
180  float Tai; //Starting acceleration duration
181  float Taf; //Ending acceleration duration
182  float Vf; //Final velocity
183 
184  //Calculated
185  float Vm; //Middle velocity
186  float Ai; //Starting acceleration
187  float Af; //Ending acceleration
188 public:
189  avd_movement();
190  void clear();
191 
192  void get(float Time, float *Position, float *Velocity);
193  void get(float *Position, float *Velocity);
194 
195  void set(float position);
196  void setAVD(float final_position, float total_movement_time, float starting_accleration_time, float ending_acceleration_time, float final_velocity);
197  void setVD(float total_movement_time, float ending_acceleration_time, float final_velocity);
198 
199  void update(float Time);
200  void update();
201 };
202 
203 /*
204 #ifdef __cplusplus
205 }
206 #endif
207 */
208 
209 #endif // PHYSICS_H
int fire_countermeasure_count
Definition: physics.h:112
float max_rear_vel
Definition: physics.h:53
float cur_glide_cap
Definition: physics.h:90
void physics_sim(vec3d *position, matrix *orient, physics_info *pi, float sim_time)
Definition: physics.cpp:364
int afterburner_start
Definition: physics.h:116
void setVD(float total_movement_time, float ending_acceleration_time, float final_velocity)
Definition: physics.cpp:1227
int physics_paused
Definition: object.cpp:677
vec3d rotvel
Definition: physics.h:78
void update()
Definition: physics.cpp:1246
void physics_init(physics_info *pi)
Definition: physics.cpp:49
vec3d desired_vel
Definition: physics.h:70
float afterburner_forward_accel_time_const
Definition: physics.h:60
void set(float position)
Definition: physics.cpp:1188
bool use_newtonian_damp
Definition: physics.h:92
void physics_apply_shock(vec3d *direction_vec, float pressure, physics_info *pi, matrix *orient, vec3d *min, vec3d *max, float radius)
Definition: physics.cpp:907
int fire_secondary_count
Definition: physics.h:111
int fire_primary_count
Definition: physics.h:110
Definition: pstypes.h:88
float side_slip_time_const
Definition: physics.h:44
void physics_sim_vel(vec3d *position, physics_info *pi, float sim_time, matrix *orient)
Definition: physics.cpp:263
void physics_read_flying_controls(matrix *orient, physics_info *pi, control_info *ci, float sim_time, vec3d *wash_rot=NULL)
Definition: physics.cpp:449
vec3d max_vel
Definition: physics.h:49
float side_thrust
Definition: physics.h:73
float afterburner_reverse_accel
Definition: physics.h:94
hull_check orient
Definition: lua.cpp:5049
vec3d booster_max_vel
Definition: physics.h:51
float booster_forward_accel_time_const
Definition: physics.h:61
matrix I_body_inv
Definition: physics.h:41
vec3d max_rotvel
Definition: physics.h:52
float forward_decel_time_const
Definition: physics.h:62
int afterburner_decay
Definition: physics.h:85
float delta_bank_const
Definition: physics.h:47
float pitch
Definition: physics.h:100
vec3d desired_rotvel
Definition: physics.h:71
struct control_info control_info
unsigned int uint
Definition: pstypes.h:64
void physics_apply_whack(vec3d *force, vec3d *pos, physics_info *pi, matrix *orient, float mass)
Definition: physics.cpp:776
float rotdamp
Definition: physics.h:43
struct physics_info physics_info
int fire_debug_count
Definition: physics.h:113
float Physics_viewer_bank
Definition: physics.cpp:128
float slide_decel_time_const
Definition: physics.h:64
float speed
Definition: physics.h:79
int reduced_damp_decay
Definition: physics.h:87
float forward
Definition: physics.h:105
void apply_physics(float damping, float desired_vel, float initial_vel, float t, float *new_vel, float *delta_pos)
Definition: physics.cpp:108
int check_rotvel_limit(physics_info *pi)
Definition: physics.cpp:1064
void physics_predict_pos(physics_info *pi, float delta_time, vec3d *predicted_pos)
Definition: physics.cpp:394
GLdouble GLdouble t
Definition: Glext.h:5329
void setAVD(float final_position, float total_movement_time, float starting_accleration_time, float ending_acceleration_time, float final_velocity)
Definition: physics.cpp:1194
vec3d center_of_mass
Definition: physics.h:40
vec3d afterburner_max_vel
Definition: physics.h:50
uint flags
Definition: physics.h:37
float glide_cap
Definition: physics.h:89
float bank
Definition: physics.h:104
vec3d prev_fvec
Definition: physics.h:82
float forward_cruise_percent
Definition: physics.h:106
int afterburner_stop
Definition: physics.h:117
void physics_predict_pos_and_vel(physics_info *pi, float delta_time, vec3d *predicted_vel, vec3d *predicted_pos)
Definition: physics.cpp:424
void physics_set_viewer(physics_info *p, int dir)
Definition: physics.cpp:135
vec3d vel
Definition: physics.h:77
void clear()
Definition: physics.cpp:1145
float fspeed
Definition: physics.h:80
float heading
Definition: physics.h:102
float forward_accel_time_const
Definition: physics.h:59
matrix last_rotmat
Definition: physics.h:83
GLfloat GLfloat p
Definition: Glext.h:8373
float slide_accel_time_const
Definition: physics.h:63
float vertical
Definition: physics.h:101
void physics_predict_vel(physics_info *pi, float delta_time, vec3d *predicted_vel)
Definition: physics.cpp:407
float afterburner_max_reverse_vel
Definition: physics.h:93
hull_check pos
Definition: lua.cpp:5050
float sideways
Definition: physics.h:103
float shockwave_shake_amp
Definition: physics.h:65
vec3d prev_ramp_vel
Definition: physics.h:69
int shockwave_decay
Definition: physics.h:86
float mass
Definition: physics.h:39
void physics_sim_rot(matrix *orient, physics_info *pi, float sim_time)
Definition: physics.cpp:151
float glide_accel_mult
Definition: physics.h:91
float vert_thrust
Definition: physics.h:74
float heading
Definition: physics.h:81
void physics_collide_whack(vec3d *impulse, vec3d *delta_rotvel, physics_info *pi, matrix *orient, bool is_landing)
Definition: physics.cpp:1026
float forward_thrust
Definition: physics.h:72
void physics_sim_editor(vec3d *position, matrix *orient, physics_info *pi, float sim_time)
Definition: physics.cpp:385