Skip to content

Commit 263c6a1

Browse files
committed
Fix walking path calc.
1 parent b13bf04 commit 263c6a1

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

blade-engine/src/com/bladecoder/engine/model/ObstacleActor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
******************************************************************************/
1616
package com.bladecoder.engine.model;
1717

18+
import com.badlogic.gdx.utils.Json;
19+
import com.bladecoder.engine.util.PolygonUtils;
20+
import com.bladecoder.engine.util.SerializationHelper;
21+
import com.bladecoder.engine.util.SerializationHelper.Mode;
1822

1923
/**
2024
* An Obstacle actor is used to restrict the walk zone in the scene
@@ -51,4 +55,14 @@ public void setPosition(float x, float y) {
5155
scene.getPolygonalNavGraph().addDinamicObstacle(bbox);
5256
}
5357
}
58+
59+
@Override
60+
public void write(Json json) {
61+
if (SerializationHelper.getInstance().getMode() == Mode.MODEL) {
62+
PolygonUtils.ensureClockWise(bbox.getVertices(), 0, bbox.getVertices().length);
63+
bbox.dirty();
64+
}
65+
66+
super.write(json);
67+
}
5468
}

blade-engine/src/com/bladecoder/engine/polygonalpathfinder/PolygonalNavGraph.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,12 @@ public boolean removeDinamicObstacle(Polygon poly) {
320320

321321
@Override
322322
public void write(Json json) {
323+
PolygonUtils.ensureClockWise(walkZone.getVertices(), 0, walkZone.getVertices().length);
324+
walkZone.dirty();
325+
323326
Polygon p = new Polygon(walkZone.getVertices());
324327
p.setPosition(walkZone.getX() / walkZone.getScaleX(), walkZone.getY() / walkZone.getScaleY());
328+
325329
json.writeValue("walkZone", p);
326330
}
327331

blade-engine/src/com/bladecoder/engine/util/PolygonUtils.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
******************************************************************************/
1616
package com.bladecoder.engine.util;
1717

18+
import com.badlogic.gdx.math.GeometryUtils;
1819
import com.badlogic.gdx.math.Intersector;
1920
import com.badlogic.gdx.math.Polygon;
2021
import com.badlogic.gdx.math.Vector2;
@@ -254,7 +255,7 @@ public static boolean inLineOfSight(Vector2 p1, Vector2 p2, Polygon polygon, boo
254255
return obstacle?!result:result;
255256
}
256257

257-
private static float TOLERANCE_LINE_SEGMENTS_CROSS = 0.1f;
258+
private static float TOLERANCE_LINE_SEGMENTS_CROSS = 0.01f;
258259

259260
public static boolean lineSegmentsCross(float ax, float ay, float bx,
260261
float by, float cx, float cy, float dx, float dy) {
@@ -277,4 +278,18 @@ public static boolean lineSegmentsCross(float ax, float ay, float bx,
277278

278279
return (r > TOLERANCE_LINE_SEGMENTS_CROSS && r < 1 - TOLERANCE_LINE_SEGMENTS_CROSS) && (s > TOLERANCE_LINE_SEGMENTS_CROSS && s < 1 - TOLERANCE_LINE_SEGMENTS_CROSS);
279280
}
281+
282+
static public void ensureClockWise (float[] polygon, int offset, int count) {
283+
if (GeometryUtils.isClockwise(polygon, offset, count)) return;
284+
int lastX = offset + count - 2;
285+
for (int i = offset, n = offset + count / 2; i < n; i += 2) {
286+
int other = lastX - i;
287+
float x = polygon[i];
288+
float y = polygon[i + 1];
289+
polygon[i] = polygon[other];
290+
polygon[i + 1] = polygon[other + 1];
291+
polygon[other] = x;
292+
polygon[other + 1] = y;
293+
}
294+
}
280295
}

0 commit comments

Comments
 (0)