Question by
lignumSM · May 03, 2019 at 05:01 PM ·
oculus riftgrabfreezerotation
VR Grab Freeze Y-Axis and X/Z-Rotation depending on Variable
I am using an Oculus Rift with the Oculus Integration (OVRGrabbable and OVRGrabber). The Goal is to freeze the Y-Axis and the X/Z-Rotation if a variable in the OVRGrabbable Script is activated. I created a serializeField with the variable m_yFest in the OVRGrabbable Script. I am also able the receive the variable within the OVRGrabber script by calling m_grabbedObj.yFest. Freezing the Y-Axis works but I don t know what to do with the rotation. I tried almost everything but it will teleport to another position, doesn t rotate at all or will do other wierd stuff.
Here are the two voids where I tried to freeze the axis:
protected virtual void GrabBegin()
{
float closestMagSq = float.MaxValue;
OVRGrabbable closestGrabbable = null;
Collider closestGrabbableCollider = null;
// Iterate grab candidates and find the closest grabbable candidate
foreach (OVRGrabbable grabbable in m_grabCandidates.Keys)
{
bool canGrab = !(grabbable.isGrabbed && !grabbable.allowOffhandGrab);
if (!canGrab)
{
continue;
}
for (int j = 0; j < grabbable.grabPoints.Length; ++j)
{
Collider grabbableCollider = grabbable.grabPoints[j];
// Store the closest grabbable
Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(m_gripTransform.position);
float grabbableMagSq = (m_gripTransform.position - closestPointOnBounds).sqrMagnitude;
if (grabbableMagSq < closestMagSq)
{
closestMagSq = grabbableMagSq;
closestGrabbable = grabbable;
closestGrabbableCollider = grabbableCollider;
}
}
}
// Disable grab volumes to prevent overlaps
GrabVolumeEnable(false);
if (closestGrabbable != null)
{
if (closestGrabbable.isGrabbed)
{
closestGrabbable.grabbedBy.OffhandGrabbed(closestGrabbable);
}
m_grabbedObj = closestGrabbable;
m_grabbedObj.GrabBegin(this, closestGrabbableCollider);
m_lastPos = transform.position;
m_lastRot = transform.rotation;
// Set up offsets for grabbed object desired position relative to hand.
if(m_grabbedObj.snapPosition)
{
m_grabbedObjectPosOff = m_gripTransform.localPosition;
if(m_grabbedObj.snapOffset)
{
Vector3 snapOffset = m_grabbedObj.snapOffset.position;
if (m_controller == OVRInput.Controller.LTouch) snapOffset.x = -snapOffset.x;
m_grabbedObjectPosOff += snapOffset;
}
}
{
Vector3 relPos = m_grabbedObj.transform.position - transform.position;
m_relPos = m_grabbedObj.transform.position - transform.position;
relPos = Quaternion.Inverse(transform.rotation) * relPos;
m_grabbedObjectPosOff = relPos;
}
if (m_grabbedObj.snapOrientation)
{
m_grabbedObjectRotOff = m_gripTransform.localRotation;
if(m_grabbedObj.snapOffset)
{
m_grabbedObjectRotOff = m_grabbedObj.snapOffset.rotation * m_grabbedObjectRotOff;
}
}
else
{
Quaternion relOri = Quaternion.Inverse(transform.rotation) * m_grabbedObj.transform.rotation;
m_relOri = Quaternion.Euler(0, transform.rotation.y, 0) * m_grabbedObj.transform.rotation;
m_grabbedObjectRotOff = relOri;
}
// Note: force teleport on grab, to avoid high-speed travel to dest which hits a lot of other objects at high
// speed and sends them flying. The grabbed object may still teleport inside of other objects, but fixing that
// is beyond the scope of this demo.
MoveGrabbedObject(m_lastPos, m_lastRot, true);
if(m_parentHeldObject)
{
m_grabbedObj.transform.parent = transform;
}
}
}
protected virtual void MoveGrabbedObject(Vector3 pos, Quaternion rot, bool forceTeleport = false)
{
if (m_grabbedObj == null)
{
return;
}
Rigidbody grabbedRigidbody = m_grabbedObj.grabbedRigidbody;
Vector3 grabbablePosition = pos + rot * m_grabbedObjectPosOff;
Quaternion grabbableRotation = rot * m_grabbedObjectRotOff;
if (forceTeleport)
{
grabbedRigidbody.transform.position = grabbablePosition;
grabbedRigidbody.transform.rotation = grabbableRotation;
}
else
{
if (m_grabbedObj.yFest == true)
{
Vector3 posOhneY = new Vector3(pos.x + m_relPos.x, grabbedRigidbody.transform.position.y, pos.z + m_relPos.z);
///grabbedRigidbody.MovePosition(grabbablePosition);
grabbedRigidbody.MovePosition(posOhneY);
//Quaternion rotOhneXZ = Quaternion.Euler(grabbedRigidbody.transform.rotation.x, transform.rotation.y, grabbedRigidbody.transform.rotation.z);
m_grabbedObjectRotOff = Quaternion.Euler(0f, m_grabbedObjectRotOff.y, 0f);
rot = Quaternion.Euler(0f, rot.y, 0f);
Quaternion rotOhneXZ = rot * m_grabbedObjectRotOff;
//grabbedRigidbody.MoveRotation(rotOhneXZ);
}
else
{
grabbedRigidbody.MovePosition(grabbablePosition);
grabbedRigidbody.MoveRotation(grabbableRotation);
}
}
}
Thanks in advance
Comment