fix: Pathfinding cost

This commit is contained in:
Marcin Czerniak 2021-05-09 19:43:50 +02:00
parent 6ea38b9584
commit c31c838e55
2 changed files with 16 additions and 13 deletions

View File

@ -34,7 +34,7 @@ class Pathfinding {
for (const successor of this.successor(elem)) {
if (!Pathfinding.isNodeInArr(successor, explored) && !Pathfinding.isNodeInArr(successor, fringe)) {
Pathfinding.insertWithPriority(successor, fringe);
Pathfinding.insertWithPriority(successor, fringe, expectedNode);
} else if (Pathfinding.isNodeInArr(successor, fringe)) {
const index = Pathfinding.findIndefOf(successor, fringe);
fringe.splice(index, 1, successor);
@ -51,10 +51,9 @@ class Pathfinding {
return arr.findIndex(n => Pathfinding.reached(n.state, node.state));
}
static insertWithPriority(node, arr) {
static insertWithPriority(node, arr, destination) {
for (const [i, n] of arr.entries()) {
if (node.totalCost < n.totalCost) {
console.log('Inserted inside');
arr.splice(i, 0, node);
return;
}
@ -65,7 +64,7 @@ class Pathfinding {
static successor(node) {
const list = [];
const grid = Grid.instance.getGrid();
let actionCost = CostMap.instance.getCostAt(node.state.position.x, node.state.position.y);
let actionCost = CostMap.instance.getCostAt(node.state.position.x, node.state.position.y) + 1;
list.push({
state: { ...node.state, rotation: (node.state.rotation + 270) % 360 },
@ -86,12 +85,12 @@ class Pathfinding {
const prediction = { x: node.state.position.x + direction.x, y: node.state.position.y + direction.y }
if (grid[prediction.x] && grid[prediction.x][prediction.y] == GRID_FIELD_TYPE.PATH) {
actionCost = CostMap.instance.getCostAt(prediction.x, prediction.y);
actionCost = CostMap.instance.getCostAt(prediction.x, prediction.y) + 0.1;
list.push({
state: { ...node.state, position: prediction },
action: PATHFINDING_ACTION.MOVE,
actionCost,
actionCost: actionCost + 1,
totalCost: Math.round((node.totalCost + actionCost) * 100) / 100 || actionCost,
parent: node
});
@ -113,7 +112,7 @@ class Pathfinding {
return result;
}
static getPriority(node, expectedNode) {
static getManhattanDistance(node, expectedNode) {
return Math.abs(node.state.position.x - expectedNode.state.position.x) + Math.abs(node.state.position.y - expectedNode.state.position.y);
}

View File

@ -49,10 +49,10 @@ class AgentController {
if (nearestPoint) {
if (nearestPoint.action == PATHFINDING_ACTION.MOVE) {
await this.moveInLine(nearestPoint.state.position);
await this.moveInLine(nearestPoint.state.position, nearestPoint.actionCost);
}
if (nearestPoint.action == PATHFINDING_ACTION.ROTATE) {
await this.rotate({ z: nearestPoint.state.rotation });
await this.rotate({ z: nearestPoint.state.rotation }, nearestPoint.actionCost);
}
nearestPoint = this.currentPath[++this.currentPathIndex];
@ -77,10 +77,12 @@ class AgentController {
*
* @param {{ x: number, y: number }} position
*/
moveInLine(position) {
moveInLine(position, actionCost) {
return new Promise((resolve, reject) => {
const cycle = () => {
const { speed } = this;
let { speed } = this;
speed *= (actionCost - 1) * 4 + 1;
this.currentAction = this.actions.IN_MOVE;
this.position = {
x: this.position.x + (Math.sign(position.x - this.position.x)/speed),
@ -135,7 +137,7 @@ class AgentController {
* @param {{ z: number}} rotation
* @returns
*/
rotate(rotation) {
rotate(rotation, actionCost) {
return new Promise((resolve, reject) => {
if (this.rotation.z == rotation.z) {
resolve();
@ -155,8 +157,10 @@ class AgentController {
const newZRotation = rotation.z;
const clampRotation = (rot) => (rot + 360) % 360;
let { rotationSpeed } = this;
rotationSpeed *= clamp(2 - actionCost, 0.1, 1);
this.rotation = { z: clampRotation(oldZRotation + (this.rotationSpeed * sign)) };
this.rotation = { z: clampRotation(oldZRotation + (rotationSpeed * sign)) };
if (Math.abs(oldZRotation - newZRotation) < 5) {
resolve();