Beispiel #1
0
        public void DetectTouchedBlocks(ICollisionEngineContext context, IMovableObject checkedObject)
        {
            checkedObject.TouchedObjects.Clear();

            var areaToCheck = IntRectangle.FromPositionAndSize(checkedObject.Position, checkedObject.Size).Extend(Constants.BlockSize);

            var checkedObjBounds = checkedObject.Bounds;

            foreach (var obj in context.GetCollidableObjectsWithin(areaToCheck))
            {
                if (obj == checkedObject)
                {
                    continue;
                }

                if (obj.IsSolid)
                {
                    var objBounds = CoordinateSystem.Denormalize(obj.Bounds, checkedObjBounds);

                    if (checkedObjBounds.TryGetTouchedSide(objBounds, out var touchedSide))
                    {
                        checkedObject.TouchedObjects[touchedSide].Add(obj);
                    }
                }
            }
        }
Beispiel #2
0
        public void MoveUp(ICollisionEngineContext context, IMovableObject movingObj, int distance)
        {
            var movingObjBounds = movingObj.Bounds;

            var sweptArea = new IntRectangle(movingObjBounds.Left, movingObjBounds.Top - distance, movingObjBounds.Front, movingObjBounds.Width, distance, movingObjBounds.Depth);

            var areaToCheck = sweptArea.Extend(Constants.BlockSize);

            foreach (var obj in context.GetCollidableObjectsWithin(areaToCheck))
            {
                if (obj == movingObj)
                {
                    continue;
                }

                var objBounds = CoordinateSystem.Denormalize(obj.Bounds, sweptArea);

                if (obj.IsSolid && sweptArea.Intersects(objBounds))
                {
                    sweptArea = new IntRectangle(sweptArea.Left, objBounds.Bottom, sweptArea.Front, sweptArea.Width, sweptArea.Bottom - objBounds.Bottom, sweptArea.Depth);
                }
            }

            movingObj.Position = movingObj.Position.MoveUp(sweptArea.Height);
        }