Ejemplo n.º 1
0
    public override dNewtonCollision Create(NewtonWorld world)
    {
        dNewtonCollision shape = base.Create(world);

        m_scale.y = 1.0f;
        return(shape);
    }
        public void SetMaterial(dNewtonCollision shape)
        {
            int materialID = m_Material ? m_Material.GetInstanceID() : 0;

            shape.SetMaterialID(materialID);
            shape.SetAsTrigger(m_IsTrigger);
        }
        // these are all privates
        private void UpdateParams(dNewtonCollision shape)
        {
            Vector3 scale = GetScale();

            shape.SetScale(scale.x, scale.y, scale.z);

            dMatrix matrix = Utils.ToMatrix(m_Position, Quaternion.Euler(m_Rotation));

            if (transform.gameObject.GetComponent <NewtonBody>() == null)
            {
                matrix = matrix.matrixMultiply(Utils.ToMatrix(transform.position, transform.rotation));
                Transform bodyTransform = transform.parent;
                while ((bodyTransform != null) && (bodyTransform.gameObject.GetComponent <NewtonBody>() == null))
                {
                    bodyTransform = bodyTransform.parent;
                }

                if (bodyTransform != null)
                {
                    dMatrix bodyMatrix = Utils.ToMatrix(bodyTransform.position, bodyTransform.rotation);
                    matrix = matrix.matrixMultiply(bodyMatrix.Inverse());
                }
            }
            shape.SetMatrix(matrix);
        }
Ejemplo n.º 4
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();
            }
        }
        public dNewtonCollision CreateBodyShape(NewtonWorld world)
        {
            dNewtonCollision shape = Create(world);

            if (shape != null)
            {
                UpdateParams(shape);
            }
            return(shape);
        }
        internal void SetShape(dNewtonCollision shape)
        {
            if (m_Shape != null)
            {
                throw new Exception("Collision shape already set for collider.");
            }

            m_Shape = shape;
            var handle = GCHandle.Alloc(this);

            m_Shape.SetUserData(GCHandle.ToIntPtr(handle));
        }
 public void SetLayer(dNewtonCollision shape)
 {
     shape.SetLayer(m_Layer);
 }
Ejemplo n.º 8
0
    private void TraverseColliders(GameObject gameObject, List <ColliderShapePair> colliderList, GameObject rootObject, NewtonBody body)
    {
        // Don't fetch colliders from children with NewtonBodies
        if ((gameObject == rootObject) || (gameObject.GetComponent <NewtonBody>() == null))
        {
            //Fetch all colliders
            foreach (NewtonCollider collider in gameObject.GetComponents <NewtonCollider>())
            {
                dNewtonCollision shape = collider.CreateBodyShape(body.m_world);
                if (shape != null)
                {
                    ColliderShapePair pair;
                    pair.m_collider = collider;
                    pair.m_shape    = shape;
                    colliderList.Add(pair);
                }
            }

            Terrain terrain = gameObject.GetComponent <Terrain>();
            if (terrain)
            {
                NewtonHeighfieldCollider heighfield = gameObject.GetComponent <NewtonHeighfieldCollider>();
                if (heighfield)
                {
                    TerrainData data = terrain.terrainData;

                    int             treesCount        = data.treeInstanceCount;
                    TreeInstance[]  treeInstanceArray = data.treeInstances;
                    TreePrototype[] treeProtoArray    = data.treePrototypes;

                    Vector3 posit = Vector3.zero;
                    for (int i = 0; i < treesCount; i++)
                    {
                        TreeInstance tree = treeInstanceArray[i];
                        posit.x = tree.position.x * data.size.x;
                        posit.y = tree.position.y * data.size.y;
                        posit.z = tree.position.z * data.size.z;

                        //Debug.Log("xxx0 " + posit);
                        TreePrototype treeProto      = treeProtoArray[tree.prototypeIndex];
                        GameObject    treeGameObject = treeProto.prefab;
                        foreach (NewtonCollider treeCollider in treeGameObject.GetComponents <NewtonCollider>())
                        {
                            dNewtonCollision treeShape = treeCollider.CreateBodyShape(body.m_world);
                            if (treeShape != null)
                            {
                                ColliderShapePair pair;
                                Vector3           treePosit = terrain.transform.position + treeCollider.m_posit + posit;
                                //Debug.Log("xxx1 " + treePosit);
                                dMatrix matrix = Utils.ToMatrix(treePosit, Quaternion.identity);
                                treeShape.SetMatrix(matrix);

                                pair.m_collider = treeCollider;
                                pair.m_shape    = treeShape;
                                colliderList.Add(pair);
                            }
                        }
                    }
                }
            }


            foreach (Transform child in gameObject.transform)
            {
                TraverseColliders(child.gameObject, colliderList, rootObject, body);
            }
        }
    }