1 /* 2 Copyright (C) 2005 by Ruben Henner Zilibowitz <rzilibowitz@users.sourceforge.net> 3 Part of the Toy Cars Project http://toycars.sourceforge.net 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the license. 7 This program is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY. 9 10 See the COPYING file for more details. 11 */ 12 13 /* 14 * Created by Ruben on Wed Apr 21 2004. 15 */ 16 17 #ifndef RIGIDBODY_H 18 #define RIGIDBODY_H 19 20 #ifdef HAVE_CONFIG_H 21 #include "config.h" 22 #endif 23 24 #include "Geom.h" 25 26 // RigidBody class 27 // A two-dimensional rigid body with kinematic properties and an associated geometry 28 // or shape used to check for collisions between two or more different rigid bodies. 29 30 class RigidBody 31 { 32 friend class Contact; 33 34 public: 35 RigidBody(); 36 37 RigidBody( double inMass, 38 double inInertia, 39 Tuple inLocation_wc, 40 Tuple inVelocity_wc, 41 double inOrientation_wc, 42 double inAngularVelocity, 43 const Geom& inGeom); 44 ~RigidBody()45 virtual ~RigidBody() {} 46 47 virtual void compute_force_and_torque(); 48 49 virtual void rk2_step(double dt); 50 51 void compute_geom_world_coords(); 52 53 protected: 54 double mMass; // kg 55 double mInertia; // kg m^2 56 57 double mInvMass; // 1/kg 58 double mInvInertia; // 1/(kg m^2) 59 60 Tuple mTotalForce_wc; // N 61 double mTotalTorque; // Nm 62 63 Tuple mLocation_wc; // m 64 Tuple mVelocity_wc; // m/s 65 Tuple mAcceleration_wc; // m/s/s 66 67 double mOrientation_wc; // rad 68 double mAngularVelocity; // rad/s 69 double mAngularAcceleration; // rad/s/s 70 71 Tuple mNorm; // normal vector for orientation 72 73 const Geom mGeom; // fixed shape in local coordinates 74 Geom mGeom_wc; // above transformed to world coordinates 75 double mGeomRadius; // distance from centre to furthest point on geometry 76 77 public: 78 // accessor functions 79 getMass()80 double getMass() const { return mMass; } getInertia()81 double getInertia() const { return mInertia; } 82 getInvMass()83 double getInvMass() const { return mInvMass; } getInvInertia()84 double getInvInertia() const { return mInvInertia; } 85 getTotalForce_wc()86 Tuple getTotalForce_wc() const { return mTotalForce_wc; } getTotalTorque()87 double getTotalTorque() const { return mTotalTorque; } 88 getLocation_wc()89 Tuple getLocation_wc() const { return mLocation_wc; } getVelocity_wc()90 Tuple getVelocity_wc() const { return mVelocity_wc; } getAcceleration_wc()91 Tuple getAcceleration_wc() const { return mAcceleration_wc; } 92 getOrientation_wc()93 double getOrientation_wc() const { return mOrientation_wc; } getAngularVelocity()94 double getAngularVelocity() const { return mAngularVelocity; } getAngularAcceleration()95 double getAngularAcceleration() const { return mAngularAcceleration; } 96 getNorm()97 Tuple getNorm() const { return mNorm; } 98 getGeom()99 const Geom& getGeom() const { return mGeom; } getGeom_wc()100 const Geom& getGeom_wc() const { return mGeom_wc; } getGeomRadius()101 double getGeomRadius() const { return mGeomRadius; } 102 103 // impulse 104 applyImpulse(Tuple impulse)105 void applyImpulse(Tuple impulse) { mVelocity_wc += impulse; } 106 setLocation(Tuple loc)107 void setLocation(Tuple loc) { mLocation_wc = loc; } setOrientation(double theta)108 void setOrientation(double theta) { mOrientation_wc = theta; } 109 110 // reset 111 resetVelocityAndAccel()112 void resetVelocityAndAccel() { 113 mVelocity_wc.x = 0; 114 mVelocity_wc.y = 0; 115 mAcceleration_wc.x = 0; 116 mAcceleration_wc.y = 0; 117 mAngularVelocity = 0; 118 mAngularAcceleration = 0; 119 } 120 }; 121 122 #endif 123