00001
00002
00003
00004
00005
00006
00007
00008 #ifndef RIGIDBODY_H_
00009 #define RIGIDBODY_H_
00010
00011 #include "../Common/Vector.h"
00012 #include <vector>
00013 #include "../Common/Constants.h"
00014
00015
00016 class RigidBody {
00017 protected:
00018 Vector position, velocity, rotation;
00019 real inverseMass;
00020 real radius;
00021 bool awake;
00022 real motion;
00023 std::vector<RigidBody *> neighbors;
00024 int number;
00025 public:
00026 RigidBody() {motion=0.0;awake = false;};
00027 real calculateEnergy() const {
00028 return velocity*velocity;
00029 }
00030 void integrate(real duration);
00031 bool isAwake() const {
00032 return awake;
00033 }
00034 void setAwake(const bool b) {
00035 this->awake = b;
00036 if(!b) {
00037 velocity.reset();
00038 rotation.reset();
00039 motion = 0;
00040 }
00041 }
00042 void resolveCollision(const RigidBody & body) {
00043
00044 real d = calculateSquaredDistance(body);
00045
00046 if(d*d<4*radius*radius||(d*d)) {
00047 Vector normal = calculateNormalVector(body);
00048
00049 }
00050 }
00051
00052 Vector calculateVelChange(real duration)const ;
00053 real calculateSquaredDistance(const RigidBody & body) const {
00054 return (body.position-this->position).squareMagnitude();
00055 }
00056
00057 Vector calculateNormalVector(const RigidBody & body) const {
00058 Vector mid = (this->position);
00059 mid -= body.position;
00060 return (real)1.0/mid.magnitude()*mid;
00061 }
00062
00063 Vector getVelocity() const {
00064 return velocity;
00065 }
00066
00067 Vector getPosition() const {
00068 return position;
00069 }
00070
00071 int getNumber() const {
00072 return number;
00073 }
00074
00075 void setNumber(int _num) {
00076 number = _num;
00077 }
00078 void updatePosition(const Vector& v) {
00079 position += v;
00080 }
00081
00082 void updateVelocity(const Vector& v) {
00083 velocity +=v;
00084
00085
00086 }
00087 void setVelocity(const Vector&v) {
00088 velocity = v;
00089
00090
00091 }
00092
00093 void findNeighbors(std::vector<RigidBody *>& bodies) {
00094 neighbors.clear();
00095 for(std::vector<RigidBody *>::iterator it = bodies.begin(); it!=bodies.end();++it) {
00096 if(*it==this)
00097 continue;
00098 if(((*it)->getPosition()-position).squareMagnitude()<=1.02*4*BALLRADIUS*BALLRADIUS)
00099 neighbors.push_back(*it);
00100 }
00101
00102
00103
00104
00105 }
00106
00107 bool isPartOfGroup() const {
00108 return neighbors.size() > 1;
00109 }
00110
00111 RigidBody * findLeft(Vector direction) {
00112 RigidBody * founded = NULL;
00113 Vector relPos;
00114 real sin,cos;
00115
00116
00117 real max = 1.0;
00118 for(std::vector<RigidBody *>::iterator it = neighbors.begin();it!=neighbors.end();++it) {
00119 relPos = (*it)->getPosition()-position;
00120 cos = relPos.scalarProduct(direction);
00121 if(cos>0) {
00122 sin = direction.getX()*relPos.unit().getZ()-direction.getZ()*relPos.unit().getX();
00123 if(sin>=0.0 && sin < max) {
00124 founded = *it;
00125 max = sin;
00126 }
00127 }
00128 }
00129 return founded;
00130 }
00131
00132 RigidBody * findRight(Vector direction) {
00133 RigidBody * founded = NULL;
00134 Vector relPos;
00135 real sin,cos;
00136
00137
00138 real max = -1.0;
00139 for(std::vector<RigidBody *>::iterator it = neighbors.begin();it!=neighbors.end();++it) {
00140 relPos = (*it)->getPosition()-position;
00141 cos = relPos.scalarProduct(direction);
00142 if(cos>0) {
00143 sin = direction.getX()*relPos.unit().getZ()-direction.getZ()*relPos.unit().getX();
00144 if(sin<=0.0 && sin > max) {
00145 founded = *it;
00146 max = sin;
00147 }
00148 }
00149 }
00150 return founded;
00151 }
00152 };
00153
00154
00155 #endif