Fixed collision detection in probes
This was a problem before when the probe hit an object that was further than another object first. Now we traverse all objects all the time to find the closest one. \o/
This commit is contained in:
@@ -165,17 +165,28 @@ type Instruction struct {
|
||||
Scan *bool `json:"scan,omitempty"`
|
||||
}
|
||||
|
||||
func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d, *Robot) {
|
||||
// returns collision, the intersection point, and the robot with whom r has
|
||||
// collided, if this happened.
|
||||
func (r *Robot) checkCollisions(g *game, probe v.Vector2d) (bool, *v.Point2d, *Robot) {
|
||||
finalCollision := false
|
||||
collision := false
|
||||
intersection_point := v.Point2d{X: 0, Y: 0}
|
||||
bot_size := float32(5.0)
|
||||
bot_polygon := v.OrientedSquare(r.Position, r.Heading, bot_size)
|
||||
closest := float32(math.Inf(1))
|
||||
var intersection *v.Point2d
|
||||
var finalRobot *Robot
|
||||
|
||||
// TODO: this needs moved to the conf?
|
||||
botSize := float32(5.0)
|
||||
botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize)
|
||||
|
||||
// Check Walls
|
||||
r_walls := v.AABB2d{A: v.Point2d{X: bot_size, Y: bot_size}, B: v.Point2d{X: g.width - bot_size, Y: g.height - bot_size}}
|
||||
collision, _, pos := v.RectIntersection(r_walls, r.Position, move_vector)
|
||||
if collision {
|
||||
return collision, pos, nil
|
||||
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 = collision
|
||||
if dist := r.Position.Sub(*wallIntersect).Mag(); dist < closest {
|
||||
intersection = wallIntersect
|
||||
closest = dist
|
||||
}
|
||||
}
|
||||
|
||||
// Check Other Bots
|
||||
@@ -184,29 +195,47 @@ func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2
|
||||
if bot.Id == r.Id {
|
||||
continue
|
||||
}
|
||||
player_rect := v.OrientedSquare(bot.Position, bot.Heading, bot_size)
|
||||
player_rect := v.OrientedSquare(bot.Position, bot.Heading, botSize)
|
||||
collision, move_collision, translation := v.PolyPolyIntersection(
|
||||
bot_polygon, move_vector, player_rect)
|
||||
botPolygon, probe, player_rect)
|
||||
if collision || move_collision {
|
||||
return true, r.Position.Add(move_vector).Add(translation.Scale(1.1)), bot
|
||||
finalCollision = collision
|
||||
p := r.Position.Add(probe).Add(translation.Scale(1.2))
|
||||
if dist := r.Position.Sub(p).Mag(); dist < closest {
|
||||
intersection = &p
|
||||
closest = dist
|
||||
finalRobot = bot
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check Obstacles
|
||||
for _, obj := range g.obstacles {
|
||||
// collision due to motion:
|
||||
collision, move_collision, translation := v.PolyPolyIntersection(
|
||||
botPolygon, probe, obj.Bounds.ToPolygon())
|
||||
|
||||
if collision || move_collision {
|
||||
finalCollision = collision
|
||||
p := r.Position.Add(probe).Add(translation.Scale(1.1))
|
||||
if dist := r.Position.Sub(p).Mag(); dist < closest {
|
||||
intersection = &p
|
||||
closest = dist
|
||||
}
|
||||
}
|
||||
|
||||
// collision due to probe
|
||||
collision, _, wallIntersect := v.RectIntersection(obj.Bounds, r.Position, probe)
|
||||
if collision && wallIntersect != nil {
|
||||
finalCollision = collision
|
||||
if dist := r.Position.Sub(*wallIntersect).Mag(); dist < closest {
|
||||
intersection = wallIntersect
|
||||
closest = dist
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check Obstacles
|
||||
for _, obj := range g.obstacles {
|
||||
collision, move_collision, translation := v.PolyPolyIntersection(
|
||||
bot_polygon, move_vector, obj.Bounds.ToPolygon())
|
||||
|
||||
if collision || move_collision {
|
||||
// log.Printf(" COLLISION: %v %v %v\n", collision, move_collition, translation)
|
||||
// log.Printf(" DETAILS: %v %v\n", r.Position, move_vector)
|
||||
// log.Printf(" INPUT: %v %v %v\n", bot_polygon, obj.Bounds.ToPolygon(), obj.Bounds)
|
||||
return true, r.Position.Add(move_vector).Add(translation.Scale(1.1)), nil
|
||||
}
|
||||
}
|
||||
|
||||
return collision, intersection_point, nil
|
||||
return finalCollision, intersection, finalRobot
|
||||
}
|
||||
|
||||
func (r *Robot) Tick(g *game) {
|
||||
@@ -287,8 +316,8 @@ func (r *Robot) Tick(g *game) {
|
||||
}
|
||||
}
|
||||
|
||||
if r.Position != intersection_point {
|
||||
r.Position = intersection_point
|
||||
if r.Position != *intersection_point {
|
||||
r.Position = *intersection_point
|
||||
}
|
||||
|
||||
r.Health -= dmg
|
||||
@@ -343,7 +372,7 @@ func (r *Robot) Tick(g *game) {
|
||||
probe_vector := r.Probe.Sub(r.Position)
|
||||
coll, pos, _ := r.checkCollisions(g, probe_vector)
|
||||
if coll {
|
||||
r.ProbeResult = &pos
|
||||
r.ProbeResult = pos
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user