protected float? SenseGround() { var r = new Rect(x + vx, y + vy + Height, Width, SLOPE_THRESHHOLD); var x1 = x + vx; var x2 = x1 + Width; var bestIntercept = float.MaxValue; groundHeight = null; Action<Line> cb = (line) => { /* * I think what we want here is to track, for each direction, * the surface that penetrates the entity's hotspot the most. */ var intercept = float.MaxValue; if ((x1 > line.A.X || x1 > line.B.X) && (x1 < line.A.X || x1 < line.B.X) ) { var ay1 = line.atX(x1); intercept = ay1; } var ay2 = line.atX(x2); if ((x2 > line.A.X || x2 > line.B.X) && (x2 < line.A.X || x2 < line.B.X) && ay2 < intercept ) { intercept = ay2; } if (intercept < bestIntercept) { bestIntercept = intercept; } }; engine.obstructionTiles.Test(r, cb); if (bestIntercept < float.MaxValue) { groundHeight = bestIntercept; } return groundHeight; }
protected void DoCollision() { var limit = 4; do { var r = new Rect(x + vx, y + vy, Width, Height); var cr = engine.obstructionTiles.TestSAT(r); if (!cr.HasValue) { break; } var mtv = cr.Value.mtv; var velocity = new Vector2(vx, vy); velocity += mtv; vx = velocity.X; vy = velocity.Y; --limit; } while (limit > 0); var count = 4 - limit; if (count > 1) { Console.WriteLine("Resolved in {0} steps", count); } }