Skip to content

Commit 05a85e0

Browse files
committed
Added gizmos for the constraint pivot
Added text describing how to use each constraint Renamed the constraint axis
1 parent 0a1bf3b commit 05a85e0

17 files changed

Lines changed: 138 additions & 53 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.

UnityProject/Assets/BulletUnity/Scripts/Constraints/B6DOFConstraint.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ public class B6DOFConstraint : BTypedConstraint {
1010
//Todo not sure if this is working
1111
//Todo breaking strength
1212
//todo should be properties so can capture changes and propagate to scene
13+
public static string HelpMessage = "\n" +
14+
"\nTIP: To see constraint limits:\n" +
15+
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
16+
" - On Constraint set 'Debug Draw Size'\n" +
17+
" - Press play";
1318

14-
[Header("Reference Frame Local To This Object")]
15-
public Vector3 m_localConstraintPoint = Vector3.zero;
16-
public Vector3 m_localConstraintForwardDir = Vector3.forward;
17-
public Vector3 m_localConstraintUpDir = Vector3.up;
1819

1920
[Header("Limits")]
2021
public Vector3 m_linearLimitLower;
@@ -72,7 +73,7 @@ internal override bool _BuildConstraint() {
7273

7374
BM.Matrix frameInA, frameInOther;
7475
string errormsg = "";
75-
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
76+
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
7677
{
7778
m_constraintPtr = new Generic6DofConstraint(rbb, rba, frameInOther, frameInA, true);
7879
} else

UnityProject/Assets/BulletUnity/Scripts/Constraints/BBallSocketConstraint.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ public class BBallSocketConstraint : BTypedConstraint {
1010

1111

1212
//todo should be properties so can capture changes and propagate to scene
13-
14-
[Header("Constraint Point Local To This Object")]
15-
public Vector3 m_localConstraintPoint = Vector3.zero;
13+
public static string HelpMessage = "Only the 'Local Constraint Point' is used. X and Y are ignored.\n" +
14+
"\nTIP: To see constraint limits:\n" +
15+
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
16+
" - On Constraint set 'Debug Draw Size'\n" +
17+
" - Press play";
1618

1719
//called by Physics World just before constraint is added to world.
1820
//the current constraint properties are used to rebuild the constraint.

UnityProject/Assets/BulletUnity/Scripts/Constraints/BConeTwistConstraint.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using BM = BulletSharp.Math;
66

77
namespace BulletUnity {
8-
//TODO script execution order, rigid bodies before constraints & other stuff that might use them
98
[System.Serializable]
109
public class BConeTwistConstraint : BTypedConstraint {
1110
public static string HelpMessage = "btConeTwistConstraint can be used to simulate ragdoll joints (upper arm, leg etc)." +
@@ -14,14 +13,14 @@ public class BConeTwistConstraint : BTypedConstraint {
1413
"Swing is divided into swing1 and swing2 which can have different limits, giving an elliptical shape."+
1514
"(Note: the cone's base isn't flat, so this ellipse is 'embedded' on the surface of a sphere.)\n\n"+
1615
"In the contraint's frame of reference:\n"+
17-
" - twist is along the forward\n"+
18-
" - and swing 1 and 2 are along the right and up axes respectively.\n";
16+
" - twist is along the X (blue) twist limit is about Y (green) \n"+
17+
" - and swing 1 and 2 are along the Y (green) and Z (red) axes respectively.\n" +
18+
"\nTIP: To see constraint limits:\n" +
19+
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
20+
" - On Constraint set 'Debug Draw Size'\n" +
21+
" - Press play";
1922

2023

21-
[Header("Reference Frame Local To This Object")]
22-
public Vector3 m_localConstraintPoint = Vector3.zero;
23-
public Vector3 m_localConstraintForwardDir = Vector3.forward;
24-
public Vector3 m_localConstraintUpDir = Vector3.up;
2524

2625
[Header("Limits")]
2726
public float m_swingSpan1Radians = Mathf.PI;
@@ -66,7 +65,7 @@ internal override bool _BuildConstraint() {
6665
}
6766
BM.Matrix frameInA, frameInOther;
6867
string errormsg = "";
69-
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
68+
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
7069
{
7170
m_constraintPtr = new ConeTwistConstraint((RigidBody)m_otherRigidBody.GetCollisionObject(), (RigidBody)targetRigidBodyA.GetCollisionObject(), frameInOther, frameInA);
7271
} else
@@ -81,7 +80,7 @@ internal override bool _BuildConstraint() {
8180
//TODO this is broken
8281
string errormsg = "";
8382
BM.Matrix frameInB = BM.Matrix.Identity;
84-
if (CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInB, ref errormsg))
83+
if (CreateFrame(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, ref frameInB, ref errormsg))
8584
{
8685
m_constraintPtr = new ConeTwistConstraint((RigidBody)targetRigidBodyA.GetCollisionObject(), frameInB);
8786
} else

UnityProject/Assets/BulletUnity/Scripts/Constraints/BFixedConstraint.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ namespace BulletUnity {
99
public class BFixedConstraint : BTypedConstraint {
1010

1111
//todo should be properties so can capture changes and propagate to scene
12-
[Header("Reference Frame Local To This Object")]
13-
public Vector3 m_localConstraintPoint = Vector3.zero;
14-
public Vector3 m_localConstraintForwardDir = Vector3.forward;
15-
public Vector3 m_localConstraintUpDir = Vector3.up;
1612

1713
//called by Physics World just before constraint is added to world.
1814
//the current constraint properties are used to rebuild the constraint.
@@ -62,7 +58,7 @@ internal override bool _BuildConstraint() {
6258
}
6359
BM.Matrix frameInA, frameInOther;
6460
string errormsg = "";
65-
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
61+
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
6662
{
6763
m_constraintPtr = new FixedConstraint(rbb, rba, frameInOther, frameInA);
6864
} else

UnityProject/Assets/BulletUnity/Scripts/Constraints/BHingedConstraint.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ namespace BulletUnity {
99
public class BHingedConstraint : BTypedConstraint {
1010

1111
//todo should be properties so can capture changes and propagate to scene
12-
/// <summary>
13-
/// In targetRigidbody local coordinates
14-
/// </summary>
15-
[Header("Reference Frame Local To This Object")]
16-
public Vector3 m_localConstraintPoint = Vector3.zero;
17-
public Vector3 m_localConstraintForwardDir = Vector3.forward;
18-
public Vector3 m_localConstraintUpDir = Vector3.up;
12+
public static string HelpMessage = "X (red) is axis of the hinge.\n" +
13+
"\nTIP: To see constraint limits:\n" +
14+
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
15+
" - On Constraint set 'Debug Draw Size'\n" +
16+
" - Press play";
1917

2018
public bool m_enableMotor;
2119
public float m_targetMotorAngularVelocity = 0f;
@@ -54,7 +52,7 @@ internal override bool _BuildConstraint() {
5452
{
5553
world.AddRigidBody(targetRigidBodyA);
5654
}
57-
if (m_localConstraintForwardDir == Vector3.zero)
55+
if (m_localConstraintAxisX == Vector3.zero)
5856
{
5957
Debug.LogError("Constaint axis cannot be zero vector");
6058
return false;
@@ -83,7 +81,7 @@ internal override bool _BuildConstraint() {
8381
}
8482
BM.Matrix frameInA, frameInOther;
8583
string errormsg = "";
86-
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
84+
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
8785
{
8886
//warning the frameInA, frameInB version of the constructor is broken
8987
m_constraintPtr = new HingeConstraint(rbb, rba, frameInOther.Origin, frameInA.Origin, (BM.Vector3)frameInOther.Basis.Column1, (BM.Vector3)frameInA.Basis.Column1);
@@ -96,7 +94,7 @@ internal override bool _BuildConstraint() {
9694
else {
9795
//BM.Matrix frameInA = BM.Matrix.Identity;
9896
//CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInA);
99-
m_constraintPtr = new HingeConstraint(rba, m_localConstraintPoint.ToBullet(),m_localConstraintForwardDir.ToBullet(), false);
97+
m_constraintPtr = new HingeConstraint(rba, m_localConstraintPoint.ToBullet(),m_localConstraintAxisX.ToBullet(), false);
10098
}
10199
if (m_enableMotor)
102100
{

UnityProject/Assets/BulletUnity/Scripts/Constraints/BSliderConstraint.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@
77
namespace BulletUnity {
88
[System.Serializable]
99
public class BSliderConstraint : BTypedConstraint {
10-
11-
[Header("Reference Frame Local To This Object")]
12-
public Vector3 m_localConstraintPoint = Vector3.zero;
13-
public Vector3 m_localConstraintForwardDir = Vector3.forward;
14-
public Vector3 m_localConstraintUpDir = Vector3.up;
10+
public static string HelpMessage = "X (red) is slide axis. Angular limits are measured from Y (green) toward Z. \n" +
11+
"\nTIP: To see constraint limits:\n" +
12+
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
13+
" - On Constraint set 'Debug Draw Size'\n" +
14+
" - Press play";
1515

1616
[Header("Limits")]
1717
public float m_lowerLinearLimit = -10f;
1818
public float m_upperLinearLimit = 10f;
1919
public float m_lowerAngularLimitRadians = -Mathf.PI;
2020
public float m_upperAngularLimitRadians = Mathf.PI;
2121

22-
public void OnDrawGizmosSelected()
23-
{
24-
DrawTransformGizmos(transform, m_localConstraintPoint, m_localConstraintForwardDir, m_localConstraintUpDir);
25-
}
26-
2722
//called by Physics World just before constraint is added to world.
2823
//the current constraint properties are used to rebuild the constraint.
2924
internal override bool _BuildConstraint() {
@@ -69,7 +64,7 @@ internal override bool _BuildConstraint() {
6964

7065
BM.Matrix frameInA, frameInOther;
7166
string errormsg = "";
72-
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
67+
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
7368
{
7469
m_constraintPtr = new SliderConstraint(rbb, rba, frameInOther, frameInA, true);
7570
} else
@@ -81,7 +76,7 @@ internal override bool _BuildConstraint() {
8176
{
8277
BulletSharp.Math.Matrix frameInA = BM.Matrix.Identity;
8378
string errormsg = "";
84-
if (CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInA, ref errormsg))
79+
if (CreateFrame(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, ref frameInA, ref errormsg))
8580
{
8681
m_constraintPtr = new SliderConstraint(rba, frameInA, true);
8782
} else

UnityProject/Assets/BulletUnity/Scripts/Constraints/BTypedConstraint.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public enum ConstraintType
1616
constrainToAnotherBody
1717
}
1818

19+
[Header("Reference Frame Local To This Object")]
20+
public Vector3 m_localConstraintPoint = Vector3.zero;
21+
public Vector3 m_localConstraintAxisX = Vector3.forward;
22+
public Vector3 m_localConstraintAxisY = Vector3.up;
23+
24+
25+
1926
public float m_breakingImpulseThreshold = Mathf.Infinity;
2027
public bool m_disableCollisionsBetweenConstrainedBodies = true;
2128
public ConstraintType m_constraintType;
@@ -30,18 +37,18 @@ public enum ConstraintType
3037
public void DrawTransformGizmos(Transform t, Vector3 pivotPoint, Vector3 forward, Vector3 up)
3138
{
3239
Vector3 pivotWorld = t.TransformPoint(pivotPoint);
33-
Vector3 forwardWorld = t.TransformDirection(forward).normalized;
34-
Vector3 upWorld = t.TransformDirection(up).normalized;
35-
Vector3 rightWorld = Vector3.Cross(forwardWorld, upWorld);
36-
upWorld = Vector3.Cross(rightWorld, forwardWorld);
37-
upWorld.Normalize();
38-
forwardWorld.Normalize();
40+
Vector3 xWorld = t.TransformDirection(forward).normalized;
41+
Vector3 yWorld = t.TransformDirection(up).normalized;
42+
Vector3 zWorld = Vector3.Cross(xWorld, yWorld);
43+
yWorld = Vector3.Cross(zWorld, xWorld);
44+
yWorld.Normalize();
45+
xWorld.Normalize();
3946
Gizmos.color = Color.red;
40-
Gizmos.DrawLine(pivotWorld, pivotWorld + rightWorld);
47+
Gizmos.DrawLine(pivotWorld, pivotWorld + xWorld);
4148
Gizmos.color = Color.green;
42-
Gizmos.DrawLine(pivotWorld, pivotWorld + upWorld);
49+
Gizmos.DrawLine(pivotWorld, pivotWorld + yWorld);
4350
Gizmos.color = Color.blue;
44-
Gizmos.DrawLine(pivotWorld, pivotWorld + forwardWorld);
51+
Gizmos.DrawLine(pivotWorld, pivotWorld + zWorld);
4552
}
4653

4754
public bool CreateFrame(UnityEngine.Vector3 forward, UnityEngine.Vector3 up, UnityEngine.Vector3 constraintPoint, ref BulletSharp.Math.Matrix m, ref string errorMsg)
@@ -109,6 +116,13 @@ public bool CreateFramesA_B(UnityEngine.Vector3 forwardInA, UnityEngine.Vector3
109116
return true;
110117
}
111118

119+
public virtual void OnDrawGizmosSelected()
120+
{
121+
DrawTransformGizmos(transform, m_localConstraintPoint, m_localConstraintAxisX, m_localConstraintAxisY);
122+
}
123+
124+
125+
112126
protected virtual void AddToBulletWorld()
113127
{
114128
if (!m_isInWorld)

0 commit comments

Comments
 (0)