Replies: 1 comment 1 reply
-
Be careful as In case someone also needs to preserve momentum, you can store the local velocity velocity and restore it on teleport. This script restores momentum based on the local frame of reference (e.g. if a flying arrow is teleported and rotated, it will keep going forwards in the new direction, as opposed to flying sideways in the old one): public static unsafe void TeleportMjRoot(MjFreeJoint root, Vector3 unityPos, Quaternion unityRot)
{
MujocoLib.mjData_* Data = mjScene.Data;
Quaternion fromUnityRotation = MjEngineTool.UnityQuaternion(Data->qpos + root.DofAddress + 3); // Store original rotation
Quaternion rotationOffset = unityRot * Quaternion.Inverse(fromUnityRotation); // Get difference to new one (we'll transform velocity with this)
Vector3 newUnityLinVel = rotationOffset * MjEngineTool.UnityVector3(Data->qvel + root.DofAddress);
Vector3 newUnityAngVel = rotationOffset * MjEngineTool.UnityVector3(Data->qvel + root.DofAddress+3);
MjEngineTool.SetMjTransform(Data->qpos + root.QposAddress, unityPos, unityRot); //Set position state
MjEngineTool.SetMjVector3(Data->qvel + root.DofAddress, newUnityLinVel); // Set linear velocity
MjEngineTool.SetMjVector3(Data->qvel + root.DofAddress + 3, newUnityAngVel); //Set angular velocity
//Could update state potentially
//MujocoLib.mjModel_* Model = mjScene.Model;
//MujocoLib.mj_kinematics(Model, Data);
//mjScene.SyncUnityToMjState();
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've found only crumbs of information on how to "teleport" objects in MuJoCo and I'd thought I should share a snippet of how this can be done in the Unity plug-in.
Beta Was this translation helpful? Give feedback.
All reactions