Examples

07 - Hot-air balloon

This example creates a simple scene by combining the previous examples. You control a hot-air balloon travelling across the clouds. Use arrows to control the balloon.

Applet

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Code

//Import all Phys2D libraries
import pphys2d.bodies.*;
import pphys2d.joints.*;
import pphys2d.shapes.*;
import pphys2d.phys2d.raw.collide.*;
import pphys2d.phys2d.raw.strategies.*;
import pphys2d.phys2d.raw.forcesource.*;
import pphys2d.phys2d.util.*;
import pphys2d.phys2d.raw.shapes.*;
import pphys2d.*;

//Create a PPhys2D world
PPWorld world = new PPWorld();

//Create a PPhys2D Box
PPBox myBox;

//Create a PPhys2D Circle
PPCircle myCircle;

//Hol the background image
PImage backgroundImage;

//Create an Array to hold keys
boolean[] keyIsPressed = new boolean[512];

//Create an Array to hold the clouds
PPBox[] clouds = new PPBox[8];

void setup () {
  
  //Set size and framerate
  frameRate(30);
  size(500,500);

  //Set a standard world gravity
  world.setGravity(0, 200);
  
  //Load the background image
  backgroundImage = loadImage("background.png");
  
  //Add a box to the world
  myBox = new PPBox(55, 85);
  myBox.setPosition(250, 100);
  myBox.setStrokeWidth(1);
  myBox.setDamping(2);
  myBox.attachImage(loadImage("balloon.png"));
  myBox.setRotatable(false);
  world.add(myBox);
  
  //Create clouds and add them to the world
  for(int i=0; i <clouds.length; i++) {    
    clouds[i] = new PPBox(70, 43);
    clouds[i].attachImage(loadImage("cloud.png"));
    clouds[i].setPosition(random(50,450), random(50,350));
    clouds[i].setGravityEffected(false);
    clouds[i].setRotation(random(-0.2, 0.2));
    clouds[i].setRotatable(false);
    world.add(clouds[i]);
  }
  
  //Set world edges in dark gray
  world.setEdges(this, new Color (40, 40, 40));

}

void draw () {
  //Clear screen by drawing background image
  image(backgroundImage, 0, 0);
  //Draw world
  world.draw(this);  
  //Check if a key has been pressed add add force
  checkKey();  
}


void keyPressed() {
  //Set presed key code
  keyIsPressed[keyCode] = true;
}

void keyReleased() {
  //Set presed key code
  keyIsPressed[keyCode] = false;
}


void checkKey() {  
  if(keyIsPressed[38])myBox.addForce(0, -80000); //UP
  if(keyIsPressed[40])myBox.addForce(0, 80000); //DOWN
  if(keyIsPressed[37])myBox.addForce(-80000, 0); //LEFT
  if(keyIsPressed[39])myBox.addForce(80000, 0); //RIGHT
}