我已经为此工作了好几天,却一无所获。我们进行了简单的模拟,其中有两种生物:狐狸和燕尾服。 Tux会随机移动,我应该实现一种方法,以便Fox跟踪最接近的Tux。我发布了生物类而不是Tux,因为他没有覆盖Creature的move()方法。目前只有福克斯。
我对Fox的move方法中的代码的推理:创建最接近的Tux的ArrayList(如果它们的距离小于10)。对列表进行排序,然后将距离最小的Tux放入Creature对象中。获取该生物对象的角度,并根据该角度移动Fox。到目前为止,什么都没有发生。狐狸的全部都进入房间的底部,朝南,永不动弹。
注意:使用的距离方法基于毕达哥拉斯。该代码对于该解决方案而言是微不足道的。只是知道,例如,“ this.distance(c)”返回这两个对象之间的距离。
狐狸类:
import env3d.EnvObject;
import java.util.ArrayList;
import java.util.Collections;
public class Fox extends Creature
{
private double rand = 0;
private double randangle = 0;
private int frame = 0;
private double angle = 0;
private ArrayList<Creature> neighbours;
public Fox(double x, double y, double z)
{
super(x, y, z);
// Must use the mutator as the fields have private access
// in the parent class
setTexture("models/fox/fox.png");
setModel("models/fox/fox.obj");
neighbours = new ArrayList<Creature>();
}
public void move(ArrayList<Creature> creatures, ArrayList<Creature> dead_creatures)
{
// Move the object based on it's closest neighbour
for (Creature c : creatures) {
// Creates a list of closest Tux
ArrayList<Creature> neighbours = new ArrayList<Creature>();
if (this.distance(c) < 4 && c instanceof Tux) {
neighbours.add(c);
}
// sorting algortihm
if (neighbours.size() > 0) {
Creature minc = neighbours.get(0);
for (int i = 1; i < neighbours.size(); i++) {
if (this.distance(neighbours.get(i)) < this.distance(minc) ) {
minc = neighbours.get(i);
}
// angle that Fox should go to catch Tux
angle = Math.toDegrees(Math.atan2(this.getZ()-minc.getZ(),this.getX()-minc.getX()))+90;
}
}
// Move Fox based on it's closest neighbour's angle
setX(getX()+(Math.sin(Math.toRadians(angle))));
setZ(getZ()+(Math.cos(Math.toRadians(angle))));
}
// Makes sure object stays in the dimensions of the room
if (getX() < getScale()) setX(getScale());
if (getX() > 50-getScale()) setX(50 - getScale());
if (getZ() < getScale()) setZ(getScale());
if (getZ() > 50-getScale()) setZ(50 - getScale());
// The move method now handles collision detection
for (Creature c : creatures) {
if (c.distance(this) < c.getScale()+this.getScale() && c instanceof Tux) {
dead_creatures.add(c);
}
}
}
}
晚礼服类:
import env3d.EnvObject;
import java.util.ArrayList;
abstract public class Creature extends EnvObject
{
private double rand = 0;
private double randangle = 0;
private int frame = 0;
/**
* Constructor for objects of class Creature
*/
public Creature(double x, double y, double z)
{
setX(x);
setY(y);
setZ(z);
setScale(1);
}
protected void generateRand()
{
rand = Math.random();
}
protected void generateRandAngle()
{
randangle = Math.random()*120;
}
public void move(ArrayList<Creature> creatures, ArrayList<Creature> dead_creatures)
{
if (frame == 10) {
generateRand();
generateRandAngle();
if (rand < 0.5){
setRotateY(getRotateY()-randangle);
} else if (rand < 1) {
setRotateY(getRotateY()+randangle);
}
frame = 0;
}
setX(getX()+(Math.sin(Math.toRadians(getRotateY()))*0.2));
setZ(getZ()+(Math.cos(Math.toRadians(getRotateY()))*0.2));
if (getX() < getScale()) setX(getScale());
if (getX() > 50-getScale()) setX(50 - getScale());
if (getZ() < getScale()) setZ(getScale());
if (getZ() > 50-getScale()) setZ(50 - getScale());
// The move method now handles collision detection
if (this instanceof Fox) {
for (Creature c : creatures) {
if (c.distance(this) < c.getScale()+this.getScale() && c instanceof Tux) {
dead_creatures.add(c);
}
}
}
frame++;
}
}
游戏类别:
import env3d.Env;
import java.util.ArrayList;
import env3d.EnvApplet;
/**
* A predator and prey simulation. Fox is the predator and Tux is the prey.
*/
public class Game extends EnvApplet
{
private Env env;
private boolean finished;
private boolean move;
private ArrayList<Creature> creatures;
/**
* Constructor for the Game class. It sets up the foxes and tuxes.
*/
public Game()
{
// we use a separate ArrayList to keep track of each animal.
// our room is 50 x 50.
creatures = new ArrayList<Creature>();
for (int i = 0; i < 55; i++) {
if (i < 5) {
creatures.add(new Fox((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
} else {
creatures.add(new Tux((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
}
}
}
/**
* Play the game
*/
public void play()
{
finished = false;
// Create the new environment. Must be done in the same
// method as the game loop
env = new Env();
// Make the room 50 x 50.
env.setRoom(new Room());
// Add all the animals into to the environment for display
for (Creature c : creatures) {
env.addObject(c);
}
// Sets up the camera
env.setCameraXYZ(25, 50, 55);
env.setCameraPitch(-63);
// Turn off the default controls
env.setDefaultControl(false);
// A list to keep track of dead tuxes.
ArrayList<Creature> dead_creatures = new ArrayList<Creature>();
// The main game loop
while (!finished) {
if (env.getKey() == 1) {
finished = true;
}
// Move each fox and tux.
for (Creature c : creatures) {
c.move(creatures, dead_creatures);
}
// Clean up of the dead tuxes.
for (Creature c : dead_creatures) {
env.removeObject(c);
creatures.remove(c);
}
// we clear the ArrayList for the next loop. We could create a new one
// every loop but that would be very inefficient.
dead_creatures.clear();
// Update display
env.advanceOneFrame();
}
// Just a little clean up
env.exit();
}
/**
* Main method to launch the program.
*/
public static void main(String args[]) {
(new Game()).play();
}
}
最佳答案
if (this.distance(c) < 4 && c instanceof Tux) {
neighbours.add(c);
}
结合
无尾礼服(如果他们的距离小于10)
从您的介绍中...首先,两个人说的不一样。其次,出于调试目的,为什么还要关心距离?
creatures.add(new Fox((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
creatures.add(new Tux((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
通常,调试时我通常会尽量避免使用Math.random。无尾礼服有可能在任何狐狸的4个单位距离内产卵。这将导致它们遵循默认路径。
尝试手动将Fox和Tux彼此相邻放置-如果可行,该算法是好的。
即
creatures.add(new Fox(5, 1, 5);
creatures.add(new Tux(5, 1, 4);
另外,在里面添加一个断点
if (neighbours.size() > 0) {
Creature minc = neighbours.get(0); // Breakpoint here
会告诉您是否正在检测任何晚礼服。