Ejemplo n.º 1
0
        public void RemoveBodyFromLeap(Rigidbody rigidbody)
        {
            Body body = BodyMapper[rigidbody.gameObject];

            BodyMapper.Remove(rigidbody.gameObject);
            Scene.RemoveBody(body);
        }
        public void OnBodyRemovedCallback(Body body)
        {
            // find gameobject
            GameObject gameObject = BodyMapper.FirstOrDefault(x => x.Value.BodyId.ptr == body.BodyId.ptr).Key;

            GameObject.Destroy(gameObject);
            BodyMapper.Remove(gameObject);
        }
Ejemplo n.º 3
0
        public Body AddBodyToLeapFromUnity(Rigidbody rigidbody)
        {
            LeapInteraction properties = rigidbody.GetComponent <LeapInteraction>();

            if (rigidbody.collider && properties)
            {
                Collider[] colliders = rigidbody.GetComponents <Collider>();

                Shape shape = new Shape();
                foreach (Collider collider in colliders)
                {
                    if (collider is SphereCollider)
                    {
                        float          scale = rigidbody.transform.lossyScale.x;
                        SphereCollider sc    = collider as SphereCollider;
                        shape = Shape.CreateSphere(sc.radius * scale);
                    }
                    else if (collider is CapsuleCollider)
                    {
                        float           scale = rigidbody.transform.lossyScale.x;
                        CapsuleCollider cc    = collider as CapsuleCollider;
                        shape = Shape.CreateCapsule((Shape.CapsuleOrientation)cc.direction, Math.Max(0f, cc.height / 2f - cc.radius) * scale, cc.radius * scale);
                    }
                    else if (collider is BoxCollider)
                    {
                        BoxCollider bc    = collider as BoxCollider;
                        Vector3     scale = collider.transform.lossyScale;
                        shape = Shape.CreateBox(Vector3.Scale(bc.size, scale) / 2f, 0f);
                    }
                }

                if (shape != IntPtr.Zero)
                {
                    Body body = new Body();//shape);
                    body.Shape = shape;
                    body.Mass  = rigidbody.mass;

                    // Add body anchors.
                    for (int i = 0; i < rigidbody.transform.childCount; i++)
                    {
                        Transform child = rigidbody.transform.GetChild(i);
                        if (child.name.StartsWith("Anchor") || child.name.StartsWith("ClickAnchor"))
                        {
                            LeapTransform anchor = new LeapTransform();
                            anchor.Position = Vector3.Scale(child.localPosition - rigidbody.transform.rotation * TransformSyncUtil.GetCenterFromCollider(rigidbody.gameObject), rigidbody.transform.lossyScale);
                            anchor.Rotation = child.localRotation;
                            if (child.name.StartsWith("Anchor"))
                            {
                                body.Shape.AddAnchor(anchor);
                            }
                            if (child.name.StartsWith("ClickAnchor"))
                            {
                                body.Shape.AddClickAnchor(anchor);
                            }
                        }
                    }

                    // Apply BodyProperties
                    properties.ApplyToBody(body);

                    Scene.AddBody(body);
                    BodyMapper.Add(rigidbody.gameObject, body);

                    rigidbody.maxAngularVelocity = 100.0f;

                    return(body);
                }
            }
            return(null);
        }
        public void OnBodyAddedCallback(Body body)// IntPtr bodyHandle)
        {
            // Create the game object
            bool       isFinger = false;
            GameObject gameObject;

            Shape.ShapeType type = body.Shape.Type;
            if (type == Shape.ShapeType.Capsule)
            {
                float capsuleRadius        = body.Shape.CapsuleRadius;
                float capsuleSegmentLength = body.Shape.CapsuleSegmentLength;

                gameObject = GameObject.Instantiate(Resources.Load(UnityUtil.ResourcesFolder + UnityUtil.FingerBoneTemplateName) as GameObject) as GameObject;

                // Adjust dimensions
                float capsuleHeight = capsuleRadius * 2f + capsuleSegmentLength;
                float yScale        = capsuleHeight / 2f;
                float zxScale       = capsuleRadius / 0.5f;
                gameObject.transform.localScale = new Vector3(zxScale, yScale, zxScale);
                gameObject.name = UnityUtil.FingerBoneName;

                LeapTransform t = new LeapTransform();
                t = new LeapTransform(Native.AccessPropertyAsTransform(body, Native.Property.BodyTransform, Native.Mode.Get, t.ToNative()));
                gameObject.transform.position = t.Position;
                gameObject.transform.rotation = t.Rotation;

                gameObject.rigidbody.maxAngularVelocity = 100.0f;

                isFinger = true;
                fingerBoneCount++;
            }
            else
            {
                gameObject      = GameObject.Instantiate(Resources.Load(UnityUtil.ResourcesFolder + UnityUtil.HandPalmTemplateName) as GameObject) as GameObject;
                gameObject.name = UnityUtil.HandPalmName;
                LeapTransform t = new LeapTransform();
                t = new LeapTransform(Native.AccessPropertyAsTransform(body, Native.Property.BodyTransform, Native.Mode.Get, t.ToNative()));
                gameObject.transform.position = t.Position;
                gameObject.transform.rotation = t.Rotation;

                gameObject.rigidbody.maxAngularVelocity = 100.0f;

                latestHandPalmAdded = gameObject;
                fingerBoneCount     = 0;
            }

            BodyMapper.Add(gameObject, body);

            GameObject container = isFinger && latestHandPalmAdded != null ? latestHandPalmAdded : GameObject.Find(UnityUtil.DynamicObjectContainerName) as GameObject;

            gameObject.transform.parent = container.transform;

            gameObject.layer = UnityUtil.LayerForHands;

            if (fingerBoneCount % 3 == 0 && fingerBoneCount > 0)
            {
                gameObject.name = UnityUtil.FingerTipBoneName;
                if (fingerBoneCount == 3)
                {
                    gameObject.name = UnityUtil.ThumbTipBoneName;
                }
            }

            if (fingerBoneCount == 15)
            {
                latestHandPalmAdded = null;
                fingerBoneCount     = 0;

                if (UnityUtil.FilterHandCollisionPerColliderExplicitly)
                {
                    DisableHandSelfCollisions(gameObject.transform.parent);
                }
            }
        }