private ContactBone initContactBone(Leap.Bone bone, GameObject contactBoneObj, int boneArrayIndex, Collider boneCollider) { contactBoneObj.layer = _contactBoneParent.gameObject.layer; contactBoneObj.transform.localScale = Vector3.one; ContactBone contactBone = contactBoneObj.GetComponent <ContactBone>(); contactBone.collider = boneCollider; contactBone.interactionController = this; contactBone.interactionHand = this; _contactBones[boneArrayIndex] = contactBone; Transform capsuleTransform = contactBoneObj.transform; capsuleTransform.SetParent(_contactBoneParent.transform, false); Rigidbody body = contactBoneObj.GetComponent <Rigidbody>(); body.freezeRotation = true; contactBone.rigidbody = body; body.useGravity = false; body.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; // TODO: Allow different collision detection modes as an optimization. body.mass = 0.1f; body.position = bone != null?bone.Center.ToVector3() : _unwarpedHandData.PalmPosition.ToVector3(); body.rotation = bone != null?bone.Rotation.ToQuaternion() : _unwarpedHandData.Rotation.ToQuaternion(); contactBone.lastTargetPosition = bone != null?bone.Center.ToVector3() : _unwarpedHandData.PalmPosition.ToVector3(); return(contactBone); }
private void initContactBones() { _colliderBuffer.Clear(); _contactBoneBuffer.Clear(); // Scan for existing colliders and construct contact bones out of them. Utils.FindColliders <Collider>(this.gameObject, _colliderBuffer, includeInactiveObjects: true); foreach (var collider in _colliderBuffer) { if (collider.isTrigger) { continue; // Contact Bones are for "contacting" colliders. } ContactBone contactBone = collider.gameObject.AddComponent <ContactBone>(); Rigidbody body = collider.gameObject.GetComponent <Rigidbody>(); if (body == null) { body = collider.gameObject.AddComponent <Rigidbody>(); } body.freezeRotation = true; body.useGravity = false; body.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; body.mass = 1F; contactBone.interactionController = this; contactBone.rigidbody = body; contactBone.collider = collider; _contactBoneBuffer.Add(contactBone); } //The number of bones is equal to the colliders found that are not set to trigger int numBones = _contactBoneBuffer.Count; _contactBones = new ContactBone[numBones]; _contactBoneLocalPositions = new Vector3[numBones]; _contactBoneLocalRotations = new Quaternion[numBones]; _contactBoneTargetPositions = new Vector3[numBones]; _contactBoneTargetRotations = new Quaternion[numBones]; for (int i = 0; i < numBones; i++) { _contactBones[i] = _contactBoneBuffer[i]; _contactBoneLocalPositions[i] = _contactBoneTargetPositions[i] = this.transform.InverseTransformPoint(_contactBones[i].transform.position); _contactBoneLocalRotations[i] = _contactBoneTargetRotations[i] = this.transform.InverseTransformRotation(_contactBones[i].transform.rotation); } }
private void initContactBones() { _contactBones = new ContactBone[NUM_FINGERS * BONES_PER_FINGER + 1]; _handContactBoneMapFunctions = new BoneMapFunc[NUM_FINGERS * BONES_PER_FINGER + 1]; // Finger bones for (int fingerIndex = 0; fingerIndex < NUM_FINGERS; fingerIndex++) { for (int jointIndex = 0; jointIndex < BONES_PER_FINGER; jointIndex++) { GameObject contactBoneObj = new GameObject("Contact Fingerbone", typeof(CapsuleCollider), typeof(Rigidbody), typeof(ContactBone)); contactBoneObj.layer = manager.contactBoneLayer; Bone bone = _unwarpedHandData.Fingers[fingerIndex] .Bone((Bone.BoneType)(jointIndex) + 1); // +1 to skip first bone. int boneArrayIndex = fingerIndex * BONES_PER_FINGER + jointIndex; contactBoneObj.transform.position = bone.Center.ToVector3(); contactBoneObj.transform.rotation = bone.Rotation.ToQuaternion(); // Remember the method we used to calculate this bone position from // a Leap Hand for later. int fingerIndexCopy = fingerIndex; int jointIndexCopy = jointIndex; _handContactBoneMapFunctions[boneArrayIndex] = (Leap.Hand hand, out Vector3 targetPosition, out Quaternion targetRotation) => { Bone theBone = hand.Fingers[fingerIndexCopy].Bone((Bone.BoneType)(jointIndexCopy + 1)); targetPosition = theBone.Center.ToVector3(); targetRotation = theBone.Rotation.ToQuaternion(); }; CapsuleCollider capsule = contactBoneObj.GetComponent <CapsuleCollider>(); capsule.direction = 2; capsule.radius = bone.Width * 0.5f; capsule.height = bone.Length + bone.Width; capsule.material = defaultContactBoneMaterial; ContactBone contactBone = initContactBone(bone, contactBoneObj, boneArrayIndex, capsule); contactBone.lastTargetPosition = bone.Center.ToVector3(); } } // Palm bone { // Palm is attached to the third metacarpal and derived from it. GameObject contactBoneObj = new GameObject("Contact Palm Bone", typeof(BoxCollider), typeof(Rigidbody), typeof(ContactBone)); Bone bone = _unwarpedHandData.Fingers[(int)Finger.FingerType.TYPE_MIDDLE].Bone(Bone.BoneType.TYPE_METACARPAL); int boneArrayIndex = NUM_FINGERS * BONES_PER_FINGER; contactBoneObj.transform.position = _unwarpedHandData.PalmPosition.ToVector3(); contactBoneObj.transform.rotation = _unwarpedHandData.Rotation.ToQuaternion(); // Remember the method we used to calculate the palm from a Leap Hand for later. _handContactBoneMapFunctions[boneArrayIndex] = (Leap.Hand hand, out Vector3 targetPosition, out Quaternion targetRotation) => { targetPosition = hand.PalmPosition.ToVector3(); targetRotation = hand.Rotation.ToQuaternion(); }; BoxCollider box = contactBoneObj.GetComponent <BoxCollider>(); box.center = new Vector3(_unwarpedHandData.IsLeft ? -0.005f : 0.005f, bone.Width * -0.3f, -0.01f); box.size = new Vector3(bone.Length, bone.Width, bone.Length); box.material = defaultContactBoneMaterial; initContactBone(null, contactBoneObj, boneArrayIndex, box); } // Constrain the bones to each other to prevent separation. addContactBoneJoints(); }
private void initContactBones() { Dictionary <Finger.FingerType, string> _fingerNames = new Dictionary <Finger.FingerType, string>() { { Finger.FingerType.TYPE_INDEX, "Index" }, { Finger.FingerType.TYPE_MIDDLE, "Middle" }, { Finger.FingerType.TYPE_PINKY, "Pinky" }, { Finger.FingerType.TYPE_RING, "Ring" }, { Finger.FingerType.TYPE_THUMB, "Thumb" }, { Finger.FingerType.TYPE_UNKNOWN, "Unknown" } }; Dictionary <Bone.BoneType, string> _boneNames = new Dictionary <Bone.BoneType, string>() { { Bone.BoneType.TYPE_DISTAL, "Distal" }, { Bone.BoneType.TYPE_INTERMEDIATE, "Intermediate" }, { Bone.BoneType.TYPE_INVALID, "Invalid" }, { Bone.BoneType.TYPE_METACARPAL, "Metacarpal" }, { Bone.BoneType.TYPE_PROXIMAL, "Proximal" } }; _contactBones = new ContactBone[NUM_FINGERS * BONES_PER_FINGER + 1]; _handContactBoneMapFunctions = new BoneMapFunc[NUM_FINGERS * BONES_PER_FINGER + 1]; // Finger bones for (int fingerIndex = 0; fingerIndex < NUM_FINGERS; fingerIndex++) { for (int jointIndex = 0; jointIndex < BONES_PER_FINGER; jointIndex++) { Finger finger = _unwarpedHandData.Fingers[fingerIndex]; Bone bone = finger.Bone((Bone.BoneType)(jointIndex) + 1); // +1 to skip first bone. GameObject contactBoneObj = new GameObject("Contact Fingerbone (" + _fingerNames[finger.Type] + "-" + _boneNames[bone.Type] + ")", typeof(CapsuleCollider), typeof(Rigidbody), typeof(ContactBone)); contactBoneObj.layer = manager.contactBoneLayer; int boneArrayIndex = fingerIndex * BONES_PER_FINGER + jointIndex; contactBoneObj.transform.position = bone.Center.ToVector3(); contactBoneObj.transform.rotation = bone.Rotation.ToQuaternion(); // Remember the method we used to calculate this bone position from // a Leap Hand for later. int fingerIndexCopy = fingerIndex; int jointIndexCopy = jointIndex; _handContactBoneMapFunctions[boneArrayIndex] = (Leap.Hand hand, out Vector3 targetPosition, out Quaternion targetRotation) => { Bone theBone = hand.Fingers[fingerIndexCopy].Bone((Bone.BoneType)(jointIndexCopy + 1)); targetPosition = theBone.Center.ToVector3(); targetRotation = theBone.Rotation.ToQuaternion(); }; CapsuleCollider capsule = contactBoneObj.GetComponent <CapsuleCollider>(); capsule.direction = 2; capsule.radius = bone.Width * 0.5f; capsule.height = bone.Length + bone.Width; capsule.material = defaultContactBoneMaterial; ContactBone contactBone = initContactBone(bone, contactBoneObj, boneArrayIndex, capsule); contactBone.lastTargetPosition = bone.Center.ToVector3(); } } // Palm bone { // Palm is attached to the third metacarpal and derived from it. GameObject contactBoneObj = new GameObject("Contact Palm Bone", typeof(BoxCollider), typeof(Rigidbody), typeof(ContactBone)); Bone bone = _unwarpedHandData.Fingers[(int)Finger.FingerType.TYPE_MIDDLE].Bone(Bone.BoneType.TYPE_METACARPAL); int boneArrayIndex = NUM_FINGERS * BONES_PER_FINGER; contactBoneObj.transform.position = _unwarpedHandData.PalmPosition.ToVector3(); contactBoneObj.transform.rotation = _unwarpedHandData.Rotation.ToQuaternion(); // Remember the method we used to calculate the palm from a Leap Hand for later. _handContactBoneMapFunctions[boneArrayIndex] = (Leap.Hand hand, out Vector3 targetPosition, out Quaternion targetRotation) => { targetPosition = hand.PalmPosition.ToVector3(); targetRotation = hand.Rotation.ToQuaternion(); }; BoxCollider box = contactBoneObj.GetComponent <BoxCollider>(); box.center = new Vector3(_unwarpedHandData.IsLeft ? -0.005f : 0.005f, bone.Width * -0.3f, -0.01f); box.size = new Vector3(bone.Length, bone.Width, bone.Length); box.material = defaultContactBoneMaterial; initContactBone(null, contactBoneObj, boneArrayIndex, box); } // Constrain the bones to each other to prevent separation. addContactBoneJoints(); }