我正在開發像http://harmmade.com/vectorracer/這樣的賽車遊戲,並且我已經實現了用於AI玩家的A *算法。該算法對於1平鋪運動效果很好,但我不希望AI玩家一次只移動1個平鋪(僅使用它們的相鄰點),我需要它們在加速和減速時能夠加速和減速在轉彎時關閉。他們的下一個位置應該取決於他們之前的位置,就像Vector Racer一樣。考慮速度的A *算法
public boolean createRoute() {
// The list where the points will be added in reverse order (from finish_point)
ArrayList<Track_Point> path = new ArrayList<>();
// The list where the unchecked points will be stored
ArrayList<Track_Point> open = new ArrayList<>();
// The list where the checked points will be stored
ArrayList<Track_Point> closed = new ArrayList<>();
// The starting point is always added as the first point to be checked
open.add(starting_point);
Track_Point current;
while (true) {
current = null;
// If all points from the open list have been removed (be being checked), it means that there isn't a possible path from the starting to the finish point
if (open.isEmpty()) {
System.out.println("no route available");
return false;
}
// Selects the point with the lowest F value from the open list
for (Track_Point temp : open) {
temp.show();
if (current == null || temp.getF() < current.getF()) {
current = temp;
}
}
// If the current point has reached the finish point, break the loop to construct the path
if (current.equals(finish_point)) {
break;
}
// Removes the current point (with the lowest F value) from the open list
open.remove(current);
// Adds the current point (with the lowest F value) to the closed list
closed.add(current);
ArrayList<Track_Point> possible_points = createNextPossibleTrackPoints(current);
//Sets the parent of the possible points
for (Track_Point tp : possible_points) {
if (!tp.equals(current)) {
tp.setParent(current);
}
}
for (Track_Point possible_point : possible_points) {
double nextG = current.getG() + current.distance(possible_point);
if (nextG < possible_point.getG()) {
open.remove(possible_point);
closed.remove(possible_point);
}
if (!open.contains(possible_point) && !closed.contains(possible_point)) {
possible_point.setParent(current);
open.add(possible_point);
}
}
}
//Track_Point current = finish_point;
while (current.getParent() != null) {
path.add(current);
current = current.getParent();
}
// optimalRacingLine is the list where all the points will be held in the correct order
optimalRacingLine.add(starting_point);
for (int k = path.size() - 1; k >= 0; k--) {
optimalRacingLine.add(path.get(k));
}
return true;
}
createPossiblePoints(Point current)到目前爲止返回當前點的鄰接點列表。 每個點的H值都是在它們的構造函數中計算的,因爲我在那裏通過終點並計算它們之間的距離。 當我爲它設置父項時,計算每個點的G值,G值是從新點到它們的父項+父項的G值的距離。
如何修改此代碼以允許加速/減速?
Track_Point的代碼:
package model;
import javafx.geometry.Point2D;
public class Track_Point extends Point2D {
private Track_Point parent, velocity;
private double f, g, h;
public Track_Point(double x, double y) {
super(x, y);
}
public Track_Point(double x, double y, Track_Point f) { // f is the finish point
super(x, y);
h = distance(f);
}
public void setParent(Track_Point tp) {
parent = tp;
g = distance(tp) + tp.getG();
f = g + h;
velocity = new Track_Point(getX() - parent.getX(), getY() - parent.getY());
}
public Track_Point getParent() {
return parent;
}
public double getG() {
return g;
}
public double getH() {
return h;
}
public double getF() {
return f;
}
public Track_Point getVelocity() {
return velocity;
}
@Override
public String toString() {
return "(" + (int) getX() + " , " + (int) getY() + ")";
}
public void show() {
System.out.println(toString());
}
}
加入了一些我的嘗試失敗的截圖和工作簡單的A *版本
http://tinypic.com/r/zlakg2/8 - 工作版本
http://tinypic.com/r/2e3u07o/8 - 修改後的版本(使用速度爲createNextPossiblePoints方法中的參數)
關鍵是要將x和y速度表示爲狀態的一部分,以及x和y位置。 「Track_Point」包含他們的字段嗎? (你需要顯示'Track_Point'的定義。) –
我已經更新了代碼:) – fatherjim91
我發現了一些令人困惑的事情(我不完全確定f,g和h代表什麼,而且你似乎正在存儲速度在第二個'Track_Point'對象(它自己的速度,你忽略?)),但它可能是你所需要做的就是改變'createNextPossibleTrackPoints()',所以不是產生與當前點相鄰的點,它們是實際的合法位置集合,即,如果將速度添加到當前位置的位置,則位置與您要去的位置相鄰。 –