# Moving a Vehicle

**URL:** <https://discourse.processing.org/t/moving-a-vehicle/12107>\
**Category:** Coding Questions\
**Created:** [June 16, 2019, 10:07pm UTC](https://discourse.processing.org/t/moving-a-vehicle/12107 "2019-06-16T22:07:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcholman23](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marcholman23](https://discourse.processing.org/u/marcholman23)\
**Post date:** [June 16, 2019, 10:07pm UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/1 "2019-06-16T22:07:23Z")

</div>

Hi,  
I’m trying to create a program that allows the user to move a car with the up down right and left arrow keys.

I want the up and down keys to accelerate backwards and forwards.

I want the left and right keys to control direction of movement so that the car rotates and points where its headed.

I’m trying to write this in an object oriented fashion and use PVectors.

So far I have the car moving but the controls are extremely awkward. Also it seems keyPressed and keyReleased can’t be placed in the Input class - it seems processing only calls them if they are after draw in the main sketch program. Seems strange.

I’d appreciate any advice. Here is my code:

Main class:

```auto
Vehicle vehicle;
Input input;

void setup() {
 size(640, 480); 
 input = new Input();
 vehicle = new Vehicle(input);
 
}

void draw() {
 background(0);
 vehicle.applyForce();
 vehicle.display();
}

void keyPressed() {
   int code = keyCode;
   switch(code) {
    case 38:
      input.up = true;
      break;
    case 40:
      input.down = true;
      break;
    case 39:
      input.right = true;
      break;
    case 37:
      input.left = true;
      break;
   }
  }
  
  void keyReleased() {
   int code = keyCode;
   switch(code) {
    case 38:
      input.up = false;
      break;
    case 40:
      input.down = false;
      break;
    case 39:
      input.right = false;
      break;
    case 37:
      input.left = false;
      break;
   }
  }

```

INPUT CONSTANTS CLASS:

```auto
class Input {
  boolean up = false;
  boolean down = false;
  boolean left = false;
  boolean right = false;
 
}

```

VEHICLE CLASS

```auto
class Vehicle {
 PVector location;
 PVector velocity;
 PVector acceleration;
 PImage image;
 Input input;
 
 int topSpeed = 1;
 
 Vehicle(Input input) {
   this.image = loadImage("player1car.png");
   this.input = input;
   this.location = new PVector(width/2, height/2);
   this.velocity = new PVector(0,0);
   this.acceleration = new PVector(0.0,0.0);
 }
 
 void applyForce() {
   // get the acceleration
   if(input.up) {
    acceleration.y -= 1; 
    println("up");
   }
   if(input.down) {
    acceleration.y += 1;
    println("down");
   }
   if(input.right) {
    acceleration.x += 1; 
    println("right");
   }
   if(input.left) {
    acceleration.x -= 1; 
    println("left");
   }
   
   velocity.add(acceleration);
   velocity.limit(topSpeed);
   location.add(velocity);
   velocity.mult(0);
 }
 
 
 
 void display() {
   
   image(image, location.x, location.y);
 }
}

```

Thanks for your help,  
Marc

---

<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:** [June 17, 2019, 12:11am UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/2 "2019-06-17T00:11:44Z")

</div>

```auto
PVector p=new PVector(110,110,0);float v=0;boolean[] keys=new boolean[4];
void setup(){size(220, 220);}void draw(){background(0);s();r();}
void keyPressed(){b(true);}void keyReleased(){b(false);}
void b(boolean d){if(key==CODED){if(keyCode==LEFT)keys[0]=d;if(keyCode==RIGHT)keys[1]=d;if(keyCode==UP)keys[2]=d;if(keyCode==DOWN)keys[3]=d;}}
void s(){p.z+=(keys[1]?.1:0)-(keys[0]?.1:0);v+=(keys[2]?.1:0)-(keys[3]?.1:0);p.x=(p.x+width+v*cos(p.z))%width;p.y=(p.y+height+v*sin(p.z))%height;v*=.99;}
void r(){translate(p.x,p.y);rotate(p.z);noStroke();fill(0,128,0); rect(-10,-10,20,20);stroke(0);noFill();triangle(-8,-8,7,0,-8,8);}

```

---

<div class="post-metadata">

**Author:** ![marcholman23](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marcholman23](https://discourse.processing.org/u/marcholman23)\
**Post date:** [June 17, 2019, 1:36am UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/3 "2019-06-17T01:36:11Z")

</div>

Thanks! Your code works and should be very helpful to me.

I’m having a little difficulty parsing out the meaning of your variable and method names though. I gather you used a three dimensional PVector to store the rotation value as z?

Can you give me a rundown on the variable and method names? That would make it easier to read the code.

Thanks much,  
Marc

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 17, 2019, 4:29am UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/4 "2019-06-17T04:29:08Z")

</div>

`http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Nl$898UQxW3Q`

---

<div class="post-metadata">

**Author:** ![marcholman23](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marcholman23](https://discourse.processing.org/u/marcholman23)\
**Post date:** [June 17, 2019, 3:05pm UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/5 "2019-06-17T15:05:03Z")

</div>

Aha… thank you.

One additional question.

I do not know the term for the file with setup and draw but do keyPressed and keyReleased have to be called from that?

I tried building my input into a separate Input class and saw that these methods weren’t working.

Thanks,  
Marc

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 17, 2019, 3:13pm UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/6 "2019-06-17T15:13:17Z")

</div>

[https://processing.org/reference/keyPressed\_.html](https://processing.org/reference/keyPressed_.html)

[https://processing.org/reference/keyReleased\_.html](https://processing.org/reference/keyReleased_.html)

🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 17, 2019, 3:35pm UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/7 "2019-06-17T15:35:33Z")

</div>

> [@marcholman23](#):
>
> keyPressed and keyReleased

are called automatically…

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 19, 2019, 4:26pm UTC](https://discourse.processing.org/t/moving-a-vehicle/12107/8 "2019-06-19T16:26:15Z")

</div>

The functions listed in the reference under “Input: Mouse” and “Input: Keyboard” are called automatically when an event occurs – they are hooks. You put code in functions with those magic names in order to handle those kinds of events.

[https://processing.org/reference/](https://processing.org/reference/)

There is a simple method for registering your own method – general pre or post draw – that is used by Library authors. But the simplest thing is to just create a function and call it from draw or from one of the special Input functions on an input event.

- [https://github.com/processing/processing/wiki/Library-Basics](https://github.com/processing/processing/wiki/Library-Basics)
