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

时间:2018-05-02 19:03:11

标签: java processing

我对编码非常陌生,我的目标是在到达第一个(站点)时制作和对象(卡车)更改目的地。 至于现在,卡车离开原来的地方(混凝土厂)并移动到创建的第一个站点。用户可以使用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();
  }
}

1 个答案:

答案 0 :(得分:0)

你真的非常接近莉莉。

如果您打印目的地和位置的值,您会发现它们已经超级关闭,但是,由于增量,它们永远不会“满足”。值永远不会匹配(不相等)。

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

void reachDestination()
  {
    println(destination,location);
      //if ((destination.x == location.x) && (destination.y == location.y)) {
      if(dist(destination.x,destination.y,location.x,location.y) < 1){

        if(siteNumber < sites.size() -1){

          siteNumber++; // siteNumber = siteNumber + 1;
          destination = sites.get(siteNumber);
          changeDirection = true;

        }else{

          println("reached final site");

        }
        println("reachDestination");
    }
  }

请记住,dist()使用平方根,这可能会成为大量网站的缓慢计算(由于sqrt),但您可以使用平方距离。

此外,PVector具有lerp()函数,该函数返回两个给定位置(例如目的地和站点)之间的插值位置和插值量(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);
}

您实际上可以将其作为演示程序运行:

var destinations;

var traversal = 0.0;

function setup(){
  createCanvas(100,100);
  destinations = [
    createVector(10,10),
    createVector(90,10),
    createVector(90,90),
    createVector(10,90)
  ];
}
function draw(){
  background(255);
  
  //draw destinations
  for(var i = 0 ; i < destinations.length; i++){
    var d = destinations[i];
    ellipse(d.x,d.y,9,9);
  }
  
  //calculate traversal
  var traversed = traversePoints(destinations,0.01);
  //draw traversal
  ellipse(traversed.x,traversed.y,3,3);
}

function traversePoints(destinations,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
  var pointIndex = Math.floor(traversal);
  //compute the local traversal (0.0 -> 1.0 = 0% to 100% in between two points: current and next)
  var pointT = traversal - pointIndex;
  //compute the next current point index
  var pointIndexNext = (pointIndex + 1) % destinations.length; 
  
  //interpolate between current and next points using above local traversal, offsetting by the last mainHingeition)
  return p5.Vector.lerp(destinations[pointIndex],
                            destinations[pointIndexNext],
                            pointT);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js"></script>