Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved A* pathfinding #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions bin/pure_js_example/lib/hxDaedalus.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions src/hxDaedalus/ai/AStar.hx
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,21 @@ class AStar {

fromPoint.x = entryX[ curFace ];
fromPoint.y = entryY[ curFace ];
entryPoint.x = ( innerEdge.originVertex.pos.x + innerEdge.destinationVertex.pos.x ) / 2;
entryPoint.y = ( innerEdge.originVertex.pos.y + innerEdge.destinationVertex.pos.y ) / 2;

// entryPoint will be the direct point of intersection between fromPoint and toXY if the edge innerEdge
// intersects it
var vw1 : Point2D = innerEdge.originVertex.pos;
var vw2 : Point2D = innerEdge.destinationVertex.pos;
if (!Geom2D.intersections2segments(fromPoint.x, fromPoint.y, toX, toY, vw1.x, vw1.y, vw2.x, vw2.y, entryPoint)) {
// Recycle the entryPoint variable to create a Point2D(toX, toY)
entryPoint.x = toX;
entryPoint.y = toY;
var vst = vw1.distanceSquaredTo(fromPoint) + vw1.distanceSquaredTo(entryPoint);
var wst = vw2.distanceSquaredTo(fromPoint) + vw2.distanceSquaredTo(entryPoint);
entryPoint.x = vst <= wst ? vw1.x : vw2.x;
entryPoint.y = vst <= wst ? vw1.y : vw2.y;
}

distancePoint.x = entryPoint.x - toX;
distancePoint.y = entryPoint.y - toY;
h = distancePoint.length;
Expand Down
6 changes: 6 additions & 0 deletions src/hxDaedalus/data/math/Point2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ class Point2D{
var diffY : Float = y - p.y;
return Math.sqrt( diffX*diffX + diffY*diffY );
}

public function distanceSquaredTo( p: Point2D ): Float {
var diffX : Float = x - p.x;
var diffY : Float = y - p.y;
return diffX*diffX + diffY*diffY;
}
}