152 lines
6.3 KiB
C#
152 lines
6.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace PhysicsTools
|
|
{
|
|
public class Joint
|
|
{
|
|
public UnityEngine.Joint joint;
|
|
|
|
public Joint(Segment seg1, Segment seg2, JointProperties prop, SegmentPropertiesBase segProperties,
|
|
Quaternion twistOffset)
|
|
{
|
|
if (seg2 == null)
|
|
{
|
|
}
|
|
|
|
seg2.seg.transform.rotation *= twistOffset;
|
|
switch (prop.type)
|
|
{
|
|
case JointProperties.Type.HINGE_JOINT:
|
|
{
|
|
HingeJoint hingeJoint = seg1.seg.AddComponent<HingeJoint>();
|
|
hingeJoint.autoConfigureConnectedAnchor = false;
|
|
joint = hingeJoint;
|
|
JointLimits limits = new JointLimits
|
|
{
|
|
min = 0f - prop.swingLimitDeg,
|
|
max = prop.swingLimitDeg
|
|
};
|
|
joint.axis = new Vector3(0f, 0f, 1f);
|
|
hingeJoint.limits = limits;
|
|
break;
|
|
}
|
|
case JointProperties.Type.CONFIGURABLE_JOINT:
|
|
{
|
|
ConfigurableJoint configurableJoint = seg1.seg.AddComponent<ConfigurableJoint>();
|
|
configurableJoint.enableCollision = false;
|
|
configurableJoint.xMotion = ConfigurableJointMotion.Locked;
|
|
configurableJoint.yMotion = ConfigurableJointMotion.Locked;
|
|
configurableJoint.zMotion = ConfigurableJointMotion.Locked;
|
|
configurableJoint.angularXMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.angularYMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.angularZMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.autoConfigureConnectedAnchor = false;
|
|
configurableJoint.lowAngularXLimit = new SoftJointLimit
|
|
{
|
|
limit = 0f - prop.twistLimitDeg
|
|
};
|
|
configurableJoint.highAngularXLimit = new SoftJointLimit
|
|
{
|
|
limit = prop.twistLimitDeg
|
|
};
|
|
configurableJoint.projectionMode = JointProjectionMode.PositionAndRotation;
|
|
configurableJoint.projectionDistance = prop.projectionDistance;
|
|
SoftJointLimit softJointLimit = new SoftJointLimit
|
|
{
|
|
limit = prop.swingLimitDeg
|
|
};
|
|
configurableJoint.angularYLimit = softJointLimit;
|
|
configurableJoint.angularZLimit = softJointLimit;
|
|
joint = configurableJoint;
|
|
joint.axis = new Vector3(0f, 1f, 0f);
|
|
if (prop.breakingForce != 0f)
|
|
{
|
|
configurableJoint.breakForce = prop.breakingForce;
|
|
}
|
|
|
|
configurableJoint.enablePreprocessing = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (segProperties is SegmentPropertiesCylinder)
|
|
{
|
|
joint.anchor = new Vector3(0f, 1f - prop.offsetScale, 0f);
|
|
joint.connectedAnchor = new Vector3(0f, -1f + prop.offsetScale, 0f);
|
|
}
|
|
else
|
|
{
|
|
joint.anchor = new Vector3(0f, (1f - prop.offsetScale) / 2f, 0f);
|
|
joint.connectedAnchor = new Vector3(0f, (-1f + prop.offsetScale) / 2f, 0f);
|
|
}
|
|
|
|
joint.connectedBody = seg2.seg.GetComponent<Rigidbody>();
|
|
}
|
|
|
|
public Joint(GameObject seg1, GameObject seg2, Vector3 vGlobalAnchor, Vector3 vGlobalAxis, JointProperties prop,
|
|
Rope r, int jtPos)
|
|
{
|
|
ConfigurableJoint configurableJoint = seg1.AddComponent<ConfigurableJoint>();
|
|
configurableJoint.enableCollision = false;
|
|
configurableJoint.xMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.yMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.zMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.angularXMotion = ConfigurableJointMotion.Limited;
|
|
configurableJoint.angularYMotion = ConfigurableJointMotion.Free;
|
|
configurableJoint.angularZMotion = ConfigurableJointMotion.Free;
|
|
configurableJoint.anchor = seg1.transform.InverseTransformPoint(vGlobalAnchor);
|
|
configurableJoint.axis = seg1.transform.InverseTransformDirection(vGlobalAxis);
|
|
if (seg2 != null)
|
|
{
|
|
configurableJoint.connectedBody = seg2.GetComponent<Rigidbody>();
|
|
}
|
|
|
|
SoftJointLimit linearLimit = new SoftJointLimit
|
|
{
|
|
limit = 0.01f
|
|
};
|
|
SoftJointLimitSpring linearLimitSpring = default(SoftJointLimitSpring);
|
|
switch (jtPos)
|
|
{
|
|
case 0:
|
|
linearLimitSpring = r.getStartJtSpring();
|
|
break;
|
|
case 1:
|
|
linearLimitSpring = r.getEndJtSpring();
|
|
break;
|
|
default:
|
|
{
|
|
float damper = (linearLimitSpring.spring = 0f);
|
|
linearLimitSpring.damper = damper;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (linearLimitSpring.spring == 0f)
|
|
{
|
|
linearLimit.limit = 0f;
|
|
}
|
|
|
|
configurableJoint.linearLimitSpring = linearLimitSpring;
|
|
configurableJoint.linearLimit = linearLimit;
|
|
configurableJoint.projectionMode = JointProjectionMode.PositionAndRotation;
|
|
configurableJoint.projectionDistance = prop.projectionDistanceFirst;
|
|
configurableJoint.lowAngularXLimit = new SoftJointLimit
|
|
{
|
|
limit = 0f - prop.twistLimitDeg
|
|
};
|
|
configurableJoint.highAngularXLimit = new SoftJointLimit
|
|
{
|
|
limit = prop.twistLimitDeg
|
|
};
|
|
if (prop.breakingForce != 0f)
|
|
{
|
|
configurableJoint.breakForce = prop.breakingForce;
|
|
}
|
|
|
|
joint = configurableJoint;
|
|
configurableJoint.autoConfigureConnectedAnchor = false;
|
|
configurableJoint.enablePreprocessing = false;
|
|
}
|
|
}
|
|
} |