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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using System.Collections.Generic;
using System.Text;
using MonoXnaCompactMaths;
namespace XnaDevRu.BulletX.Dynamics
{
public class ConstraintSetting
{
private float _tau, _damping;
public ConstraintSetting()
{
_tau = 0.3f;
_damping = 1.0f;
}
public float Damping
{
get { return _damping; }
set { _damping = value; }
}
public float Tau
{
get { return _tau; }
set { _tau = value; }
}
}
public class Point2PointConstraint : TypedConstraint
{
private JacobianEntry[] _jacobian;
private Vector3 _pivotInA, _pivotInB;
private ConstraintSetting _setting = new ConstraintSetting();
public Point2PointConstraint()
{
_jacobian = new JacobianEntry[3];
}
public Point2PointConstraint(RigidBody rbA, RigidBody rbB, Vector3 pivotInA, Vector3 pivotInB)
: base(rbA, rbB)
{
_jacobian = new JacobianEntry[3];
_pivotInA = pivotInA;
_pivotInB = pivotInB;
}
public Point2PointConstraint(RigidBody rbA, Vector3 pivotInA)
: base(rbA)
{
_jacobian = new JacobianEntry[3];
_pivotInA = pivotInA;
_pivotInB = MathHelper.MatrixToVector(rbA.CenterOfMassTransform, _pivotInA);
}
public ConstraintSetting Settings { get { return _setting; } set { _setting = value; } }
public Vector3 PivotInA
{
set
{
_pivotInA = value;
}
}
public Vector3 PivotInB
{
set
{
_pivotInB = value;
}
}
public override void BuildJacobian()
{
Vector3 normal = new Vector3();
for (int i = 0; i < 3; i++)
{
MathHelper.SetElement(ref normal, i, 1);
_jacobian[i] = new JacobianEntry(
MatrixOperations.Transpose(RigidBodyA.CenterOfMassTransform),
MatrixOperations.Transpose(RigidBodyB.CenterOfMassTransform),
MathHelper.Transform(_pivotInA, RigidBodyA.CenterOfMassTransform) - RigidBodyA.CenterOfMassPosition,
MathHelper.Transform(_pivotInB, RigidBodyB.CenterOfMassTransform) - RigidBodyB.CenterOfMassPosition,
normal,
RigidBodyA.InvInertiaDiagLocal,
RigidBodyA.InverseMass,
RigidBodyB.InvInertiaDiagLocal,
RigidBodyB.InverseMass
);
MathHelper.SetElement(ref normal, i, 0);
}
}
public override void SolveConstraint(float timeStep)
{
Vector3 pivotAInW = MathHelper.Transform(_pivotInA, RigidBodyA.CenterOfMassTransform);
Vector3 pivotBInW = MathHelper.Transform(_pivotInB, RigidBodyB.CenterOfMassTransform);
Vector3 normal = new Vector3();
for (int i = 0; i < 3; i++)
{
MathHelper.SetElement(ref normal, i, 1);
float jacDiagABInv = 1.0f / _jacobian[i].Diagonal;
Vector3 rel_pos1 = pivotAInW - RigidBodyA.CenterOfMassPosition;
Vector3 rel_pos2 = pivotBInW - RigidBodyB.CenterOfMassPosition;
Vector3 vel1 = RigidBodyA.GetVelocityInLocalPoint(rel_pos1);
Vector3 vel2 = RigidBodyB.GetVelocityInLocalPoint(rel_pos2);
Vector3 vel = vel1 - vel2;
float rel_vel = Vector3.Dot(normal, vel);
float depth = -Vector3.Dot((pivotAInW - pivotBInW), normal);
float impulse = depth * _setting.Tau / timeStep * jacDiagABInv - _setting.Damping * rel_vel * jacDiagABInv;
AppliedImpulse += impulse;
Vector3 impulseVector = normal * impulse;
RigidBodyA.ApplyImpulse(impulseVector, pivotAInW - RigidBodyA.CenterOfMassPosition);
RigidBodyB.ApplyImpulse(-impulseVector, pivotBInW - RigidBodyB.CenterOfMassPosition);
MathHelper.SetElement(ref normal, i, 0);
}
}
}
}
|