Moving a Vehicle

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:

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:

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

VEHICLE CLASS

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

1 Like
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);}
1 Like

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

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

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

https://processing.org/reference/keyPressed_.html

https://processing.org/reference/keyReleased_.html

:slight_smile:

1 Like

are called automatically…

1 Like

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/

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.

1 Like