Esempio n. 1
0
    /// <summary>
    /// Simulate physics of objects in room for dT.
    /// </summary>
    public static void Run(PhysicsSettings settings, Room room, float dT)
    {
        // fetch buffers for the physics job
        var battleObjectCount = room.AllObjects.Count;
        var job = new PhysicsJob()
        {
            dT = dT,

            // objects buffer
            objectCount   = battleObjectCount,
            positions     = new NativeArray <float2>(battleObjectCount, Allocator.TempJob),
            origPositions = new NativeArray <float2>(battleObjectCount, Allocator.TempJob),
            velocities    = new NativeArray <float2>(battleObjectCount, Allocator.TempJob),
            properties    = new NativeArray <ColliderProperties>(battleObjectCount, Allocator.TempJob),

            // output collision events buffer
            collisionEvents = new NativeList <int2>(Allocator.TempJob),

            // load settings into the job
            PhysicsPassesPerFrame = settings.Passes,
            BounceAbsorb          = settings.BounceAbsorb,
            VelocityDragConstant  = settings.VelocityDragConstant,
            VelocityDragRelative  = pow(settings.VelocityDragRelative, 1f / settings.Passes),

            // get world boundaries
            LeftEdge   = -room.Width / 2,
            RightEdge  = room.Width / 2,
            TopEdge    = -room.Height / 2,
            BottomEdge = room.Height / 2,
        };

        // load objects into the buffer
        var battleObjects = room.AllObjects;

        for (int i = 0; i < battleObjectCount; i++)
        {
            var obj = battleObjects[i];
            job.positions[i]  = obj.Position;
            job.velocities[i] = obj.Velocity;
            job.properties[i] = new ColliderProperties(obj);
        }

        // run it!
        job.Run();

        // apply results to the game. Skip NaNs if any.
        for (int i = 0; i < battleObjectCount; i++)
        {
            var newPosition = job.positions[i];
            if (any(isnan(newPosition)))
            {
                continue;
            }

            var newVelocity = job.velocities[i];
            if (any(isnan(newVelocity)))
            {
                continue;
            }

            var obj = battleObjects[i];
            if (!obj.Settings.IsStatic)
            {
                obj.Position = newPosition;
                obj.Velocity = newVelocity;
            }
        }

        // dispatch collected collision events
        foreach (var collisionEvent in job.collisionEvents)
        {
            if (collisionEvent.y < 0)
            {
                battleObjects[collisionEvent.x].OnCollisionWithWall();
            }
            else
            {
                battleObjects[collisionEvent.x].OnCollisionWith(battleObjects[collisionEvent.y]);
                battleObjects[collisionEvent.y].OnCollisionWith(battleObjects[collisionEvent.x]);
            }
        }

        // return buffer memory
        job.collisionEvents.Dispose();
        job.positions.Dispose();
        job.origPositions.Dispose();
        job.velocities.Dispose();
        job.properties.Dispose();
    }
Esempio n. 2
0
 private void TickCollision(float dT)
 {
     PhysicsJob.Run(PhysicsSettings.Instance, this, dT);
 }