00001 #ifndef GSGL_PHYSICS_RIGID_BODY_H 00002 #define GSGL_PHYSICS_RIGID_BODY_H 00003 00004 // 00005 // $Id: rigid_body.hpp 2 2008-03-01 20:58:50Z kulibali $ 00006 // 00007 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are 00011 // met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, 00014 // this list of conditions and the following disclaimer. 00015 // 00016 // * Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // * Neither the name of the The Periapsis Project nor the names of its 00021 // contributors may be used to endorse or promote products derived from 00022 // this software without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00025 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 // 00036 00037 #include "physics/physics.hpp" 00038 #include "physics/physics_frame.hpp" 00039 00040 #include "math/vector.hpp" 00041 #include "math/transform.hpp" 00042 #include "math/quaternion.hpp" 00043 #include "math/solver.hpp" 00044 00045 namespace gsgl 00046 { 00047 00048 namespace physics 00049 { 00050 00051 class rigid_body; 00052 00053 00054 struct PHYSICS_API rigid_body_state 00055 { 00056 rigid_body *parent_body; 00057 00058 // state variables 00059 math::vector x; // position of the center of mass 00060 math::quaternion q; // orientation 00061 math::vector p; // linear momentum 00062 math::vector L; // angular momentum 00063 00064 rigid_body_state & operator= (const rigid_body_state & s); 00065 00066 rigid_body_state operator+ (const rigid_body_state & s) const; 00067 rigid_body_state operator* (const double & n) const; 00068 rigid_body_state derivative(const double & t) const; 00069 }; // class rigid_body_state 00070 00071 00072 00073 class PHYSICS_API rigid_body 00074 : public physics_frame 00075 { 00076 protected: 00077 // constants 00078 math::vector center_of_mass; 00079 00080 gsgl::real_t mass, mass_inverse; 00081 math::transform jbody, jbody_inverse; 00082 00083 // state variables 00084 rigid_body_state cur_state; 00085 friend struct rigid_body_state; 00086 00087 // derived quantities 00088 math::transform R; // orientation 00089 math::vector v; // linear velocity 00090 math::vector w; // angular velocity 00091 00092 math::transform j_inverse; // inverse inertia tensor in world coordinates 00093 00094 // computed quantities 00095 math::vector force, torque; 00096 00097 // differential equation solver 00098 math::solver<rigid_body_state> *motion_solver; 00099 00100 public: 00101 rigid_body(const data::config_record & obj_config); 00102 virtual ~rigid_body(); 00103 00104 gsgl::real_t & get_total_mass() { return mass; } 00105 00106 virtual math::transform calculate_inertia_tensor(math::vector & center_of_mass) = 0; 00107 00108 /// \note Forces & torques are in world coordinates, and the time is in UNIX time (seconds)! 00109 virtual void calculate_force_and_torque(const double & t, math::vector & force, math::vector & torque) = 0; 00110 00111 // node implementation 00112 virtual void init(gsgl::scenegraph::context *c); 00113 virtual void update(gsgl::scenegraph::context *c); 00114 00115 private: 00116 void compute_derived_quantities(); 00117 }; // class rigid_body 00118 00119 } // namespace physics 00120 00121 } // namespace gsgl 00122 00123 #endif