Fixing reversing and wall collisions
This commit is contained in:
parent
06ce7eefc8
commit
af418f1e6f
60
robot.go
60
robot.go
@ -186,7 +186,6 @@ type Instruction struct {
|
|||||||
// collided, if this happened.
|
// collided, if this happened.
|
||||||
func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *Robot) {
|
func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *Robot) {
|
||||||
finalCollision := false
|
finalCollision := false
|
||||||
collision := false
|
|
||||||
closest := math.Inf(1)
|
closest := math.Inf(1)
|
||||||
var intersection *v.Point2d
|
var intersection *v.Point2d
|
||||||
var finalRobot *Robot
|
var finalRobot *Robot
|
||||||
@ -195,19 +194,29 @@ func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *R
|
|||||||
botSize := 5.0
|
botSize := 5.0
|
||||||
botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize)
|
botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize)
|
||||||
|
|
||||||
// Check Walls
|
bounds := []Obstacle{
|
||||||
r_walls := v.AABB2d{A: v.Point2d{X: botSize, Y: botSize}, B: v.Point2d{X: g.width - botSize, Y: g.height - botSize}}
|
Obstacle{
|
||||||
collision, _, wallIntersect := v.RectIntersection(r_walls, r.Position, probe)
|
Bounds: v.AABB2d{A: v.Point2d{0, 0}, B: v.Point2d{0, g.width}},
|
||||||
if collision && wallIntersect != nil {
|
Hp: 0,
|
||||||
finalCollision = true
|
},
|
||||||
if dist := r.Position.Sub(*wallIntersect).Mag(); dist < closest {
|
Obstacle{
|
||||||
intersection = wallIntersect
|
Bounds: v.AABB2d{A: v.Point2d{0, 0}, B: v.Point2d{0, g.height}},
|
||||||
closest = dist
|
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
|
// Check Obstacles
|
||||||
for _, obj := range g.obstacles {
|
for _, obj := range obstacles {
|
||||||
// collision due to motion:
|
// collision due to motion:
|
||||||
collision, move_collision, translation := v.PolyPolyIntersection(
|
collision, move_collision, translation := v.PolyPolyIntersection(
|
||||||
botPolygon, probe, obj.Bounds.ToPolygon())
|
botPolygon, probe, obj.Bounds.ToPolygon())
|
||||||
@ -262,7 +271,7 @@ func (r *Robot) Tick(g *Game) {
|
|||||||
r.Hit = false
|
r.Hit = false
|
||||||
r.scan(g)
|
r.scan(g)
|
||||||
|
|
||||||
// Adjust Speed
|
// Cap Target Speed
|
||||||
if r.TargetSpeed > r.Stats.Speed {
|
if r.TargetSpeed > r.Stats.Speed {
|
||||||
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
|
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)
|
r.Speed += (r.Stats.Acceleration * r.Delta)
|
||||||
|
// Stop us from going too far
|
||||||
if r.Speed > r.TargetSpeed {
|
if r.Speed > r.TargetSpeed {
|
||||||
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 {
|
} 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
|
// Adjust Heading
|
||||||
|
Loading…
Reference in New Issue
Block a user