File indexing completed on 2024-05-19 07:40:20

0001 /*
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 /** \file motor.h
0008  *  \brief LinearMotor class
0009  */
0010 
0011 #ifndef STEPCORE_MOTOR_H
0012 #define STEPCORE_MOTOR_H
0013 
0014 #include "world.h"
0015 #include "object.h"
0016 #include "constants.h"
0017 #include "types.h"
0018 
0019 
0020 namespace StepCore
0021 {
0022 
0023 class Particle;
0024 class RigidBody;
0025 
0026 /** \ingroup forces
0027  *  \brief Linear motor: applies a force at given position on the body
0028  */
0029 class LinearMotor : public Force
0030 {
0031     STEPCORE_OBJECT(LinearMotor)
0032 
0033 public:
0034     /** Constructs LinearMotor */
0035     explicit LinearMotor(Object* body = nullptr, const Vector2d& localPosition = Vector2d::Zero(),
0036                         const Vector2d &forceValue = Vector2d::Zero());
0037 
0038     void calcForce(bool calcVariances) override;
0039 
0040     /** Get pointer to the body */
0041     Object* body() const { return _body; }
0042     /** Set pointer to the connected body */
0043     void setBody(Object* body);
0044 
0045     /** Local position of the motor on the body
0046      *  or in the world (if the motor is not connected) */
0047     const Vector2d& localPosition() const { return _localPosition; }
0048     /** Set local position of the motor on the body
0049      *  or in the world (if the motor is not connected) */
0050     void setLocalPosition(const Vector2d& localPosition) { _localPosition = localPosition; }
0051 
0052     /** Position of the motor */
0053     Vector2d position() const;
0054 
0055     /** Whether the motor is rigidly fixed to the body */
0056     bool isRigidlyFixed() const { return _rigidlyFixed; }
0057     /** Sets whether the motor is rigidly fixed to the body, i.e. rotates the
0058      *  force vector in sync with body rotation */
0059     void setRigidlyFixed(bool fixed) { _rigidlyFixed = fixed; }
0060 
0061     /** Get force value */
0062     const Vector2d& forceValue() const { return _forceValue; }
0063     /** Set force value */
0064     void setForceValue(const Vector2d& forceValue) { _forceValue = forceValue; }
0065 
0066     //void worldItemRemoved(Item* item);
0067     //void setWorld(World* world);
0068         
0069 protected:
0070     Object*  _body;
0071     Vector2d _localPosition;
0072     Vector2d _forceValue;
0073     double   _lastBodyAngle = 0;
0074     bool     _rigidlyFixed = false;
0075 
0076     Particle*  _p;
0077     RigidBody* _r;
0078 };
0079 
0080 /** \ingroup forces
0081  *  \brief Circular motor: applies a torque to the body
0082  */
0083 class CircularMotor : public Force
0084 {
0085     STEPCORE_OBJECT(CircularMotor)
0086 
0087 public:
0088     /** Constructs CircularMotor */
0089     explicit CircularMotor(Object* body = nullptr, const Vector2d& localPosition = Vector2d::Zero(),
0090                                             double torqueValue = 0);
0091 
0092     void calcForce(bool calcVariances) override;
0093 
0094     /** Get pointer to the body */
0095     Object* body() const { return _body; }
0096     /** Set pointer to the connected body */
0097     void setBody(Object* body);
0098 
0099     /** Local position of the motor on the body
0100      *  or in the world (if the motor is not connected) */
0101     Vector2d localPosition() const;
0102     /** Set local position of the motor on the body
0103      *  or in the world (if the motor is not connected) */
0104     void setLocalPosition(const Vector2d& localPosition) {
0105         _localPosition = localPosition;
0106     }
0107 
0108     /** Position of the motor */
0109     Vector2d position() const;
0110 
0111     /** Get torque value */
0112     double torqueValue() const { return _torqueValue; }
0113     /** Set torque value */
0114     void setTorqueValue(const double torqueValue) { _torqueValue = torqueValue; }
0115 
0116     //void worldItemRemoved(Item* item);
0117     //void setWorld(World* world);
0118         
0119 protected:
0120     Object*  _body;
0121     Vector2d _localPosition;
0122     double   _torqueValue;
0123 
0124     RigidBody* _r;
0125 };
0126 
0127 
0128 } // namespace StepCore
0129 
0130 #endif
0131