public void SetupSimData(PhysXRigidBody attachedRigidBody, IntPtr wheelSimData, int wheelNum, uint vehicleId)
    {
        if (autoCalculateSpringStrength)
        {
            float sprungMass = PhysXLib.GetSuspensionSprungMass(suspension);
            suspensionSpringStrength = (Physics.gravity.magnitude * sprungMass) / (suspensionDistance * (1 - suspensionSpringTargetPosition));
        }

        this.wheelNum = wheelNum;

        this.attachedRigidBody = attachedRigidBody;

        Transform bodyParent = attachedRigidBody.transform;

        PhysXVec3 wheelCentrePos = new PhysXVec3(bodyParent.InverseTransformPoint(transform.TransformPoint(wheelCentre)));
        PhysXVec3 forceAppPos    = new PhysXVec3(bodyParent.InverseTransformPoint(transform.TransformPoint(wheelCentre + forceAppPoint)));

        PhysXLib.SetWheelSimForceAppPoint(wheelSimData, wheelNum, forceAppPos);
        PhysXLib.SetWheelSimWheelCentre(wheelSimData, wheelNum, wheelCentrePos);
        PhysXLib.SetWheelSimWheelData(wheelSimData, wheelNum, wheel);
        PhysXLib.SetWheelSimTireData(wheelSimData, wheelNum, tire);
        PhysXLib.SetWheelSimSuspensionData(wheelSimData, wheelNum, suspension, new PhysXVec3(-transform.up));
        PhysXLib.SetWheelSimWheelShape(wheelSimData, wheelNum, -1);
        PhysXLib.SetWheelSimQueryFilterData(wheelSimData, wheelNum, 0, 0, 0, vehicleId);
    }
    public IntPtr SetupInitialProperties()
    {
        wheelCentre = Vector3.up * -suspensionDistance * suspensionSpringTargetPosition;

        wheel = PhysXLib.CreateWheelData();
        PhysXLib.SetWheelMass(wheel, mass);
        PhysXLib.SetWheelRadius(wheel, radius);
        PhysXLib.SetWheelDampingRate(wheel, dampingRate);
        PhysXLib.SetWheelMomentOfInertia(wheel, 0.5f * mass * radius * radius);
        PhysXLib.SetWheelWidth(wheel, width);

        tire = PhysXLib.CreateTireData();
        PhysXLib.SetTireBaseFriction(tire, baseForwardFriction);
        PhysXLib.SetTireMaxFrictionSlipPoint(tire, extremumForwardSlip);
        PhysXLib.SetTireMaxFriction(tire, extremumForwardFriction);
        PhysXLib.SetTirePlateuxSlipPoint(tire, asymptoteForwardSlip);
        PhysXLib.SetTirePlateuxFriction(tire, asymptoteForwardFriction);
        PhysXLib.SetTireLongitudinalStiffnessScale(tire, forwardStiffness);
        PhysXLib.SetTireLateralStiffnessMaxLoad(tire, asymptoteSidewaysTireLoad);
        PhysXLib.SetTireMaxLateralStiffness(tire, _asymptoteSidewaysStiffness);

        suspension = PhysXLib.CreateSuspensionData();
        PhysXLib.SetSuspensionMaxCompression(suspension, suspensionDistance * suspensionSpringTargetPosition);
        PhysXLib.SetSuspensionMaxDroop(suspension, suspensionDistance * (1 - suspensionSpringTargetPosition));
        PhysXLib.SetSuspensionSpringStrength(suspension, suspensionSpringStrength);
        PhysXLib.SetSuspensionSpringDamper(suspension, suspensionSpringDamper);

        return(suspension);
    }
Exemplo n.º 3
0
    void Awake()
    {
        if (sceneManagerExists)
        {
            Debug.Log("PhysX Scene Manager already exists");
            Destroy(gameObject);
            return;
        }

        IntPtr unalignedMem = Marshal.AllocHGlobal(scratchKilobytes * 1024 + 15);

        scratchMem = new IntPtr(16 * ((unalignedMem.ToInt64() + 15) / 16));

        sceneManagerExists = true;

        GetComponent <PhysicsToggle>().Setup();

        PhysXLib.SetupPhysX();
        PhysXLib.RegisterCollisionCallback(AddCollision);
        PhysXLib.RegisterTriggerCallback(AddTrigger);
        DontDestroyOnLoad(gameObject);
        SceneManager.sceneLoaded   += OnSceneLoaded;
        SceneManager.sceneUnloaded += OnSceneUnloaded;
        PhysXSceneSimulator.AddScene(this);
    }
Exemplo n.º 4
0
 protected override void OnDestroy()
 {
     if (vehicle != IntPtr.Zero)
     {
         PhysXLib.DestroyVehicle(vehicle);
     }
     base.OnDestroy();
 }
    // Start is called before the first frame update
    public override void Setup(PhysXBody attachedRigidBody, uint vehicleId)
    {
        IntPtr geom = PhysXLib.CreateSphereGeometry(radius);

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

        base.Setup(attachedRigidBody, vehicleId);
    }
    public void GetWorldPose(out Vector3 position, out Quaternion rotation)
    {
        position = transform.TransformPoint(wheelCentre);
        float rotationAngle = PhysXLib.GetWheelRotationAngle(vehicle, wheelNum) * Mathf.Rad2Deg;

        rotation = transform.rotation * Quaternion.AngleAxis(steerAngle, Vector3.up) * Quaternion.AngleAxis(rotationAngle, Vector3.right);
        //rotation = transform.rotation * Quaternion.AngleAxis(steerAngle, transform.up);
    }
Exemplo n.º 7
0
 public Vector3 ClosestPoint(Vector3 position)
 {
     physXPoint.FromVector(position);
     if (PhysXLib.GetClosestPointOnShape(shape, physXPoint, physXClosestPoint) == 0)
     {
         return(position);
     }
     return(physXClosestPoint.ToVector());
 }
Exemplo n.º 8
0
 void Setup()
 {
     scene = PhysXLib.CreateScene(new PhysXVec3(Physics.gravity));
     foreach (PhysXBody body in preRegisteredBodies)
     {
         AddActor(body);
     }
     preRegisteredBodies.Clear();
 }
Exemplo n.º 9
0
    public void AddTorque(Vector3 force, ForceMode forceMode)
    {
        int forceModeInt = (int)forceMode;

        if (forceMode == ForceMode.Acceleration)
        {
            forceModeInt = 3;
        }

        physXForce.FromVector(force);
        PhysXLib.AddTorque(physXBody, physXForce, forceModeInt);
    }
Exemplo n.º 10
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;
        }
    }
Exemplo n.º 11
0
    public void AddForceAtPosition(Vector3 force, Vector3 position, ForceMode forceMode)
    {
        int forceModeInt = (int)forceMode;

        if (forceMode == ForceMode.Acceleration)
        {
            forceModeInt = 3;
        }

        physXForce.FromVector(force);
        physXForcePos.FromVector(position);
        PhysXLib.AddForceAtPosition(physXBody, physXForce, physXForcePos, forceModeInt);
    }
Exemplo n.º 12
0
 public void AddActor(PhysXBody body)
 {
     if (scene == IntPtr.Zero)
     {
         preRegisteredBodies.Add(body);
     }
     else
     {
         body.Setup();
         bodies.Add(body.physXBody.ToInt64(), body);
         PhysXLib.AddActorToScene(scene, body.physXBody);
         body.PostSceneInsertionSetup();
     }
 }
Exemplo n.º 13
0
    public void Simulate()
    {
        if (doPhysics)
        {
            PhysXLib.StepPhysics(scene, Time.fixedDeltaTime, scratchMem, scratchKilobytes * 1024);

            foreach (PhysXBody body in bodies.Values)
            {
                body.UpdatePositionAndVelocity();
            }

            collisionCallbackMarker.Begin();
            foreach (PhysXCollision collision in PhysXSceneManager.ongoingCollisions)
            {
                collision.PopulateWithUnityObjects(bodies);
                PhysXBody body = null;
                if (bodies.TryGetValue(collision.self.ToInt64(), out body))
                {
                    try {
                        body.FireCollisionEvents(collision);
                    }
                    catch (Exception e) {
                        Debug.LogError("Exception: " + e.Message + "\n" + e.StackTrace);
                    }
                }
                PhysXCollision.ReleaseCollision(collision);
            }
            PhysXSceneManager.ongoingCollisions.Clear();

            foreach (PhysXTrigger trigger in PhysXSceneManager.ongoingTriggers)
            {
                trigger.PopulateWithUnityObjects(bodies);
                PhysXBody body = null;
                if (bodies.TryGetValue(trigger.self.ToInt64(), out body))
                {
                    try {
                        body.FireTriggerEvents(trigger);
                    }
                    catch (Exception e) {
                        Debug.LogError("Exception: " + e.Message + "\n" + e.StackTrace);
                    }
                }
                PhysXTrigger.ReleaseTrigger(trigger);
            }
            PhysXSceneManager.ongoingTriggers.Clear();
            collisionCallbackMarker.End();

            PhysXLib.StepGhostPhysics(scene, Time.fixedDeltaTime, scratchMem, scratchKilobytes * 1024);
        }
    }
Exemplo n.º 14
0
    public virtual void Setup()
    {
        physXPosition.FromVector(transform.position);
        physXRotation.FromQuaternion(transform.rotation);
        IntPtr physXTransform = PhysXLib.CreateTransform(physXPosition, physXRotation);

        _position = transform.position;
        _rotation = transform.rotation;
        physXBody = PhysXLib.CreateStaticRigidBody(physXTransform);

        PhysXCollider[] colliders = GetComponentsInChildren <PhysXCollider>(true);

        foreach (PhysXCollider collider in colliders)
        {
            collider.Setup(this, 0);
        }
    }
Exemplo n.º 15
0
    public override void Setup()
    {
        physXPosition.FromVector(transform.position);
        physXRotation.FromQuaternion(transform.rotation);
        IntPtr physXTransform = PhysXLib.CreateTransform(physXPosition, physXRotation);

        _position = transform.position;
        _rotation = transform.rotation;
        physXBody = PhysXLib.CreateDynamicRigidBody(physXTransform);

        // PhysXLib.RegisterCollisionEnterCallback(ProcessCollisionEnterEvents, physXDynamicRigidBody);
        // PhysXLib.RegisterCollisionStayCallback(ProcessCollisionStayEvents, physXDynamicRigidBody);
        // PhysXLib.RegisterCollisionExitCallback(ProcessCollisionExitEvents, physXDynamicRigidBody);

        PhysXLib.SetRigidBodyFlag(physXBody, PhysXLib.PhysXRigidBodyFlag.eKINEMATIC, kinematic);
        if (kinematic)
        {
            PhysXLib.SetRigidBodyDominanceGroup(physXBody, 1);
        }

        PhysXLib.SetRigidBodyMaxDepenetrationVelocity(physXBody, Physics.defaultMaxDepenetrationVelocity);
        PhysXLib.SetRigidBodyMaxLinearVelocity(physXBody, maxVelocity);

        wheels = new List <PhysXWheelCollider>(GetComponentsInChildren <PhysXWheelCollider>(true));
        PhysXCollider[] colliders = GetComponentsInChildren <PhysXCollider>(true);

        if (wheels.Count > 0)
        {
            vehicleId = currentVehicleId;
            currentVehicleId++;
        }

        foreach (PhysXCollider collider in colliders)
        {
            collider.Setup(this, vehicleId);
        }
        //Debug.Log(vehicleId);

        PhysXLib.SetRigidBodyMassAndInertia(physXBody, mass, new PhysXVec3(Vector3.zero));
        PhysXLib.SetRigidBodyDamping(physXBody, linearDamping, angularDamping);
    }
Exemplo n.º 16
0
    // Start is called before the first frame update
    public virtual void Setup(PhysXBody attachedRigidBody, uint vehicleId)
    {
        this.attachedRigidBody = attachedRigidBody as PhysXRigidBody;
        Transform bodyParent = attachedRigidBody.transform;

        PhysXVec3 position = new PhysXVec3(bodyParent.InverseTransformPoint(transform.TransformPoint(offset)));
        PhysXQuat rotation = new PhysXQuat(transform.rotation * Quaternion.Inverse(bodyParent.rotation));

        IntPtr localTransform = PhysXLib.CreateTransform(position, rotation);

        PhysXLib.SetShapeLocalTransform(shape, localTransform);

        PhysXLib.SetShapeSimulationFlag(shape, !trigger);
        PhysXLib.SetShapeSceneQueryFlag(shape, !trigger);
        PhysXLib.SetShapeTriggerFlag(shape, trigger);

        PhysXLib.CollisionEvent collisionEventFlags = attachedRigidBody.collisionEventFlags;
        PhysXLib.SetCollisionFilterData(shape, (UInt32)ownLayers, (UInt32)collisionLayers, (UInt32)collisionEventFlags, vehicleId);
        PhysXLib.SetQueryFilterData(shape, (UInt32)ownLayers, 0, 0, vehicleId);
        shapeNum = attachedRigidBody.AddCollider(this);
    }
Exemplo n.º 17
0
    public override void UpdatePositionAndVelocity()
    {
        PhysXLib.GetPosition(physXBody, physXPosition);

        PhysXLib.GetRotation(physXBody, physXRotation);

        physXPosition.ToVector(ref _position);
        physXRotation.ToQuaternion(ref _rotation);

        transform.SetPositionAndRotation(_position, _rotation);

        PhysXLib.GetLinearVelocity(physXBody, physXVelocity);
        physXVelocity.ToVector(ref _velocity);

        PhysXLib.GetAngularVelocity(physXBody, physXAngularVelocity);
        physXAngularVelocity.ToVector(ref _angularVelocity);

        foreach (PhysXWheelCollider wheel in wheels)
        {
            wheel.UpdateData();
        }
    }
Exemplo n.º 18
0
    // 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);
        }
    }
Exemplo n.º 19
0
 public int AddCollider(PhysXCollider collider)
 {
     colliders.Add(collider.shape.ToInt64(), collider);
     return(PhysXLib.AttachShapeToRigidBody(collider.shape, physXBody));
 }
Exemplo n.º 20
0
 protected virtual void OnDestroy()
 {
     PhysXLib.DestroyActor(physXBody);
     sceneManager.RemoveActor(this);
 }
Exemplo n.º 21
0
 public static bool FireRaycast(PhysXVec3 origin, PhysXVec3 direction, float distance, IntPtr raycastHit)
 {
     return(PhysXLib.FireRaycast(scene, origin, direction, distance, raycastHit));
 }
Exemplo n.º 22
0
 public void AddGhostImpulse(Vector3 force, Vector3 position)
 {
     physXForce.FromVector(force);
     physXForcePos.FromVector(position);
     PhysXLib.AddForceAtPosition(ghostBody, physXForce, physXForcePos, (int)ForceMode.Impulse);
 }
Exemplo n.º 23
0
 public static bool FireRaycastFiltered(PhysXVec3 origin, PhysXVec3 direction, float distance, IntPtr raycastHit, uint w0, uint w1, uint w2, uint w3)
 {
     return(PhysXLib.FireRaycastFiltered(scene, origin, direction, distance, raycastHit, w0, w1, w2, w3));
 }
Exemplo n.º 24
0
 void OnSceneUnloaded(Scene s)
 {
     PhysXLib.DestroyScene(scene);
     scene = IntPtr.Zero;
     bodies.Clear();
 }
Exemplo n.º 25
0
    public void SetVehicle(IntPtr vehicle)
    {
        this.vehicle = vehicle;

        wheelSimData = PhysXLib.GetWheelSimData(vehicle);
    }
Exemplo n.º 26
0
 public void UpdateData()
 {
     wheelCentre = Vector3.up * (PhysXLib.GetSuspensionCompression(vehicle, wheelNum) - suspensionDistance * suspensionSpringTargetPosition);
 }