Example #1
0
        public void RemoveCollider(NewtonCollider collider)
        {
            if (ReferenceEquals(collider, m_RootCollider) && (collider is NewtonNullCollider || collider is NewtonCompoundCollider || collider is NewtonSceneCollider))
            {
                throw new NullReferenceException(string.Format("Can not remove root collider of type {0}.", collider.GetType().Name));
            }

            bool success = m_Colliders.Remove(collider);

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

                    sceneShape.BeginAddRemoveCollision();
                    sceneShape.RemoveCollision(collider.m_ParentHandle);
                    collider.m_ParentHandle = IntPtr.Zero;
                    sceneShape.EndAddRemoveCollision();
                }
                else if (m_Colliders.Count == 0)
                {
                    NewtonCollider   nullCollider = m_Body.gameObject.AddComponent <NewtonNullCollider>();
                    dNewtonCollision nullShape    = nullCollider.Create(m_Body.World);
                    nullCollider.SetShape(nullShape);

                    m_RootCollider = nullCollider;
                    m_Colliders.Add(nullCollider);

                    m_Body.GetBody().SetCollision(nullShape);
                }
                else
                {
                    if (m_Colliders.Count == 2)
                    {
                        m_Body.GetBody().SetCollision(collider.GetShape());

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


                        m_RootCollider = m_Colliders.First();
                    }
                    else
                    {
                        var compoundShape = (dNewtonCollisionCompound)GetShape();

                        compoundShape.BeginAddRemoveCollision();
                        compoundShape.RemoveCollision(collider.m_ParentHandle);
                        collider.m_ParentHandle = IntPtr.Zero;
                        compoundShape.EndAddRemoveCollision();
                    }
                }


                m_Body.ResetCenterOfMass();
            }
        }
Example #2
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();
            }
        }
Example #3
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();
            }
        }