Game controls: input and forces

Affecting bodies dynamically
Create a fisica world and combine them with the standard Processing functions mousePressed and keyPressed:

void mousePressed()
//do something

//this function is called when a key is pressed
void keyPressed() {
//now you have to check which key was pressed

//for letters, symbols and numbers you can use the special variable 'key'
  if (key == 'a') {
    println("a pressed");
  if (key == ' ') {
    println("space pressed");

  //for special keys you can use keyCode and these processing constants:
  if (keyCode == SHIFT) {
    println("SHIFT Pressed");

Find a way to:
.create a new body
.setting the position of an existing body to the mouse coordinate
.setting the rotation, the width and height of a body
.toggle the body as “static”
.remove a body
.attach and detach an image to the body

How to detect if a key is down

Processing doesn’t have a function to detect if a particular key is currently pressed. You have to use state variables or arrays in combination with keyPressed and keyReleased.
This is a very important feature in games so we’ll use a couple of custom functions from now on (this is not a fisica specific example).

//I use an array to store the state of all keys in a boolean array down true, up false
//an array is like a table or a container of variables of the same kind
boolean[] keys = new boolean[526];

void setup() {
  //sketch size
  size(800, 600);

void draw() {

  this is the function you can use to check if a key is currently down
  it can be used in a few ways: 
  if(keyIsDown(UP)) UP is the constant for the arrow key
  others are UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT...
    println("The key A is DOWN");
  //you can also check if it's up
  //if(keyIsDown('a') == false)
    //println("The key A is UP");


//this function is called when a key is pressed
void keyPressed() {
println("The key "+ key+ " just went down");

//update the state in the array
keys[keyCode] = true;

//this function is called when a key is released
void keyReleased() {
println("The key "+ key+ " just went up");

//update the state in the array
keys[keyCode] = false; 

//This may be a little exotic, just leave it there
boolean keyIsDown(char c) { String s = c + "";
return keyIsDown(int(s.toUpperCase().charAt(0))); }
boolean keyIsDown(String s) { return keyIsDown(int(s.toUpperCase().charAt(0))); }
boolean keyIsDown(int code) { if (keys.length >= code) { return keys[code]; } return false; }

Moving things: Force, Impulse, Momentum

Screen Shot 2014-10-14 at 11.10.24 AM

Download the example

Different ways to move objects:

circle.addForce(2000, 0);


box.addImpulse(2000, 0);

circle.setVelocity(100, 0);


Example: 4-way movement
Defender (1981) Defender (1981)
When games moved from the paddle (knob to play pong) to the joystick, 4 or 8 way controls became popular and established genres and conventions like shoot ’em up, top-down (e.g. Zelda), or side scrolling brawler (e.g. Double Dragon).
Download Example

Example: move toward a point
Point an click movements (usually in combination with path-finding algorithms to avoid obstacles) are the basis of several genres like the graphic adventure (Maniac Mansion) or the Real Time Strategy or RTS (e.g. Starcraft).
DownloadDownload Example

Example: directional movement
Asteroids (1979) Asteroids (1979)
The 360 degree relative movement was the among the very first type of control employed in games (i.e. SpaceWar!) and it’s the basis of top down racing games. It breaks the controls into rotation and thrust, a simple interface paddle/knob + button can command very complex manoeuvres.


Implement a control system for a body that “feels good” and responsive.

Posted in

Post a comment