• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

step/stepcore

motor.cc

Go to the documentation of this file.
00001 /* This file is part of StepCore library.
00002    Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
00003 
00004    StepCore library is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2 of the License, or
00007    (at your option) any later version.
00008 
00009    StepCore library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with StepCore; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 void LinearMotor::worldItemRemoved(Item* item)
00084 {
00085     if(item == NULL) return;
00086     if(item == _body) setBody(NULL);
00087 }
00088 
00089 void LinearMotor::setWorld(World* world)
00090 {
00091     if(world == NULL) {
00092         setBody(NULL);        
00093     } else if(this->world() != NULL) { 
00094         if(_body != NULL) setBody(world->item(body()->name()));
00095     }
00096     Item::setWorld(world);
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 void  CircularMotor::worldItemRemoved(Item* item)
00140 {
00141     if(item == NULL) return;
00142     if(item == _body) setBody(NULL);
00143 }
00144 
00145 void CircularMotor::setWorld(World* world)
00146 {
00147     if(world == NULL) {
00148         setBody(NULL);        
00149     } else if(this->world() != NULL) { 
00150         if(_body != NULL) setBody(world->item(body()->name()));
00151     }
00152     Item::setWorld(world);
00153 }
00154 */
00155 
00156 } // namespace StepCore
00157 

step/stepcore

Skip menu "step/stepcore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal