I am starting to dive into physics constraints and am trying to setup a simple example. I would like to have the sphere in the attached picture act as the static body in which the dynamic cube body will try to move towards. Since the cube is on the right of this image, I would want it to fling to the left, back to the right, back in, at lower and lower frequencies finally coming to a rest at the spheres center.
When playing around I’ve gotten the cube to act as expected towards the world origin but I’ve been unable to replicate it for a different point. I don’t really recall what that setup was exactly either but it was something like (you will have to see reference code for this to make sense) and I only used a single body and entity.
physicsJoint.BodyAFromJoint = BodyFrame.Identity
For reference, I’ve looked through the physics joint samples, read pretty much all the physics documentation and have the physics package source code open but I’m still at a loss. I know there are helper APIs for creating constraints, but I’m really trying to get a handle of how to just setup a simple use case from scratch and from what I can tell there is not a constraint for this particular example, maybe it would combine two constraints LimitDOFJoint and something else…
I feel like I’m having difficulty setting the proper BodyAFromJoint and BodyBFromJoint parameters, and when it comes to linear algebra all though I’m well read up on it I don’t have that much practical experience so I often am doing a lot of trial and error instead of fully understanding what perhaps some of the example code is doing when its performing things like
// what exactly does the inverse of a RigidTransform represent?
// what exactly does a multiplication between two RigidTransforms represent?
RigidTransform bFromA = math.mul(math.inverse(worldFromB), worldFromA);
Can someone please explain it to me like I’m 5 on how to get a proper spring setup along the X axis between two points? My next problem would be to implement a similar spring except for an angular constraint instead of a linear one, kinda like a seesaw that comes to equilibrium when starting from a 30ish degree offset.
public class JointTesting : MonoBehaviour, IConvertGameObjectToEntity
{
public GameObject TargetBody;
public void Convert(Entity entity, EntityManager entityManager, GameObjectConversionSystem conversionSystem)
{
var jointEntity = entityManager.CreateEntity(new ComponentType[]
{
typeof(JointTest),
typeof(PhysicsJoint),
typeof(PhysicsConstrainedBodyPair)
});
entityManager.SetName(jointEntity, "PhysicsJoint X Pivot Spring");
var physicsJoint = new PhysicsJoint();
{
physicsJoint.JointType = JointType.Custom;
physicsJoint.SetConstraints(new FixedList128<Constraint>
{
new Constraint
{
Type = ConstraintType.Linear,
ConstrainedAxes = new bool3(false,true,true),
SpringDamping = Constraint.DefaultSpringDamping,
SpringFrequency = Constraint.DefaultSpringFrequency
},
new Constraint
{
Type = ConstraintType.Angular,
ConstrainedAxes = new bool3(true,true,true),
SpringDamping = Constraint.DefaultSpringDamping,
SpringFrequency = Constraint.DefaultSpringFrequency
},
});
var worldFromTarget = Math.DecomposeRigidBodyTransform(TargetBody.transform.localToWorldMatrix);
var worldFromEntity = Math.DecomposeRigidBodyTransform(this.transform.localToWorldMatrix);
// the code below is what I'm trying to wrap my head around
// I know theres a relationship between the cube -> sphere
// I want the sphere (TargetBody) to be the pivot point
// And have the cube (this) to try and spring to equilibrium towards the sphere
// I copy and pasted this from some other examples, not really any logical or critical thinking going on my end
// some basic questions
// what exactly does the inverse of a RigidTransform represent?
// what exactly does a multiplication between two RigidTransforms represent?
RigidTransform bFromA = math.mul(math.inverse(worldFromTarget), worldFromEntity);
var positionLocal = this.transform.position;
var positionInConnectedEntity = math.transform(bFromA, positionLocal);
var bodyAFromJoint = BodyFrame.Identity;
bodyAFromJoint.Position = positionInConnectedEntity;
// what do I set these to? I'm just guessing right now
physicsJoint.BodyAFromJoint = bodyAFromJoint;
physicsJoint.BodyBFromJoint = BodyFrame.Identity;
entityManager.SetComponentData(jointEntity, physicsJoint);
}
var targetEntity = conversionSystem.GetPrimaryEntity(TargetBody);
var constraintPair = new PhysicsConstrainedBodyPair(entity, targetEntity, false);
entityManager.SetComponentData(jointEntity, constraintPair);
}
}