00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "motor.h"
00020 #include "rigidbody.h"
00021 #include "particle.h"
00022 #include <cmath>
00023
00024 namespace StepCore
00025 {
00026
00027 STEPCORE_META_OBJECT(LinearMotor, "Linear motor: applies a constant force to a given position of the body", 0,
00028 STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Force),
00029 STEPCORE_PROPERTY_RW(Object*, body, STEPCORE_UNITS_NULL, "Body", body, setBody)
00030 STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition, "m", "Position of the motor on a body", localPosition, setLocalPosition)
00031 STEPCORE_PROPERTY_RW(StepCore::Vector2d, forceValue, "N", "Value of the force, acting on the body", forceValue, setForceValue))
00032
00033 STEPCORE_META_OBJECT(CircularMotor, "Circular motor: applies a constant torque to the body", 0,
00034 STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Force),
00035 STEPCORE_PROPERTY_RW(Object*, body, STEPCORE_UNITS_NULL, "Body", body, setBody)
00036 STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition, "m", "Position of the motor on a body", localPosition, setLocalPosition)
00037 STEPCORE_PROPERTY_RW(double, torqueValue, "N m", "Value of the torque, acting on the body", torqueValue, setTorqueValue))
00038
00039
00040 LinearMotor::LinearMotor(Object* body, const Vector2d& localPosition, Vector2d forceValue)
00041 : _localPosition(localPosition), _forceValue(forceValue)
00042 {
00043 setBody(body);
00044 setColor(0xff0000ff);
00045 }
00046
00047 void LinearMotor::calcForce(bool calcVariances)
00048 {
00049 if(_p) _p->applyForce(_forceValue);
00050 else if(_r) _r->applyForce(_forceValue,
00051 _r->pointLocalToWorld(_localPosition));
00052
00053 }
00054
00055 void LinearMotor::setBody(Object* body)
00056 {
00057 if(body) {
00058 if(body->metaObject()->inherits<Particle>()) {
00059 _body = body;
00060 _p = static_cast<Particle*>(body);
00061 _r = NULL;
00062 return;
00063 } else if(body->metaObject()->inherits<RigidBody>()) {
00064 _body = body;
00065 _p = NULL;
00066 _r = static_cast<RigidBody*>(body);
00067 return;
00068 }
00069 }
00070 _body = NULL;
00071 _p = NULL;
00072 _r = NULL;
00073 }
00074
00075 Vector2d LinearMotor::position() const
00076 {
00077 if(_p) return _p->position() + _localPosition;
00078 else if(_r) return _r->pointLocalToWorld(_localPosition);
00079 return _localPosition;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00101 CircularMotor::CircularMotor(Object* body, const Vector2d& localPosition, double torqueValue)
00102 : _localPosition(localPosition), _torqueValue(torqueValue)
00103 {
00104 setBody(body);
00105 setColor(0xff0000ff);
00106 }
00107
00108 void CircularMotor::calcForce(bool calcVariances)
00109 {
00110 if(_r) _r->applyTorque(_torqueValue);
00111 }
00112
00113 void CircularMotor::setBody(Object* body)
00114 {
00115 if(body) {
00116 if(body->metaObject()->inherits<RigidBody>()) {
00117 _body = body;
00118 _r = static_cast<RigidBody*>(body);
00119 return;
00120 }
00121 }
00122 _body = NULL;
00123 _r = NULL;
00124 }
00125
00126 Vector2d CircularMotor::localPosition() const
00127 {
00128 if(_r) return Vector2d(0);
00129 else return _localPosition;
00130 }
00131
00132 Vector2d CircularMotor::position() const
00133 {
00134 if(_r) return _r->position();
00135 return _localPosition;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 }
00157