// 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);
        }
Пример #2
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);
            }
        }
    }