00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "linearEulerIntegrator.h"
00020 #include "forceNode.h"
00021 #include "physicalNode.h"
00022 #include "config_physics.h"
00023
00024
00025
00026
00027
00028
00029 LinearEulerIntegrator::
00030 LinearEulerIntegrator(void) {
00031 }
00032
00033
00034
00035
00036
00037
00038 LinearEulerIntegrator::
00039 ~LinearEulerIntegrator(void) {
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049 void LinearEulerIntegrator::
00050 child_integrate(Physical *physical,
00051 pvector< PT(LinearForce) >& forces,
00052 float dt) {
00053 pvector< PT(PhysicsObject) >::const_iterator current_object_iter;
00054
00055
00056
00057
00058
00059
00060 precompute_linear_matrices(physical, forces);
00061 const pvector< LMatrix4f > &matrices = get_precomputed_linear_matrices();
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 current_object_iter = physical->get_object_vector().begin();
00072 for (; current_object_iter != physical->get_object_vector().end();
00073 current_object_iter++) {
00074 LVector3f md_accum_vec, non_md_accum_vec, accel_vec, vel_vec;
00075 LPoint3f pos;
00076 float mass;
00077
00078 PhysicsObject *current_object = *current_object_iter;
00079
00080
00081
00082 if (current_object == (PhysicsObject *) NULL)
00083 continue;
00084
00085 if (current_object->get_active() == false)
00086 continue;
00087
00088
00089 md_accum_vec.set(0.0f, 0.0f, 0.0f);
00090 non_md_accum_vec.set(0.0f, 0.0f, 0.0f);
00091
00092
00093 LVector3f f;
00094
00095
00096 ForceNode *force_node;
00097 pvector< PT(LinearForce) >::const_iterator f_cur;
00098
00099
00100 f_cur = forces.begin();
00101 int index = 0;
00102 for (; f_cur != forces.end(); f_cur++) {
00103 LinearForce *cur_force = *f_cur;
00104
00105
00106 if (cur_force->get_active() == false)
00107 continue;
00108
00109 force_node = cur_force->get_force_node();
00110
00111
00112 f = cur_force->get_vector(current_object) * matrices[index++];
00113
00114
00115 if (cur_force->get_mass_dependent() == true)
00116 md_accum_vec += f;
00117 else
00118 non_md_accum_vec += f;
00119 }
00120
00121
00122 f_cur = physical->get_linear_forces().begin();
00123 for (; f_cur != physical->get_linear_forces().end(); f_cur++) {
00124 LinearForce *cur_force = *f_cur;
00125
00126
00127 if (cur_force->get_active() == false)
00128 continue;
00129
00130 force_node = cur_force->get_force_node();
00131
00132
00133 f = cur_force->get_vector(current_object) * matrices[index++];
00134
00135
00136 if (cur_force->get_mass_dependent() == true)
00137 md_accum_vec += f;
00138 else
00139 non_md_accum_vec += f;
00140 }
00141
00142
00143 pos = current_object->get_position();
00144 vel_vec = current_object->get_velocity();
00145 mass = current_object->get_mass();
00146
00147
00148
00149 nassertv(mass != 0.0f);
00150
00151 accel_vec = md_accum_vec / mass;
00152 accel_vec += non_md_accum_vec;
00153
00154
00155 vel_vec += accel_vec * dt;
00156
00157
00158 float len = vel_vec.length();
00159
00160 if (len > current_object->get_terminal_velocity()) {
00161
00162 vel_vec *= current_object->get_terminal_velocity() / len;
00163 }
00164
00165 pos += vel_vec * dt;
00166
00167
00168 current_object->set_position(pos);
00169 current_object->set_velocity(vel_vec);
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178
00179