Exemplo n.º 1
0
        public void AddCollider(NewtonCollider collider)
        {
            if (collider.GetShape() == null)
            {
                throw new NullReferenceException("Can not add collider with null shape.");
            }

            bool success = m_Colliders.Add(collider);

            if (success)
            {
                if (m_Body.IsScene)
                {
                    var sceneShape = (dNewtonCollisionScene)GetShape();

                    sceneShape.BeginAddRemoveCollision();
                    sceneShape.AddCollision(collider.GetShape());
                    sceneShape.EndAddRemoveCollision();
                }
                else if (m_RootCollider is NewtonNullCollider)
                {
                    m_Body.GetBody().SetCollision(collider.GetShape());

                    m_RootCollider.DestroyShape();
                    m_Colliders.Remove(m_RootCollider);
                    Component.Destroy(m_RootCollider);

                    m_RootCollider = collider;
                }
                else if (m_RootCollider is NewtonCompoundCollider)
                {
                    var compoundShape = (dNewtonCollisionCompound)GetShape();

                    compoundShape.BeginAddRemoveCollision();
                    collider.m_ParentHandle = compoundShape.AddCollision(collider.GetShape());
                    compoundShape.EndAddRemoveCollision();
                }
                else
                {
                    NewtonCompoundCollider   compoundCollider = m_Body.gameObject.AddComponent <NewtonCompoundCollider>();
                    dNewtonCollisionCompound compoundShape    = (dNewtonCollisionCompound)compoundCollider.Create(m_Body.World);
                    compoundCollider.SetShape(compoundShape);

                    m_Colliders.Add(compoundCollider);

                    compoundShape.BeginAddRemoveCollision();
                    m_RootCollider.m_ParentHandle = compoundShape.AddCollision(m_RootCollider.GetShape());
                    collider.m_ParentHandle       = compoundShape.AddCollision(collider.GetShape());
                    compoundShape.EndAddRemoveCollision();

                    m_RootCollider = compoundCollider;
                    m_Body.GetBody().SetCollision(compoundShape);
                }

                m_Body.ResetCenterOfMass();
            }
        }
Exemplo n.º 2
0
        public NewtonBodyCollision(NewtonBody body)
        {
            if (body.World.GetWorld() == null)
            {
                throw new NullReferenceException("Native world instance is null. The World component was probably destroyed");
            }

            m_Body = body;

            List <NewtonCollider> colliderList = new List <NewtonCollider>();

            TraverseColliders(body.gameObject, colliderList, body.gameObject, body);

            if (body.IsScene)
            {
                NewtonSceneCollider   sceneCollider = body.gameObject.AddComponent <NewtonSceneCollider>();
                dNewtonCollisionScene sceneShape    = (dNewtonCollisionScene)sceneCollider.Create(body.World);
                sceneCollider.SetShape(sceneShape);

                m_RootCollider = sceneCollider;
                m_Colliders.Add(sceneCollider);

                sceneShape.BeginAddRemoveCollision();
                foreach (NewtonCollider collider in colliderList)
                {
                    m_Colliders.Add(collider);
                    collider.m_ParentHandle = sceneShape.AddCollision(collider.GetShape());
                }
                sceneShape.EndAddRemoveCollision();
            }
            else if (colliderList.Count == 0)
            {
                NewtonCollider collider = body.gameObject.AddComponent <NewtonNullCollider>();
                collider.SetShape(collider.Create(body.World));

                m_RootCollider = collider;
                m_Colliders.Add(collider);
            }
            else if (colliderList.Count == 1)
            {
                m_RootCollider = colliderList[0];
                m_Colliders.Add(colliderList[0]);
            }
            else
            {
                NewtonCompoundCollider   compoundCollider = body.gameObject.AddComponent <NewtonCompoundCollider>();
                dNewtonCollisionCompound compoundShape    = (dNewtonCollisionCompound)compoundCollider.Create(body.World);
                compoundCollider.SetShape(compoundShape);

                m_RootCollider = compoundCollider;
                m_Colliders.Add(compoundCollider);

                compoundShape.BeginAddRemoveCollision();
                foreach (NewtonCollider collider in colliderList)
                {
                    m_Colliders.Add(collider);
                    collider.m_ParentHandle = compoundShape.AddCollision(collider.GetShape());
                }
                compoundShape.EndAddRemoveCollision();
            }
        }