Java 使对象在到达目的地时改变方向

Java 使对象在到达目的地时改变方向,java,processing,Java,Processing,我对编码非常陌生,我的目标是在到达第一个目的地(地点)时,使目标(卡车)改变目的地。 至于现在,卡车离开了原来的地方(混凝土厂),移到了第一个创建的场地。用户可以使用mousePress添加站点(我使用了一个站点数组)。但后来我的卡车卡在了第一个地点,无法开到下一个地点。我还希望卡车前往2个地点,然后返回混凝土厂,然后再次前往2个地点等。。。 有人能帮帮我吗,我很绝望,我的最后期限是下周一 这是我的代码: Lorry lorry; int xCoord; int yCoord; ArrayLi

我对编码非常陌生,我的目标是在到达第一个目的地(地点)时,使目标(卡车)改变目的地。 至于现在,卡车离开了原来的地方(混凝土厂),移到了第一个创建的场地。用户可以使用mousePress添加站点(我使用了一个站点数组)。但后来我的卡车卡在了第一个地点,无法开到下一个地点。我还希望卡车前往2个地点,然后返回混凝土厂,然后再次前往2个地点等。。。 有人能帮帮我吗,我很绝望,我的最后期限是下周一

这是我的代码:

Lorry lorry;

int xCoord;
int yCoord;
ArrayList<Site> sites;
int siteSize = 30;

void setup() // What is called once at the beginning
{
  size (500, 500);

  xCoord = int(width/2);
  yCoord = int(height/2);

  //Creating empty Array List where store sites objects
  sites = new ArrayList<Site>();

  //Adding first site
  sites.add(new Site(random(width), random(height), siteSize));

  //storing lorries
  lorry = new Lorry(xCoord, yCoord);
}

void draw() // Draw the background and concrete plant
{
  background (235, 247, 255);
  ellipse(xCoord, yCoord, 60, 60);

  //Calling the sites
  for (int i = sites.size () - 1; i>=0; i--) {
    Site site = sites.get(i);
    site.displaySites();
  }

  //calling the lorry functions
  lorry.updateLorry();
}

void mousePressed() {
  sites.add(new Site(mouseX, mouseY, siteSize));
}

class Site

{
  float x,y;
  float size;

  Site (float xin, float yin, float sin)
  {
    x = xin;
    y = yin;
    size = sin;
  }

  void displaySites()
  {
    rectangle(x, y, 60, 60);
  }
}

class Lorry
{
  PVector location;
  PVector concretePlant;
  PVector velocity;
  boolean changeDirection;
  int siteNumber = 0;
  Site destination;

  Lorry(float xCoord, float yCoord)
  {
    concretePlant = new PVector(xCoord, yCoord); //Initial start point
    location = new PVector(xCoord, yCoord); //Initial start point
    velocity = new PVector(2, 2);
    destination = sites.get(siteNumber);
    changeDirection = false;
  }

  void displayLorry()
  {
    rectangle(location.x, location.y, 30, 30);
  }

  void Move()
  {
    float xdir = destination.x - location.x;
    float ydir = destination.y - location.y;
    PVector dir = new PVector (xdir, ydir);
    dir.normalize();
    location.add(dir);
  }

  void reachDestination()
  {
      if ((destination.x == location.x) && (destination.y == location.y)) {
        siteNumber++; // siteNumber = siteNumber + 1;
        destination = sites.get(siteNumber);
        changeDirection = true;
    }
  }

  void updateLorry()
  {
    displayLorry();
    Move();
    reachDestination();
  }
}
货车;
int xCoord;
国际协调;
阵列列表站点;
int siteSize=30;
void setup()//开始时调用一次的内容
{
大小(500500);
xCoord=int(宽度/2);
yCoord=int(高度/2);
//创建存储站点对象的空数组列表
sites=新的ArrayList();
//添加第一个站点
添加(新站点(随机(宽度)、随机(高度)、站点大小);
//储存卡车
货车=新货车(xCoord,yCoord);
}
void draw()//绘制背景和混凝土植物
{
背景(235、247、255);
椭圆(xCoord,yCoord,60,60);
//调用站点
对于(int i=sites.size()-1;i>=0;i--){
Site=sites.get(i);
site.displaySites();
}
//调用卡车函数
lorry.updateLorry();
}
void mousePressed(){
添加(新站点(mouseX、mouseY、siteSize));
}
课堂现场
{
浮动x,y;
浮子大小;
场地(浮心、浮阴、浮罪)
{
x=xin;
y=阴;
大小=罪恶;
}
void displaySites()
{
矩形(x,y,60,60);
}
}
班车
{
PVector定位;
PVector混凝土厂;
PVector速度;
布尔变换方向;
int siteNumber=0;
站点目的地;
货车(浮式xCoord、浮式yCoord)
{
concretePlant=新PVector(xCoord,yCoord);//初始起点
位置=新的PVector(xCoord,yCoord);//初始起点
速度=新的PVector(2,2);
目的地=sites.get(siteNumber);
改变方向=错误;
}
货车
{
矩形(位置.x,位置.y,30,30);
}
无效移动()
{
float xdir=destination.x-location.x;
float ydir=destination.y-location.y;
PVector dir=新PVector(xdir,ydir);
dir.normalize();
地址:add(dir);
}
void reachDestination()
{
if((destination.x==location.x)和&(destination.y==location.y)){
siteNumber++;//siteNumber=siteNumber+1;
目的地=sites.get(siteNumber);
改变方向=正确;
}
}
void updateLorry()
{
显示卡车();
Move();
到达目的地();
}
}

你真是超级亲密,莉莉

如果您打印destination和location的值,您会注意到它们正变得越来越接近,但是,由于它们从未“满足”过的增量。这些值从不匹配(不相等)

您可以轻松地将equals条件替换为更实用的阈值距离条件(例如,如果目的地和位置之间的距离小于1px):

void reachDestination()
{
println(目的地、地点);
//if((destination.x==location.x)和&(destination.y==location.y)){
if(距离(destination.x,destination.y,location.x,location.y)<1){
if(siteNumber
请记住,使用平方根可能会使大量站点的计算速度变慢(由于sqrt),但是您可以使用平方距离

此外,PVector具有一个函数,用于返回两个给定位置(例如,目的地和站点)之间的插值位置和插值量(介于0.0(开始位置)和1.0(结束位置)之间的值)

以下是概念验证草图:

PVector[] destinations = {
  new PVector(10,10),
  new PVector(90,10),
  new PVector(90,90),
  new PVector(10,90)
};

float traversal = 0.0;

void setup(){

}
void draw(){
  background(255);

  //draw destinations
  for(PVector d : destinations){
    ellipse(d.x,d.y,9,9);
  }

  //calculate traversal
  PVector traversed = traversePoints(destinations,0.01);
  //draw traversal
  ellipse(traversed.x,traversed.y,3,3);
}

PVector traversePoints(PVector[] destinations,float speed){
  if(speed < 0){
    speed = 0.05;
  }
  //increment full path traversal  (the higher the increment, the faster the move)
  traversal += speed;
  //loop back to 0 when fully traversed 
  if(traversal > destinations.length) traversal = 0.0;
  //compute the current point index
  int pointIndex = (int)traversal;
  //compute the local traversal (0.0 -> 1.0 = 0% to 100% in between two points: current and next)
  float pointT = traversal - pointIndex;
  //compute the next current point index
  int pointIndexNext = (pointIndex + 1) % destinations.length; 

  //interpolate between current and next points using above local traversal, offsetting by the last mainHingeition)
  return PVector.lerp(destinations[pointIndex],
                            destinations[pointIndexNext],
                            pointT);
}
PVector[]目的地={
新PVector(10,10),
新PVector(90,10),
新PVector(90,90),
新PVector(10,90)
};
浮点遍历=0.0;
无效设置(){
}
作废提款(){
背景(255);
//抽签目的地
用于(PVector d:目的地){
椭圆(d.x,d.y,9,9);
}
//计算遍历
PVector traversed=穿越点(目的地,0.01);
//绘制导线
椭圆(横切.x,横切.y,3,3);
}
PVector穿越点(PVector[]目的地,浮动速度){
如果(速度<0){
速度=0.05;
}
//增量全路径遍历(增量越高,移动越快)
横向+=速度;
//完全遍历时循环回0
如果(遍历>destinations.length)遍历=0.0;
//计算当前点索引
int pointIndex=(int)遍历;
//计算局部遍历(0.0->1.0=0%到100%,位于两点之间:当前点和下一点)
float pointT=遍历-点索引;
//计算下一个当前点索引
int pointIndexNext=(pointIndex+1)%destinations.length;
//使用上面的局部遍历在当前点和下一点之间插值,由上一次维护偏移)
返回PVector.lerp(目的地[pointIndex],
目的地[pointIndexNext],
点);
}
实际上,您可以如下所示运行此演示:

var目的地;
var遍历=0.0;
函数设置(){
createCanvas(100100);
目的地=[
createVector(10,10),
createVector(90,10),
createVector(90,90),
createVector(10,90)
];
}
函数绘图(){
背景(255);
//抽签目的地
对于(变量i=0;iPVector[] destinations = {
  new PVector(10,10),
  new PVector(90,10),
  new PVector(90,90),
  new PVector(10,90)
};

float traversal = 0.0;

void setup(){

}
void draw(){
  background(255);

  //draw destinations
  for(PVector d : destinations){
    ellipse(d.x,d.y,9,9);
  }

  //calculate traversal
  PVector traversed = traversePoints(destinations,0.01);
  //draw traversal
  ellipse(traversed.x,traversed.y,3,3);
}

PVector traversePoints(PVector[] destinations,float speed){
  if(speed < 0){
    speed = 0.05;
  }
  //increment full path traversal  (the higher the increment, the faster the move)
  traversal += speed;
  //loop back to 0 when fully traversed 
  if(traversal > destinations.length) traversal = 0.0;
  //compute the current point index
  int pointIndex = (int)traversal;
  //compute the local traversal (0.0 -> 1.0 = 0% to 100% in between two points: current and next)
  float pointT = traversal - pointIndex;
  //compute the next current point index
  int pointIndexNext = (pointIndex + 1) % destinations.length; 

  //interpolate between current and next points using above local traversal, offsetting by the last mainHingeition)
  return PVector.lerp(destinations[pointIndex],
                            destinations[pointIndexNext],
                            pointT);
}