예제 #1
0
    public override void PostSceneInsertionSetup()
    {
        if (wheels.Count > 0)
        {
            IntPtr   wheelSimData   = PhysXLib.CreateWheelSimData(wheels.Count);
            IntPtr[] suspensions    = new IntPtr[wheels.Count];
            IntPtr   wheelPositions = PhysXLib.CreateVectorArray();

            for (int i = 0; i < wheels.Count; i++)
            {
                suspensions[i] = wheels[i].SetupInitialProperties();
                PhysXLib.AddVectorToArray(wheelPositions, new PhysXVec3(transform.InverseTransformPoint(wheels[i].worldWheelCentre)));
            }

            PhysXLib.SetSuspensionSprungMasses(suspensions, wheels.Count, wheelPositions, new PhysXVec3(Vector3.zero), mass);

            for (int i = 0; i < wheels.Count; i++)
            {
                //Debug.Log(vehicleId);
                wheels[i].SetupSimData(this, wheelSimData, i, vehicleId);
            }

            vehicle = PhysXLib.CreateVehicleFromRigidBody(physXBody, wheelSimData);

            for (int i = 0; i < wheels.Count; i++)
            {
                wheels[i].SetVehicle(vehicle);
            }
        }

        physXCOMPosition.FromVector(centreOfMass);
        // PhysXVec3 position = new PhysXVec3(centreOfMass);
        // PhysXQuat rotation = new PhysXQuat(Quaternion.identity);

        IntPtr oldCentre = PhysXLib.GetCentreOfMass(physXBody);

        IntPtr newCentre = PhysXLib.CreateTransform(physXCOMPosition, physXCOMRotation);

        PhysXLib.SetRigidBodyMassPose(physXBody, newCentre);

        if (vehicle != IntPtr.Zero)
        {
            PhysXLib.UpdateVehicleCentreOfMass(oldCentre, newCentre, vehicle);
        }

        if (GetComponentInChildren <CollisionSoftener>() != null)
        {
            Debug.Log("made ghost body");
            ghostBody     = PhysXLib.CreateGhostRigidBody(physXBody, ghostBlend);
            _ghostEnabled = true;
        }
    }
    // private Mesh meshMesh = null;

    // Start is called before the first frame update
    public override void Setup(PhysXBody attachedRigidBody, uint vehicleId)
    {
        if (mesh == null)
        {
            Debug.LogError("Collider mesh is null on " + gameObject.name);
        }
        else if (!mesh.isReadable)
        {
            Debug.LogError("Collider mesh: " + mesh.name + " is not readable");
        }
        else
        {
            // meshMesh = Instantiate(mesh);
            // meshMesh.Clear();
            // List<Vector3> meshMeshVertices = new List<Vector3>();

            IntPtr vertexArray = PhysXLib.CreateVectorArray();

            Vector3[] unscaledVertices = mesh.vertices;
            Vector3[] vertices         = new Vector3[unscaledVertices.Length];
            for (int i = 0; i < unscaledVertices.Length; i++)
            {
                vertices[i] = new Vector3(unscaledVertices[i].x * scale.x, unscaledVertices[i].y * scale.y, unscaledVertices[i].z * scale.z);
                // vertices[i] = unscaledVertices[i];//new Vector3(unscaledVertices[i].x * scale.x, unscaledVertices[i].y * scale.y, unscaledVertices[i].z * scale.z);
            }

            Vector3 centre = Vector3.zero;
            foreach (Vector3 vertex in vertices)
            {
                centre += vertex;
            }
            centre /= vertices.Length;

            // offset += new Vector3(centre.x * scale.x, centre.y * scale.y, centre.z * scale.z);
            offset += centre;

            PhysXVec3 physXVertex = new PhysXVec3(Vector3.zero);
            foreach (Vector3 vertex in vertices)
            {
                physXVertex.FromVector(vertex - centre);
                PhysXLib.AddVectorToArray(vertexArray, physXVertex);
            }

            IntPtr geom = IntPtr.Zero;
            if (convex)
            {
                geom = PhysXLib.CreateConvexMeshGeometry(vertexArray, new PhysXVec3(Vector3.one));
            }
            else
            {
                geom = PhysXLib.CreateMeshGeometry(vertexArray, mesh.triangles, mesh.triangles.Length / 3, new PhysXVec3(Vector3.one));
            }

            shape = PhysXLib.CreateShape(geom, physXMaterial, 0.02f);

            // if (!convex) {
            //     int vertexCount = PhysXLib.GetMeshVertexCount(geom);
            //     int triCount = PhysXLib.GetMeshTriangleCount(geom);
            //     IntPtr usedVertices = PhysXLib.CreateVectorArray();
            //     int[] usedTris = new int[triCount * 3];
            //     PhysXLib.GetMeshGeometry(geom, usedVertices, usedTris);

            //     for (int i = 0; i < vertexCount; i++) {
            //         PhysXLib.GetVectorFromArray(usedVertices, physXVertex, i);
            //         meshMeshVertices.Add(physXVertex.ToVector());
            //     }
            //     meshMesh.SetVertices(meshMeshVertices);
            //     meshMesh.triangles = usedTris;
            //     meshMesh.RecalculateNormals();
            // }

            base.Setup(attachedRigidBody, vehicleId);
        }
    }