# Moving objects assignment

**URL:** <https://discourse.processing.org/t/moving-objects-assignment/4346>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 10, 2018, 11:10pm UTC](https://discourse.processing.org/t/moving-objects-assignment/4346 "2018-10-10T23:10:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![guru\_tk](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@guru\_tk](https://discourse.processing.org/u/guru_tk)\
**Post date:** [October 10, 2018, 11:10pm UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/1 "2018-10-10T23:10:32Z")

</div>

this is the question

 ![IMG-20181004-WA0027](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4a901a0690359dc583d47d04c47c351d6a87ba04.jpeg)  
[https://photos.google.com/photo/AF1QipM2ICRz3oqmgr-gBQ7dQp5yBVqK1\_ag2hX2v0ap](https://photos.google.com/photo/AF1QipM2ICRz3oqmgr-gBQ7dQp5yBVqK1_ag2hX2v0ap)  
[https://photos.google.com/photo/AF1QipOUSXFSEl3n3UNftSNsEon3xtcczVtXGArcZV6G](https://photos.google.com/photo/AF1QipOUSXFSEl3n3UNftSNsEon3xtcczVtXGArcZV6G)

Create a new program, with a size of 400x400 ,in processing and implement the features as described below using loops and if statements:

1. Create a class called vehicle. The class should draw an arrow shaped vehicle and add the following pictures to the class:  
• A X and y coordinate of the centre of the vehicle  
• A fill colour.  
• A variable indicating the direction the vehicle is facing.  
• A constructor to give initial values to the X and Y coordinates.  
• A function to set the fill colour.  
• Functions to draw the vehicle facing the different directions (see fig 1 to 4).  
• A MoveLeft function to decrease the vehicles X coordinate by 1 pixel, moving it to the left.  
• A MoveRight function to increase the vehicles X coordinate by 1 pixel, moving it to the Right.  
• A MoveUp function to decrease the vehicles Y coordinate by 1 pixel, moving it Down.  
• A MoveDown function to increase the vehicles Y coordinate by 1 pixel, moving it Up .  
Using the mouse clicked function add the following features  
• Left mouse button is clicked the first vehicle must systematically move to the location where the mouse was clicked.  
• Right mouse button is clicked the second vehicle must systematically move to the location where the mouse was clicked.

**_this is my program_**

```auto
Vehicle myVehicle1;
Vehicle myVehicle2;
float xCoOrdinate = 50, yCoOrdinate = 50;
int move = 3;
void setup() {
  size(400, 400);
  // Initialize Car object
  myVehicle1 = new Vehicle(color(255, 0, 0), random(0, width), random(0, height), 1);
  myVehicle2 = new Vehicle(color(0, 0, 255), random(0, width), random(0, height), 1);
}
void draw() {
  background(255);
  // Operate Car object.
 // moving();
 // myVehicle1.move();
  myVehicle1.display();
 // myVehicle2.move();
  myVehicle2.display();
}
class Vehicle {
  color c;
  float xpos;
  float ypos;
  float xspeed;
  Vehicle(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
    xpos = random(0, width);
    ypos = random(0, height);
  }
  void display() {//vehicle shape
    rectMode(CENTER);
    fill(c);
    quad(xpos, ypos-15, xpos+15, ypos+8, xpos, ypos, xpos-15, ypos+8);
  }
  //void move() {
    //xpos = xpos + xspeed;
    //if (xpos > width) {
      //xpos = 0;
    //}
  //}
  void facingLeft() {
  fill(c);
  quad(xpos-15, ypos, xpos+8, ypos-15, xpos, ypos, xpos+8, ypos+15);
}
void facingRight() {
  fill(c);
  quad(xpos, ypos, xpos-8, ypos-15, xpos+15, ypos, xpos-8, ypos+15);
}
void facingUp() {
  fill(c);
  quad(xpos, ypos-15, xpos+15, ypos+8, xpos, ypos, xpos-15, ypos+8);
}
void facingDown() {
  fill(c);
  quad(xpos-15, ypos-8, xpos, ypos, xpos+15, ypos-8, xpos, ypos+15);
}

  void moveLeft() {
    xCoOrdinate--;
  }
  void moveRight() {
    xCoOrdinate++;
  }
  void moveUp() {
    xCoOrdinate--;
  }
  void moveDown() {
    xCoOrdinate++;
  }
  void moving(){
for(int i = 0; i < 10; i++)
  {
      //rectMode(CORNER);
      fill(233,56,43);
      rect(xpos, ypos, 25, 25);
    // TestLocations(k);
     if(xpos < mouseX)
      {
        
       xpos = xpos + move; 
       facingLeft();
       moveLeft();
  }
       if(xpos > mouseX)
      {
       xpos = xpos - move; 
       facingRight();
       moveRight();
      }
      
      if(ypos > mouseY)
      {
       ypos = ypos - move; 
       facingUp();
       moveUp();
      }
      
       if(ypos < mouseY)
      {
       ypos = ypos + move; 
       facingDown();
       moveDown();
      } 
      //if((xpos == mouseX) && (ypos == mouseY))
     // {
       // xpos = tempXpos;
       // ypos = tempYpos;
      }
}
}
  void mouseClicked() {
    if (mouseButton == LEFT) {
      //myVehicle1.move();
    } else {
     // myVehicle2.move();
    }
  }
//}

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [October 10, 2018, 11:43pm UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/2 "2018-10-10T23:43:57Z")

</div>

What exactly do you need help with within your program?

---

<div class="post-metadata">

**Author:** ![guru\_tk](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@guru\_tk](https://discourse.processing.org/u/guru_tk)\
**Post date:** [October 10, 2018, 11:53pm UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/3 "2018-10-10T23:53:27Z")

</div>

Im new to processing,so i dont know what its short of for it to function…so i need someone to troubleshoot for me and help me where doesn’t function well.The vehicles should be controlled by mouse clicking,right click one vehicle and right click the other one.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 11, 2018, 12:39am UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/4 "2018-10-11T00:39:03Z")

</div>

Alright, but it’s impossible for us to troubleshoot that code right now because it is a mess. You should at least edit your post and format your code properly - select the code, and hit the format button, which looks like this: \</\>

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [October 11, 2018, 1:19am UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/5 "2018-10-11T01:19:11Z")

</div>

You’ve figured out how to make the vehicles go to the mouse, but it seems by the look of your assignment they only want it to move one direction at a time. (Right now the vehicle can go 2 directions at the same time)

It also seems you’ve mixed up left and right when drawing the vehicle.

---

<div class="post-metadata">

**Author:** ![guru\_tk](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@guru\_tk](https://discourse.processing.org/u/guru_tk)\
**Post date:** [October 11, 2018, 4:09am UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/6 "2018-10-11T04:09:13Z")

</div>

thank you, I think its better now…please help ASAP

---

<div class="post-metadata">

**Author:** ![guru\_tk](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@guru\_tk](https://discourse.processing.org/u/guru_tk)\
**Post date:** [October 11, 2018, 4:19am UTC](https://discourse.processing.org/t/moving-objects-assignment/4346/7 "2018-10-11T04:19:48Z")

</div>

wasn’t aware of that,will get onto it…they actually dont move to the mouse,thats whats confusing me too.
