forked from bepu/bepuphysics2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearAxisLimit.cs
157 lines (143 loc) · 8.62 KB
/
LinearAxisLimit.cs
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
using BepuPhysics.CollisionDetection;
using BepuUtilities;
using BepuUtilities.Memory;
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using static BepuUtilities.GatherScatter;
namespace BepuPhysics.Constraints
{
/// <summary>
/// Constrains points on two bodies to a range of offsets from each other along a direction anchored to body A.
/// </summary>
public struct LinearAxisLimit : ITwoBodyConstraintDescription<LinearAxisLimit>
{
/// <summary>
/// Local offset from the center of body A to its attachment point.
/// </summary>
public Vector3 LocalOffsetA;
/// <summary>
/// Local offset from the center of body B to its attachment point.
/// </summary>
public Vector3 LocalOffsetB;
/// <summary>
/// Direction of the motorized axis in the local space of body A.
/// </summary>
public Vector3 LocalAxis;
/// <summary>
/// Minimum offset along the world axis between A and B's anchor points.
/// </summary>
public float MinimumOffset;
/// <summary>
/// Maximum offset along the world axis between A and B's anchor points.
/// </summary>
public float MaximumOffset;
/// <summary>
/// Spring frequency and damping parameters.
/// </summary>
public SpringSettings SpringSettings;
public int ConstraintTypeId
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return LinearAxisLimitTypeProcessor.BatchTypeId;
}
}
public Type TypeProcessorType => typeof(LinearAxisLimitTypeProcessor);
public void ApplyDescription(ref TypeBatch batch, int bundleIndex, int innerIndex)
{
Debug.Assert(MaximumOffset >= MinimumOffset, "LinearAxisLimit.MaximumOffset must be greater than or equal to LinearAxisLimit.MinimumOffset.");
ConstraintChecker.AssertUnitLength(LocalAxis, nameof(LinearAxisLimit), nameof(LocalAxis));
ConstraintChecker.AssertValid(SpringSettings, nameof(LinearAxisLimit));
Debug.Assert(ConstraintTypeId == batch.TypeId, "The type batch passed to the description must match the description's expected type.");
ref var target = ref GetOffsetInstance(ref Buffer<LinearAxisLimitPrestepData>.Get(ref batch.PrestepData, bundleIndex), innerIndex);
Vector3Wide.WriteFirst(LocalOffsetA, ref target.LocalOffsetA);
Vector3Wide.WriteFirst(LocalOffsetB, ref target.LocalOffsetB);
Vector3Wide.WriteFirst(LocalAxis, ref target.LocalPlaneNormal);
GatherScatter.GetFirst(ref target.MinimumOffset) = MinimumOffset;
GatherScatter.GetFirst(ref target.MaximumOffset) = MaximumOffset;
SpringSettingsWide.WriteFirst(SpringSettings, ref target.SpringSettings);
}
public void BuildDescription(ref TypeBatch batch, int bundleIndex, int innerIndex, out LinearAxisLimit description)
{
Debug.Assert(ConstraintTypeId == batch.TypeId, "The type batch passed to the description must match the description's expected type.");
ref var source = ref GetOffsetInstance(ref Buffer<LinearAxisLimitPrestepData>.Get(ref batch.PrestepData, bundleIndex), innerIndex);
Vector3Wide.ReadFirst(source.LocalOffsetA, out description.LocalOffsetA);
Vector3Wide.ReadFirst(source.LocalOffsetB, out description.LocalOffsetB);
Vector3Wide.ReadFirst(source.LocalPlaneNormal, out description.LocalAxis);
description.MinimumOffset = GatherScatter.GetFirst(ref source.MinimumOffset);
description.MaximumOffset = GatherScatter.GetFirst(ref source.MaximumOffset);
SpringSettingsWide.ReadFirst(source.SpringSettings, out description.SpringSettings);
}
}
public struct LinearAxisLimitPrestepData
{
public Vector3Wide LocalOffsetA;
public Vector3Wide LocalOffsetB;
public Vector3Wide LocalPlaneNormal;
public Vector<float> MinimumOffset;
public Vector<float> MaximumOffset;
public SpringSettingsWide SpringSettings;
}
public struct LinearAxisLimitFunctions : IConstraintFunctions<LinearAxisLimitPrestepData, LinearAxisServoProjection, Vector<float>>
{
public struct LimitJacobianModifier : LinearAxisServoFunctions.IJacobianModifier
{
public Vector<float> MinimumOffset;
public Vector<float> MaximumOffset;
public Vector<float> Error;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Modify(in Vector3Wide anchorA, in Vector3Wide anchorB, ref Vector3Wide normal)
{
Vector3Wide.Subtract(anchorB, anchorA, out var anchorOffset);
Vector3Wide.Dot(anchorOffset, normal, out var planeNormalDot);
var minimumError = MinimumOffset - planeNormalDot;
var maximumError = planeNormalDot - MaximumOffset;
var useMin = Vector.LessThan(Vector.Abs(minimumError), Vector.Abs(maximumError));
Error = Vector.ConditionalSelect(useMin, minimumError, maximumError);
normal.X = Vector.ConditionalSelect(useMin, -normal.X, normal.X);
normal.Y = Vector.ConditionalSelect(useMin, -normal.Y, normal.Y);
normal.Z = Vector.ConditionalSelect(useMin, -normal.Z, normal.Z);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Prestep(Bodies bodies, ref TwoBodyReferences bodyReferences, int count, float dt, float inverseDt, ref BodyInertias inertiaA, ref BodyInertias inertiaB,
ref LinearAxisLimitPrestepData prestep, out LinearAxisServoProjection projection)
{
SpringSettingsWide.ComputeSpringiness(prestep.SpringSettings, dt, out var positionErrorToVelocity, out var effectiveMassCFMScale, out projection.SoftnessImpulseScale);
LimitJacobianModifier modifier;
modifier.MinimumOffset = prestep.MinimumOffset;
modifier.MaximumOffset = prestep.MaximumOffset;
modifier.Error = default;
LinearAxisServoFunctions.ComputeTransforms(ref modifier, bodies, ref bodyReferences, count, prestep.LocalOffsetA, prestep.LocalOffsetB, prestep.LocalPlaneNormal, inertiaA, inertiaB, effectiveMassCFMScale,
out var anchorA, out var anchorB, out var normal, out var effectiveMass,
out projection.LinearVelocityToImpulseA, out projection.AngularVelocityToImpulseA, out projection.AngularVelocityToImpulseB,
out projection.LinearImpulseToVelocityA, out projection.AngularImpulseToVelocityA, out projection.NegatedLinearImpulseToVelocityB, out projection.AngularImpulseToVelocityB);
InequalityHelpers.ComputeBiasVelocity(modifier.Error, positionErrorToVelocity, inverseDt, out var biasVelocity);
projection.BiasImpulse = biasVelocity * effectiveMass;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WarmStart(ref BodyVelocities velocityA, ref BodyVelocities velocityB, ref LinearAxisServoProjection projection, ref Vector<float> accumulatedImpulse)
{
LinearAxisServoFunctions.ApplyImpulse(ref velocityA, ref velocityB,
projection.LinearImpulseToVelocityA, projection.AngularImpulseToVelocityA, projection.NegatedLinearImpulseToVelocityB, projection.AngularImpulseToVelocityB,
ref accumulatedImpulse);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Solve(ref BodyVelocities velocityA, ref BodyVelocities velocityB, ref LinearAxisServoProjection projection, ref Vector<float> accumulatedImpulse)
{
LinearAxisServoFunctions.ComputeCorrectiveImpulse(ref velocityA, ref velocityB, projection.LinearVelocityToImpulseA, projection.AngularVelocityToImpulseA, projection.AngularVelocityToImpulseB,
projection.BiasImpulse, projection.SoftnessImpulseScale, accumulatedImpulse, out var csi);
InequalityHelpers.ClampPositive(ref accumulatedImpulse, ref csi);
LinearAxisServoFunctions.ApplyImpulse(ref velocityA, ref velocityB,
projection.LinearImpulseToVelocityA, projection.AngularImpulseToVelocityA, projection.NegatedLinearImpulseToVelocityB, projection.AngularImpulseToVelocityB,
ref csi);
}
}
public class LinearAxisLimitTypeProcessor : TwoBodyTypeProcessor<LinearAxisLimitPrestepData, LinearAxisServoProjection, Vector<float>, LinearAxisLimitFunctions>
{
public const int BatchTypeId = 40;
}
}