【发布时间】:2014-05-04 02:04:04
【问题描述】:
我正在开发像http://harmmade.com/vectorracer/ 这样的赛车游戏,并且我已经实现了供 AI 玩家使用的 A* 算法。该算法适用于 1 块移动,但我不希望 AI 玩家一次只移动 1 个块(仅使用它们的相邻点),我需要他们能够在它们时加速和减速转弯关闭。他们的下一个位置应该取决于他们之前的位置,就像 Vector Racer。
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的定义。) -
我已经更新了代码:)
-
我发现一些事情令人困惑(我不确定 f、g 和 h 代表什么,而且您似乎将速度存储在第二个
Track_Point对象中(您忽略了它自己的速度) ?)),但您可能需要做的就是更改createNextPossibleTrackPoints(),以便生成与当前点相邻的点,而不是生成与当前点相邻的点,它们是实际的合法位置集,即与您将去的位置相邻的位置,如果您将速度添加到当前点的位置。 -
还有几件事(那我得走了,对不起):如果
distance()测量欧几里得距离,那么它不一定是到达所需的剩余步数的下限完成,因为您每次移动的速度可能超过 1 个单位——尽管我认为distance() / velocity会。您也可能对我自己的解决方案感兴趣:stackoverflow.com/a/6598303/47984。这会使用搜索空间的广度优先搜索来计算完全最优(可能的移动次数最少)的解决方案。 -
g 是从起点到当前点的移动成本,h 是当前点和终点之间的估计距离,f 是两者的总和 A* 算法使用的最佳-第一次搜索并这样做,它选择成本最低的节点,即这个解释涵盖了所有内容的 f 值,它是我用来实现 A* link Track_Point 速度的节点,这是我解决问题的失败尝试,我在 createPossiblePoints 方法中将它作为第二个参数传递,它确实创建了正确的下一个位置
标签: java algorithm a-star racing