-
Notifications
You must be signed in to change notification settings - Fork 0
/
joint.h
137 lines (126 loc) · 3.01 KB
/
joint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef JOINT_H
#define JOINT_H
#include "body.h"
class IKJoint;
class Joint{
public:
Joint(Body* a, Body* b) : A(a), B(b) {}
virtual void Initialize(double dt) = 0;
virtual void ApplyImpulse(double dt) = 0;
virtual void PositionalCorrection() = 0;
protected:
Body* A;
Body* B;
};
class RopeJoint : public Joint{
public:
RopeJoint(Body* a, Body* b, Vector2D c, Vector2D d, double e) : Joint(a, b), maxDist(e)
{
AnchorA = c - a->mass.CoM;
AnchorB = d - b->mass.CoM;
}
void Initialize(double dt);
void ApplyImpulse(double dt);
void PositionalCorrection();
private:
Vector2D AnchorA;
Vector2D AnchorB;
Vector2D u;
double maxDist;
double mass;
double impulse;
double dist;
};
class RevJoint : public Joint{
friend class IKJoint;
public:
RevJoint(Body* a, Body* b, Vector2D c, Vector2D d) : Joint(a, b) {
AnchorA = c - a->mass.CoM;
AnchorB = d - b->mass.CoM;
refAngle = B->orient - A->orient;
motorEnabled = false;
motorImpulse = 0;
limitEnabled = false;
cumImpulse.Set(0,0,0);
}
void Initialize(double dt);
void ApplyImpulse(double dt);
void PositionalCorrection();
void SetMotor(bool on, double speed, double tlimit);
void ApplyTorque(double torque, double dt);
void SetLimit(bool on, double upper, double lower);
double GetAngle();
double GetAffineAngle();
double GetAngVel();
Vector2D GetAnchor();
private:
Vector2D AnchorA;
Vector2D AnchorB;
Matrix3D mass;
bool motorEnabled;
double motorSpeed;
double motorMaxTorque;
double motorImpulse;
bool limitEnabled;
Angle lowerLimit;
Angle upperLimit;
Angle refAngle;
Vector3D cumImpulse;
enum State{
_atUpper,
_atLower,
_between
};
State state;
};
class WheelJoint : public Joint{
public:
WheelJoint(Body* a, Body* b, Vector2D c, Vector2D d, Vector2D e, double f, double g) :
Joint(a, b), AxisFrdm(e), springFreq(f), springDamp(g)
{
AnchorA = c - a->mass.CoM;
AnchorB = d - b->mass.CoM;
bool motorEnabled = false;
motorImpulse = 0;
springImpulse = 0;
cumImpulse = 0;
}
void Initialize(double dt);
void ApplyImpulse(double dt);
void PositionalCorrection();
void SetMotor(bool on, double speed, double tlimit);
void SetMotorSpeed(double speed) {motorSpeed = speed;}
private:
Vector2D AnchorA;
Vector2D AnchorB;
Vector2D AxisFrdm;
Vector2D ax, ay;
double sAx, sAy;
double sBx, sBy;
bool motorEnabled;
double motorSpeed;
double motorMaxTorque;
double motorImpulse, springImpulse, cumImpulse;
double springFreq;
double springDamp;
double mass;
double smass, bias, gamma;
};
class IKJoint{
public:
static Vector2D TwoLinkIKSolve(RevJoint* Parent, RevJoint* Child, Vector2D endpoint,
Vector2D target, double& p, double& c);
IKJoint(RevJoint* j, Vector2D endPoint);
void AddJoint(RevJoint* j);
void SetTarget(Vector2D t);
void SetTarget(bool on);
void Solve();
private:
void solve(int i);
bool targeted;
Vector2D effector;
Vector2D effRel;
Vector2D target;
std::vector<RevJoint*> joints;
};
#endif