Browse Source

Remove special case wall collisions, replace with generated objects

Fraser Graham 2 years ago
parent
commit
ef33a17b6a
2 changed files with 27 additions and 31 deletions
  1. 26
    9
      obstacle.go
  2. 1
    22
      robot.go

+ 26
- 9
obstacle.go View File

@@ -49,24 +49,41 @@ func (o Obstacle) distance_from_point(p v.Point2d) float64 {
49 49
 	return dist
50 50
 }
51 51
 
52
+func obstacleFromValues(x, y, w, h float64, hp int) (o Obstacle) {
53
+	return Obstacle{
54
+		Bounds: v.AABB2d{
55
+			A: v.Point2d{X: x, Y: y},
56
+			B: v.Point2d{X: x + w, Y: y + h},
57
+		},
58
+		Hp: hp,
59
+	}
60
+}
61
+
52 62
 // GenerateObstacles returns a slice of (count) obstacles within a region
53 63
 // bounded by width, height.
54 64
 func GenerateObstacles(count int, width, height float64) []Obstacle {
55 65
 	out := []Obstacle{}
66
+
67
+	// Create obstacles that cover the edges of the world, so we don't need to
68
+	// special case collision at the edges
69
+	// left wall
70
+	out = append(out, obstacleFromValues(0, 5, 5, height-10, 0))
71
+	// // right wall
72
+	out = append(out, obstacleFromValues(width-5, 5, 5, height-10, 0))
73
+	// // top wall
74
+	out = append(out, obstacleFromValues(0, height-5, width, 5, 0))
75
+	// bottom wall
76
+	out = append(out, obstacleFromValues(0, 0, width, 5, 0))
77
+
78
+	// Now create N random objects somewhere in the world
56 79
 	for i := 0; i < count; i++ {
57 80
 		x := rand.Float64() * (width - (width / 10))
58 81
 		y := rand.Float64() * (height - (height / 10))
59
-		w := rand.Float64() * (width / 10)
60
-		h := rand.Float64() * (height / 10)
82
+		w := rand.Float64()*(width/10) + 20
83
+		h := rand.Float64()*(height/10) + 20
61 84
 		out = append(
62 85
 			out,
63
-			Obstacle{
64
-				Bounds: v.AABB2d{
65
-					A: v.Point2d{X: x, Y: y},
66
-					B: v.Point2d{X: 20 + x + w, Y: 20 + y + h},
67
-				},
68
-				Hp: 10000,
69
-			})
86
+			obstacleFromValues(x, y, w, h, 10000))
70 87
 	}
71 88
 	return out
72 89
 }

+ 1
- 22
robot.go View File

@@ -195,29 +195,8 @@ func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *R
195 195
 	botSize := 5.0
196 196
 	botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize)
197 197
 
198
-	bounds := []Obstacle{
199
-		Obstacle{
200
-			Bounds: v.AABB2d{A: v.Point2d{0, 0}, B: v.Point2d{0, g.width}},
201
-			Hp:     0,
202
-		},
203
-		Obstacle{
204
-			Bounds: v.AABB2d{A: v.Point2d{0, 0}, B: v.Point2d{0, g.height}},
205
-			Hp:     0,
206
-		},
207
-		Obstacle{
208
-			Bounds: v.AABB2d{A: v.Point2d{g.width, g.height}, B: v.Point2d{0, g.height}},
209
-			Hp:     0,
210
-		},
211
-		Obstacle{
212
-			Bounds: v.AABB2d{A: v.Point2d{g.width, g.height}, B: v.Point2d{g.width, 0}},
213
-			Hp:     0,
214
-		},
215
-	}
216
-
217
-	obstacles := append(g.obstacles, bounds...)
218
-
219 198
 	// Check Obstacles
220
-	for _, obj := range obstacles {
199
+	for _, obj := range g.obstacles {
221 200
 		// collision due to motion:
222 201
 		collision, move_collision, translation := v.PolyPolyIntersection(
223 202
 			botPolygon, probe, obj.Bounds.ToPolygon())

Loading…
Cancel
Save