00001 #include "Point.h"
00002 #include <QDebug>
00003
00004 #include "System.h"
00005 #include "PointConstraint.h"
00006
00007 #define MAX_FORCE 1e14
00008
00009 namespace Elve {
00010
00011 Point::Point(qreal mass, const NodeID &id, System& sys) : mM(mass), mContainerData(nullptr), mID(id), mSys(sys)
00012 {
00013
00014 }
00015
00016 bool Point::moved() const
00017 {
00018 return true;
00019 }
00020
00021 const QVector2D& Point::pos() const
00022 {
00023 return mPos;
00024 }
00025
00026 void Point::addMovable(Movable *m)
00027 {
00028 m->setPoint(this);
00029 mMovables.insert(m);
00030 }
00031
00032 void Point::removeMovable(Movable *m)
00033 {
00034 mMovables.erase(m);
00035 }
00036
00037 const QVector2D& Point::speed() const
00038 {
00039 return mSpeed;
00040 }
00041
00042 const qreal& Point::mass() const {
00043 return mM;
00044 }
00045
00046 void Point::setMass(qreal mass) {
00047 mM = mass;
00048 }
00049
00050 void Point::addForce(Force *force) {
00051 mForces.insert(force);
00052 }
00053
00054 void Point::clearContraints() {
00055 mConstraints.clear();
00056 }
00057
00058 void Point::addConstraint(Constraint* c)
00059 {
00060 mConstraints.insert(c);
00061 }
00062
00063 void Point::resetForce()
00064 {
00065 mForce = QVector2D(0,0);
00066 }
00067
00068 void Point::computeForce()
00069 {
00070 for(const Force* j : mForces) {
00071 QVector2D force = j->force(*this);
00072 if(force.length() < MAX_FORCE) {
00073 mForce += force;
00074 }
00075 }
00076 }
00077
00078 const NodeID& Point::boundID() const {
00079 return mID;
00080 }
00081
00082 void Point::setPos(const QVector2D& pos)
00083 {
00084 mPos = pos;
00085 }
00086
00087 void Point::setSpeed(const QVector2D& speed)
00088 {
00089 mSpeed = speed;
00090 }
00091
00092 void Point::tick(float dt, bool update)
00093 {
00094 QVector2D a = mForce / mM;
00095
00096 mSpeed += a * dt;
00097 mPos += mSpeed*dt;
00098
00099 for(Constraint* c : mConstraints) {
00100 c->constrain(*this);
00101 }
00102
00103 if(moved() && update) {
00104 notify();
00105 }
00106 }
00107
00108 void Point::notify() {
00109 for(Movable* m : mMovables) {
00110 m->onStateChange(mPos,mSpeed);
00111 }
00112 }
00113
00114 void Point::removePConstraints() {
00115 for(auto it = mConstraints.begin(); it != mConstraints.end(); it++) {
00116 PointConstraint* pc = dynamic_cast<PointConstraint*>(*it);
00117 if(pc) {
00118 delete *it;
00119 mConstraints.erase(it);
00120 }
00121 }
00122 }
00123
00124 bool Point::pinned() const {
00125 return mSys.pinnedPoints().count(mID);
00126 }
00127
00128 void Point::pin() {
00129 mSys.pin(mID,pos());
00130 }
00131
00132 void Point::unpin() {
00133 mSys.unpin(mID);
00134 }
00135
00136 void Point::clearMovables() {
00137 mMovables.clear();
00138 }
00139
00140 void* Point::containerData() const
00141 {
00142 return mContainerData;
00143 }
00144 void Point::setContainerData(void* data) const
00145 {
00146 mContainerData = data;
00147 }
00148
00149 }