Skip to content

Commit

Permalink
Added gizmos for the constraint pivot
Browse files Browse the repository at this point in the history
Added text describing how to use each constraint
Renamed the constraint axis
  • Loading branch information
Phong13 committed May 5, 2016
1 parent 0a1bf3b commit 05a85e0
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 53 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public class B6DOFConstraint : BTypedConstraint {
//Todo not sure if this is working
//Todo breaking strength
//todo should be properties so can capture changes and propagate to scene
public static string HelpMessage = "\n" +
"\nTIP: To see constraint limits:\n" +
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
" - On Constraint set 'Debug Draw Size'\n" +
" - Press play";

[Header("Reference Frame Local To This Object")]
public Vector3 m_localConstraintPoint = Vector3.zero;
public Vector3 m_localConstraintForwardDir = Vector3.forward;
public Vector3 m_localConstraintUpDir = Vector3.up;

[Header("Limits")]
public Vector3 m_linearLimitLower;
Expand Down Expand Up @@ -72,7 +73,7 @@ internal override bool _BuildConstraint() {

BM.Matrix frameInA, frameInOther;
string errormsg = "";
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
{
m_constraintPtr = new Generic6DofConstraint(rbb, rba, frameInOther, frameInA, true);
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public class BBallSocketConstraint : BTypedConstraint {


//todo should be properties so can capture changes and propagate to scene

[Header("Constraint Point Local To This Object")]
public Vector3 m_localConstraintPoint = Vector3.zero;
public static string HelpMessage = "Only the 'Local Constraint Point' is used. X and Y are ignored.\n" +
"\nTIP: To see constraint limits:\n" +
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
" - On Constraint set 'Debug Draw Size'\n" +
" - Press play";

//called by Physics World just before constraint is added to world.
//the current constraint properties are used to rebuild the constraint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using BM = BulletSharp.Math;

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


[Header("Reference Frame Local To This Object")]
public Vector3 m_localConstraintPoint = Vector3.zero;
public Vector3 m_localConstraintForwardDir = Vector3.forward;
public Vector3 m_localConstraintUpDir = Vector3.up;

[Header("Limits")]
public float m_swingSpan1Radians = Mathf.PI;
Expand Down Expand Up @@ -66,7 +65,7 @@ internal override bool _BuildConstraint() {
}
BM.Matrix frameInA, frameInOther;
string errormsg = "";
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
{
m_constraintPtr = new ConeTwistConstraint((RigidBody)m_otherRigidBody.GetCollisionObject(), (RigidBody)targetRigidBodyA.GetCollisionObject(), frameInOther, frameInA);
} else
Expand All @@ -81,7 +80,7 @@ internal override bool _BuildConstraint() {
//TODO this is broken
string errormsg = "";
BM.Matrix frameInB = BM.Matrix.Identity;
if (CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInB, ref errormsg))
if (CreateFrame(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, ref frameInB, ref errormsg))
{
m_constraintPtr = new ConeTwistConstraint((RigidBody)targetRigidBodyA.GetCollisionObject(), frameInB);
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ namespace BulletUnity {
public class BFixedConstraint : BTypedConstraint {

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

//called by Physics World just before constraint is added to world.
//the current constraint properties are used to rebuild the constraint.
Expand Down Expand Up @@ -62,7 +58,7 @@ internal override bool _BuildConstraint() {
}
BM.Matrix frameInA, frameInOther;
string errormsg = "";
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
{
m_constraintPtr = new FixedConstraint(rbb, rba, frameInOther, frameInA);
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ namespace BulletUnity {
public class BHingedConstraint : BTypedConstraint {

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

public bool m_enableMotor;
public float m_targetMotorAngularVelocity = 0f;
Expand Down Expand Up @@ -54,7 +52,7 @@ internal override bool _BuildConstraint() {
{
world.AddRigidBody(targetRigidBodyA);
}
if (m_localConstraintForwardDir == Vector3.zero)
if (m_localConstraintAxisX == Vector3.zero)
{
Debug.LogError("Constaint axis cannot be zero vector");
return false;
Expand Down Expand Up @@ -83,7 +81,7 @@ internal override bool _BuildConstraint() {
}
BM.Matrix frameInA, frameInOther;
string errormsg = "";
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
{
//warning the frameInA, frameInB version of the constructor is broken
m_constraintPtr = new HingeConstraint(rbb, rba, frameInOther.Origin, frameInA.Origin, (BM.Vector3)frameInOther.Basis.Column1, (BM.Vector3)frameInA.Basis.Column1);
Expand All @@ -96,7 +94,7 @@ internal override bool _BuildConstraint() {
else {
//BM.Matrix frameInA = BM.Matrix.Identity;
//CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInA);
m_constraintPtr = new HingeConstraint(rba, m_localConstraintPoint.ToBullet(),m_localConstraintForwardDir.ToBullet(), false);
m_constraintPtr = new HingeConstraint(rba, m_localConstraintPoint.ToBullet(),m_localConstraintAxisX.ToBullet(), false);
}
if (m_enableMotor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@
namespace BulletUnity {
[System.Serializable]
public class BSliderConstraint : BTypedConstraint {

[Header("Reference Frame Local To This Object")]
public Vector3 m_localConstraintPoint = Vector3.zero;
public Vector3 m_localConstraintForwardDir = Vector3.forward;
public Vector3 m_localConstraintUpDir = Vector3.up;
public static string HelpMessage = "X (red) is slide axis. Angular limits are measured from Y (green) toward Z. \n" +
"\nTIP: To see constraint limits:\n" +
" - In BulletPhysicsWorld turn on 'Do Debug Draw' and set 'Debug Draw Mode' flags\n" +
" - On Constraint set 'Debug Draw Size'\n" +
" - Press play";

[Header("Limits")]
public float m_lowerLinearLimit = -10f;
public float m_upperLinearLimit = 10f;
public float m_lowerAngularLimitRadians = -Mathf.PI;
public float m_upperAngularLimitRadians = Mathf.PI;

public void OnDrawGizmosSelected()
{
DrawTransformGizmos(transform, m_localConstraintPoint, m_localConstraintForwardDir, m_localConstraintUpDir);
}

//called by Physics World just before constraint is added to world.
//the current constraint properties are used to rebuild the constraint.
internal override bool _BuildConstraint() {
Expand Down Expand Up @@ -69,7 +64,7 @@ internal override bool _BuildConstraint() {

BM.Matrix frameInA, frameInOther;
string errormsg = "";
if (CreateFramesA_B(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
{
m_constraintPtr = new SliderConstraint(rbb, rba, frameInOther, frameInA, true);
} else
Expand All @@ -81,7 +76,7 @@ internal override bool _BuildConstraint() {
{
BulletSharp.Math.Matrix frameInA = BM.Matrix.Identity;
string errormsg = "";
if (CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInA, ref errormsg))
if (CreateFrame(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, ref frameInA, ref errormsg))
{
m_constraintPtr = new SliderConstraint(rba, frameInA, true);
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public enum ConstraintType
constrainToAnotherBody
}

[Header("Reference Frame Local To This Object")]
public Vector3 m_localConstraintPoint = Vector3.zero;
public Vector3 m_localConstraintAxisX = Vector3.forward;
public Vector3 m_localConstraintAxisY = Vector3.up;



public float m_breakingImpulseThreshold = Mathf.Infinity;
public bool m_disableCollisionsBetweenConstrainedBodies = true;
public ConstraintType m_constraintType;
Expand All @@ -30,18 +37,18 @@ public enum ConstraintType
public void DrawTransformGizmos(Transform t, Vector3 pivotPoint, Vector3 forward, Vector3 up)
{
Vector3 pivotWorld = t.TransformPoint(pivotPoint);
Vector3 forwardWorld = t.TransformDirection(forward).normalized;
Vector3 upWorld = t.TransformDirection(up).normalized;
Vector3 rightWorld = Vector3.Cross(forwardWorld, upWorld);
upWorld = Vector3.Cross(rightWorld, forwardWorld);
upWorld.Normalize();
forwardWorld.Normalize();
Vector3 xWorld = t.TransformDirection(forward).normalized;
Vector3 yWorld = t.TransformDirection(up).normalized;
Vector3 zWorld = Vector3.Cross(xWorld, yWorld);
yWorld = Vector3.Cross(zWorld, xWorld);
yWorld.Normalize();
xWorld.Normalize();
Gizmos.color = Color.red;
Gizmos.DrawLine(pivotWorld, pivotWorld + rightWorld);
Gizmos.DrawLine(pivotWorld, pivotWorld + xWorld);
Gizmos.color = Color.green;
Gizmos.DrawLine(pivotWorld, pivotWorld + upWorld);
Gizmos.DrawLine(pivotWorld, pivotWorld + yWorld);
Gizmos.color = Color.blue;
Gizmos.DrawLine(pivotWorld, pivotWorld + forwardWorld);
Gizmos.DrawLine(pivotWorld, pivotWorld + zWorld);
}

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

public virtual void OnDrawGizmosSelected()
{
DrawTransformGizmos(transform, m_localConstraintPoint, m_localConstraintAxisX, m_localConstraintAxisY);
}



protected virtual void AddToBulletWorld()
{
if (!m_isInWorld)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEditor;
using UnityEngine;
using BulletUnity;

[CustomEditor(typeof(B6DOFConstraint))]
public class B6DOFConstraintEditor : Editor {



public override void OnInspectorGUI() {
EditorGUILayout.HelpBox(B6DOFConstraint.HelpMessage, MessageType.Info);
DrawDefaultInspector();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEditor;
using UnityEngine;
using BulletUnity;

[CustomEditor(typeof(BBallSocketConstraint))]
public class BBallSocketConstraintEditor : Editor {



public override void OnInspectorGUI() {
EditorGUILayout.HelpBox(BBallSocketConstraint.HelpMessage, MessageType.Info);
DrawDefaultInspector();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class BHingedConstraintEditor : Editor {

public override void OnInspectorGUI() {
BHingedConstraint hc = (BHingedConstraint)target;
EditorGUILayout.HelpBox(BHingedConstraint.HelpMessage, MessageType.Info);
EditorGUILayout.LabelField("Hinge Angle " + hc.GetAngle());
DrawDefaultInspector();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using UnityEditor;
using UnityEngine;
using BulletUnity;

[CustomEditor(typeof(BSliderConstraint))]
public class BSliderConstraintEditor : Editor {



public override void OnInspectorGUI() {
BSliderConstraint hc = (BSliderConstraint)target;
EditorGUILayout.HelpBox(BSliderConstraint.HelpMessage, MessageType.Info);
DrawDefaultInspector();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 05a85e0

Please sign in to comment.