File indexing completed on 2024-04-14 03:49:26

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 joint.h
0008  *  \brief Joint classes
0009  */
0010 
0011 #ifndef STEPCORE_JOINTS_H
0012 #define STEPCORE_JOINTS_H
0013 
0014 #include "joint.h"
0015 
0016 
0017 namespace StepCore
0018 {
0019 
0020 class Particle;
0021 class RigidBody;
0022 
0023 /** \ingroup joints
0024  *  \brief Fixes position of the body
0025  */
0026 class Anchor : public Joint
0027 {
0028     STEPCORE_OBJECT(Anchor)
0029         
0030 public:
0031     /** Constructs Anchor */
0032     explicit Anchor(Object* body = nullptr, const Vector2d& position = Vector2d::Zero(), double angle = 0);
0033 
0034     /** Get pointer to the body */
0035     Object* body() const { return _body; }
0036     /** Set pointer to the body */
0037     void setBody(Object* body);
0038 
0039     /** Get position of the anchor */
0040     const Vector2d& position() const { return _position; }
0041     /** Set position of the anchor */
0042     void setPosition(const Vector2d& position) { _position = position; }
0043 
0044     /** Get angle of the anchor */
0045     double angle() const { return _angle; }
0046     /** Set angle of the anchor */
0047     void setAngle(double angle) { _angle = angle; }
0048 
0049     int constraintsCount() override;
0050     void getConstraintsInfo(ConstraintsInfo* info, int offset) override;
0051 
0052     //void getConstraints(double* value, double* derivative);
0053     //void getJacobian(GmmSparseRowMatrix* value, GmmSparseRowMatrix* derivative, int offset);
0054 
0055 protected:
0056     Object*  _body;
0057     Vector2d _position;
0058     double   _angle;
0059 
0060     Particle*  _p;
0061     RigidBody* _r;
0062 };
0063 
0064 /** \ingroup joints
0065  *  \brief Fixes position of a given point on the body
0066  */
0067 class Pin : public Joint
0068 {
0069     STEPCORE_OBJECT(Pin)
0070 
0071 public:
0072     /** Constructs Pin */
0073     explicit Pin(Object* body = nullptr, const Vector2d& localPosition = Vector2d::Zero(),
0074                         const Vector2d& position = Vector2d::Zero());
0075 
0076     /** Get pointer to the body */
0077     Object* body() const { return _body; }
0078     /** Set pointer to the body */
0079     void setBody(Object* body);
0080 
0081     /** Local position of the pin on the body */
0082     const Vector2d& localPosition() const { return _localPosition; }
0083     /** Set local position of the pin on the body */
0084     void setLocalPosition(const Vector2d& localPosition) { _localPosition = localPosition; }
0085 
0086     /** Get global position of the pin */
0087     const Vector2d& position() const { return _position; }
0088     /** Set global position of the pin */
0089     void setPosition(const Vector2d& position) { _position = position; }
0090 
0091     int constraintsCount() override;
0092     void getConstraintsInfo(ConstraintsInfo* info, int offset) override;
0093 
0094     //void getConstraints(double* value, double* derivative);
0095     //void getJacobian(GmmSparseRowMatrix* value, GmmSparseRowMatrix* derivative, int offset);
0096 
0097 protected:
0098     Object*  _body;
0099     Vector2d _localPosition;
0100     Vector2d _position;
0101 
0102     Particle*  _p;
0103     RigidBody* _r;
0104 };
0105 
0106 /** \ingroup joints
0107  *  \brief Massless stick: fixed distance between two points on particles or rigid bodies
0108  */
0109 class Stick : public Joint
0110 {
0111     STEPCORE_OBJECT(Stick)
0112 
0113 public:
0114     /** Constructs Stick */
0115     explicit Stick(double restLength = 1, 
0116                Object* body1 = nullptr, Object* body2 = nullptr,
0117                const Vector2d& localPosition1 = Vector2d::Zero(),
0118                const Vector2d& localPosition2 = Vector2d::Zero());
0119 
0120     /** Get the restLength of the stick */
0121     double restLength() const { return _restLength; }
0122     /** Set the restLength of the stick */
0123     void   setRestLength(double restLength) { _restLength = restLength; }
0124 
0125     /** Get pointer to the first connected body */
0126     Object* body1() const { return _body1; }
0127     /** Set pointer to the first connected body */
0128     void setBody1(Object* body1);
0129 
0130     /** Get pointer to the second connected body */
0131     Object* body2() const { return _body2; }
0132     /** Set pointer to the second connected body */
0133     void setBody2(Object* body2);
0134 
0135     /** Local position of the first end of the stick on the body
0136      *  or in the world (if the end is not connected) */
0137     Vector2d localPosition1() const { return _localPosition1; }
0138     /** Set local position of the first end of the stick on the body
0139      *  or in the world (if the end is not connected) */
0140     void setLocalPosition1(const Vector2d& localPosition1) { _localPosition1 = localPosition1; }
0141 
0142     /** Local position of the second end of the stick on the body
0143      *  or in the world (if the end is not connected) */
0144     Vector2d localPosition2() const { return _localPosition2; }
0145     /** Set local position of the second end of the stick on the body
0146      *  or in the world (if the end is not connected) */
0147     void setLocalPosition2(const Vector2d& localPosition2) { _localPosition2 = localPosition2; }
0148 
0149     /** Position of the first end of the stick */
0150     Vector2d position1() const;
0151     /** Position of the second end of the stick */
0152     Vector2d position2() const;
0153 
0154     /** Velocity of the first end of the stick */
0155     Vector2d velocity1() const;
0156     /** Velocity of the second end of the stick */
0157     Vector2d velocity2() const;
0158 
0159     /** Get first connected Particle */
0160     Particle* particle1() const { return _p1; }
0161     /** Get second connected Particle */
0162     Particle* particle2() const { return _p2; }
0163     /** Get first connected RigidBody */
0164     RigidBody* rigidBody1() const { return _r1; }
0165     /** Get second connected RigidBody */
0166     RigidBody* rigidBody2() const { return _r2; }
0167 
0168     int constraintsCount() override;
0169     void getConstraintsInfo(ConstraintsInfo* info, int offset) override;
0170 
0171 protected:
0172     double   _restLength;
0173     Object*  _body1;
0174     Object*  _body2;
0175     Vector2d _localPosition1;
0176     Vector2d _localPosition2;
0177 
0178     Particle*  _p1;
0179     Particle*  _p2;
0180     RigidBody* _r1;
0181     RigidBody* _r2;
0182 };
0183 
0184 /** \ingroup joints
0185  *  \brief Massless rope: maximal distance between two points on particles or rigid bodies
0186  */
0187 class Rope: public Stick
0188 {
0189     STEPCORE_OBJECT(Rope)
0190 
0191 public:
0192     void getConstraintsInfo(ConstraintsInfo* info, int offset) override;
0193 };
0194 
0195 } // namespace StepCore
0196 
0197 #endif