Java 使对象在模拟中相互追逐
我已经为此工作了好几天,但毫无进展。我们做了一个简单的模拟,有两种生物:狐狸和燕尾服。Tux随机移动,我应该实现一个方法,让Fox跟踪最近的Tux。我发布了生物类而不是Tux,因为他没有覆盖生物的move()方法。现在只有狐狸会 我对Fox的move方法中代码的推理是:创建一个最近的Tux的ArrayList(如果它们的距离小于10)。对列表进行排序,并将距离最小的礼服放入生物对象中。获得该生物物体的角度,并根据该角度移动狐狸。到目前为止,什么也没有发生。狐狸只不过是到了房间的底部,面朝南方,一动不动 注:使用的距离法基于毕达哥拉斯。对于这个解决方案,代码很简单。只需知道,例如,“this.distance(c)”返回这两个对象之间的距离 福克斯级:Java 使对象在模拟中相互追逐,java,sorting,Java,Sorting,我已经为此工作了好几天,但毫无进展。我们做了一个简单的模拟,有两种生物:狐狸和燕尾服。Tux随机移动,我应该实现一个方法,让Fox跟踪最近的Tux。我发布了生物类而不是Tux,因为他没有覆盖生物的move()方法。现在只有狐狸会 我对Fox的move方法中代码的推理是:创建一个最近的Tux的ArrayList(如果它们的距离小于10)。对列表进行排序,并将距离最小的礼服放入生物对象中。获得该生物物体的角度,并根据该角度移动狐狸。到目前为止,什么也没有发生。狐狸只不过是到了房间的底部,面朝南方,一
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);
}
}
}
}
导入env3d.EnvObject;
导入java.util.ArrayList;
导入java.util.Collections;
公共级狐狸生物
{
私人双兰德=0;
私有双随机角=0;
私有整数帧=0;
私人双角度=0;
私人ArrayList邻居;
公共福克斯(双x,双y,双z)
{
super(x,y,z);
//必须使用mutator,因为字段具有私有访问权限
//在父类中
setTexture(“models/fox/fox.png”);
setModel(“models/fox/fox.obj”);
邻居=新的ArrayList();
}
公共虚空移动(ArrayList生物,ArrayList死亡生物)
{
//根据对象最近的邻居移动对象
对于(生物c:生物){
//创建最近的礼服列表
ArrayList邻居=新的ArrayList();
if(此距离(c)<4&&c Tux的实例){
加入(c);
}
//排序算法
if(邻居.size()>0){
生物minc=邻居。获取(0);
for(int i=1;i<0.size();i++){
if(this.distance(neights.get(i))50-getScale())setX(50-getScale());
如果(getZ()50-getScale())setZ(50-getScale());
//move方法现在处理碰撞检测
对于(生物c:生物){
if(c.distance(this)
晚礼服类:
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++;
}
}
导入env3d.EnvObject;
导入java.util.ArrayList;
抽象公共类对象
{
私人双兰德=0;
私有双随机角=0;
私有整数帧=0;
/**
*类对象的构造函数
*/
公共生物(双x,双y,双z)
{
setX(x);
赛蒂(y);
setZ(z);
设置刻度(1);
}
受保护的空生成器和()
{
rand=Math.random();
}
受保护的空生成器悬挂()
{
randangle=Math.random()*120;
}
公共虚空移动(ArrayList生物,ArrayList死亡生物)
{
如果(帧==10){
生成器和();
生成器悬挂();
如果(兰特<0.5){
setRotateY(getRotateY()-randangle);
}否则如果(兰特<1){
setRotateY(getRotateY()+randangle);
}
帧=0;
}
setX(getX()+(Math.sin(Math.toRadians(getRotateY())*0.2));
setZ(getZ()+(Math.cos(Math.toRadians(getRotateY())*0.2));
如果(getX()50-getScale())setX(50-getScale());
如果(getZ()50-getScale())setZ(50-getScale());
//move方法现在处理碰撞检测
如果(这是Fox的实例){
对于(生物c:生物){
if(c.distance(this)
游戏类别:
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();
}
}
导入env3d.Env;
导入java.util.ArrayList;
导入env3d.EnvApplet;
/**
*捕食者和猎物的模拟。狐狸是捕食者,燕尾服是猎物。
*/
公共类游戏扩展程序
{
私人环境;
私有布尔完成;
私有布尔移动;
私人ArrayList生物;
/**
*游戏类的构造函数。它设置狐狸和燕尾服。
*/
公共游戏()
{
//我们使用一个单独的数组列表来跟踪每种动物。
//我们的房间是50 x 50。
生物=新ArrayList();
对于(int i=0;i<55;i++){
如果(i<5){
添加(新狐狸((int)(Math.random()*48)+1,1,(int)(Math.random()*48)+1);
}否则{
添加(新Tux((int)(Math.random()*48)+1,1,(int)(Math.random()*48)+1);
}
}
}
/**
*玩游戏
*/
公共游戏
{
完成=错误;
//创建新环境。必须
if (this.distance(c) < 4 && c instanceof Tux) {
neighbours.add(c);
}
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));
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