From ef33a17b6a4349786e1825a2b5dc88f918cfa362 Mon Sep 17 00:00:00 2001 From: Fraser Graham Date: Wed, 13 Jul 2016 12:16:10 -0600 Subject: [PATCH] Remove special case wall collisions, replace with generated objects --- obstacle.go | 35 ++++++++++++++++++++++++++--------- robot.go | 23 +---------------------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/obstacle.go b/obstacle.go index f188ee4..ad6c19a 100644 --- a/obstacle.go +++ b/obstacle.go @@ -49,24 +49,41 @@ func (o Obstacle) distance_from_point(p v.Point2d) float64 { return dist } +func obstacleFromValues(x, y, w, h float64, hp int) (o Obstacle) { + return Obstacle{ + Bounds: v.AABB2d{ + A: v.Point2d{X: x, Y: y}, + B: v.Point2d{X: x + w, Y: y + h}, + }, + Hp: hp, + } +} + // GenerateObstacles returns a slice of (count) obstacles within a region // bounded by width, height. func GenerateObstacles(count int, width, height float64) []Obstacle { out := []Obstacle{} + + // Create obstacles that cover the edges of the world, so we don't need to + // special case collision at the edges + // left wall + out = append(out, obstacleFromValues(0, 5, 5, height-10, 0)) + // // right wall + out = append(out, obstacleFromValues(width-5, 5, 5, height-10, 0)) + // // top wall + out = append(out, obstacleFromValues(0, height-5, width, 5, 0)) + // bottom wall + out = append(out, obstacleFromValues(0, 0, width, 5, 0)) + + // Now create N random objects somewhere in the world for i := 0; i < count; i++ { x := rand.Float64() * (width - (width / 10)) y := rand.Float64() * (height - (height / 10)) - w := rand.Float64() * (width / 10) - h := rand.Float64() * (height / 10) + w := rand.Float64()*(width/10) + 20 + h := rand.Float64()*(height/10) + 20 out = append( out, - Obstacle{ - Bounds: v.AABB2d{ - A: v.Point2d{X: x, Y: y}, - B: v.Point2d{X: 20 + x + w, Y: 20 + y + h}, - }, - Hp: 10000, - }) + obstacleFromValues(x, y, w, h, 10000)) } return out } diff --git a/robot.go b/robot.go index 2ddfd84..3497b69 100644 --- a/robot.go +++ b/robot.go @@ -195,29 +195,8 @@ func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *R botSize := 5.0 botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize) - 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 obstacles { + for _, obj := range g.obstacles { // collision due to motion: collision, move_collision, translation := v.PolyPolyIntersection( botPolygon, probe, obj.Bounds.ToPolygon())