I am toying around with a Boogaloopers clone (http://www.youtube.com/watch?v=zuaRi_fhdlQ).
I have a small problem with the lassoing.
Here’s my current naive algorithm. The problem with this is that the player can swipe a U-shaped pattern with the mouse, and it is detected as a polygon, since the isPointInPolygon
-method connects the first and last points together. I wonder if there’s a simple way to fix this?
private void collisionDetection() {
Player p = Player.getPlayer();
Array<Beam> beams = p.getBeams();
if (beams.size < 3)
return;
for (int i = 0; i < beams.size; i++) {
Beam b1 = beams.get(i);
for (int k = i + 1; k < beams.size; k++) {
Beam b2 = beams.get(k);
// Don't do collision detection with adjacent beams
if (p.areConnected(b1, b2))
continue;
if (Intersector.intersectSegments(b1.getStart(), b1.getEnd(), b2.getStart(), b2.getEnd(), null)) {
Array<Vector2> path = p.getPath();
if (path.size < 3)
continue;
boolean destroyed = false;
// Destroy any monsters inside the polygon
for (int j = 0; j < entities.size; j++) {
Entity e = entities.get(j);
if (e instanceof Player)
continue;
if (Intersector.isPointInPolygon(path, e.getPosition())) {
destroyed = true;
e.destroy();
}
}
if (destroyed) {
SFX.play("sfx-destroy");
}
return;
}
}
}
}