From af418f1e6f617e4ca39abf7e142c27c8ea4e2e5c Mon Sep 17 00:00:00 2001 From: Fraser Graham Date: Sat, 26 Apr 2014 23:05:33 -0600 Subject: [PATCH] Fixing reversing and wall collisions --- robot.go | 60 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/robot.go b/robot.go index 20f71ee..928d764 100644 --- a/robot.go +++ b/robot.go @@ -186,7 +186,6 @@ type Instruction struct { // collided, if this happened. func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *Robot) { finalCollision := false - collision := false closest := math.Inf(1) var intersection *v.Point2d var finalRobot *Robot @@ -195,19 +194,29 @@ func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *R botSize := 5.0 botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize) - // Check Walls - r_walls := v.AABB2d{A: v.Point2d{X: botSize, Y: botSize}, B: v.Point2d{X: g.width - botSize, Y: g.height - botSize}} - collision, _, wallIntersect := v.RectIntersection(r_walls, r.Position, probe) - if collision && wallIntersect != nil { - finalCollision = true - if dist := r.Position.Sub(*wallIntersect).Mag(); dist < closest { - intersection = wallIntersect - closest = dist - } + bounds := []Obstacle{ + Obstacle{ + Bounds: v.AABB2d{A: v.Point2d{0, 0}, B: v.Point2d{0, g.width}}, + Hp: 0, + }, + Obstacle{ + Bounds: v.AABB2d{A: v.Point2d{0, 0}, B: v.Point2d{0, g.height}}, + Hp: 0, + }, + Obstacle{ + Bounds: v.AABB2d{A: v.Point2d{g.width, g.height}, B: v.Point2d{0, g.height}}, + Hp: 0, + }, + Obstacle{ + Bounds: v.AABB2d{A: v.Point2d{g.width, g.height}, B: v.Point2d{g.width, 0}}, + Hp: 0, + }, } + obstacles := append(g.obstacles, bounds...) + // Check Obstacles - for _, obj := range g.obstacles { + for _, obj := range obstacles { // collision due to motion: collision, move_collision, translation := v.PolyPolyIntersection( botPolygon, probe, obj.Bounds.ToPolygon()) @@ -262,7 +271,7 @@ func (r *Robot) Tick(g *Game) { r.Hit = false r.scan(g) - // Adjust Speed + // Cap Target Speed if r.TargetSpeed > r.Stats.Speed { r.TargetSpeed = r.Stats.Speed } @@ -271,19 +280,30 @@ func (r *Robot) Tick(g *Game) { r.TargetSpeed = -1.0 * r.Stats.Speed } - if r.Speed < r.TargetSpeed { + // Are we speeding up or slowing down? + increase := true + if float32(math.Abs(float64(r.Speed-r.TargetSpeed))) > v.Epsilon { + increase = false + } + + if increase { r.Speed += (r.Stats.Acceleration * r.Delta) + // Stop us from going too far if r.Speed > r.TargetSpeed { r.Speed = r.TargetSpeed } - } else if float32(math.Abs(float64(r.Speed-r.TargetSpeed))) > v.Epsilon { - r.Speed -= (r.Stats.Acceleration * r.Delta) - // Cap reverse to 1/2 speed - if r.Speed < (-0.5 * r.TargetSpeed) { - r.Speed = (-0.5 * r.TargetSpeed) - } } else { - r.Speed = r.TargetSpeed + r.Speed -= (r.Stats.Acceleration * r.Delta) + + // Dont go too far + if r.Speed < r.TargetSpeed { + r.Speed = r.TargetSpeed + } + + // Cap reverse at 1/4 top speed + if r.Speed < (-0.25 * r.Stats.Speed) { + r.Speed = (-0.25 * r.Stats.Speed) + } } // Adjust Heading