00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef PHYZ_WORLD_H
00022 #define PHYZ_WORLD_H
00023
00024 #include "csphyzik/phyztype.h"
00025 #include "csphyzik/entity.h"
00026 #include "csphyzik/phyzent.h"
00027 #include "csphyzik/linklist.h"
00028 #include "csphyzik/odesolve.h"
00029 #include "csphyzik/ctcat.h"
00030 #include "csutil/csdllist.h"
00031
00032 enum worldstate { CTWS_NORMAL, CTWS_REWOUND };
00033
00034 enum errorcode { WORLD_NOERR, WORLD_ERR_NULLPARAMETER, WORLD_ERR_NOODE,
00035 WORLD_ERR_SHITHAPPEND, WORLD_ERR_OTHERSTUFF };
00036
00037 #define DEFAULT_INIT_MAX_STATE_SIZE 1024
00038 #define STATE_RESIZE_EXTRA 256
00039
00040 class ctForce;
00041
00042 class AllocNode
00043 {
00044 public:
00045 int offset;
00046 int size;
00047 };
00048
00049 class ctWorld : public ctEntity
00050 {
00051 public:
00052
00053 ctWorld ();
00055 virtual ~ctWorld ();
00056
00057 void calc_delta_state ( real t, const real y[], real ydot[] );
00058
00060 errorcode evolve ( real t1, real t2 );
00061
00066 errorcode rewind ( real t1, real t2 );
00067
00069 void set_max_time_subdivisions ( long pmt )
00070 { max_time_subdivisions = pmt; }
00071
00072 void register_catastrophe_manager ( ctCatastropheManager *pcm );
00073
00074 void solve ( real t );
00075 errorcode add_entity ( ctEntity *pe );
00076 errorcode add_enviro_force ( ctForce *f );
00077
00078 errorcode delete_entity ( ctEntity *pb );
00079
00081 void set_ODE_solver ( OdeSolver *pode )
00082 {
00083 if( ode_to_math ) delete ode_to_math;
00084 ode_to_math = pode;
00085 }
00086
00088 void apply_function_to_body_list ( void(*fcn)( ctEntity *pe ) );
00089
00095 virtual void dydt_eval (real t, const real y[], real dy[]);
00096
00098 void resolve_collision ( ctCollidingContact *cont );
00099 ctVector3 get_relative_v
00100 ( ctPhysicalEntity *body_a, ctPhysicalEntity *body_b, const ctVector3 &the_p );
00101
00102 virtual int get_state_size ()
00103 { return 0; }
00105 virtual int set_state ( real * )
00106 { return 0; }
00108 virtual int get_state ( const real * )
00109 { return 0; }
00111 virtual int set_delta_state ( real * )
00112 { return 0; }
00113
00115 int state_size;
00116 csDLinkList free_blocks;
00117 csDLinkList used_blocks;
00118
00120 virtual int state_alloc (int size);
00121 virtual void state_free (int offset);
00122 virtual int state_realloc (int offset, int newsize)
00123 {
00124 int newloc = state_alloc(newsize);
00125 if(!newloc) return 0;
00126
00127 state_free(offset);
00128 return newloc;
00129 }
00130
00132
00138 AllocNode *sa_make_unused (int offset);
00139
00144 bool sa_make_used (AllocNode *block);
00145
00146 protected:
00148 errorcode do_time_step( real t1, real t2 );
00149
00154 void load_state ( real *state_array );
00155
00161 void reintegrate_state ( const real *state_array );
00162
00164 void load_delta_state ( real *state_array );
00165
00166 void init_state ();
00167
00168 void collide ();
00169
00170 void resize_state_vector ( long new_size );
00171
00173 worldstate fsm_state;
00174 real rewound_from;
00175
00176 ctLinkList<ctEntity> body_list;
00177 ctLinkList<ctForce> enviro_force_list;
00178 ctLinkList<ctCatastropheManager> catastrophe_list;
00179
00181 OdeSolver *ode_to_math;
00182
00184
00186 real *y_save;
00187 real *y0;
00188 real *y1;
00189
00192 bool was_catastrophe_last_frame;
00193
00195 long max_time_subdivisions;
00196
00197 long y_save_size;
00198 long max_state_size;
00199 };
00200
00201
00202 #endif