Examples

02 - Add object

Add simple boxes and a circle to your world. No user interaction.

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 Box
PPBox myBox2;

//Create a PPhys2D Box
PPBox myBox3;

//Create a PPhys2D Circle
PPCircle myCircle1;


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

  //Set a standard world gravity
  world.setGravity(0, 200);
  
  //Add a first box to the world
  myBox = new PPBox(50, 90);
  myBox.setPosition(265, 120);
  myBox.setStrokeWidth(1);
  myBox.setRotation(0.2);
  myBox.setFillColor(new Color(60,60,140));
  myBox.setStrokeColor(new Color(40,40,40));
  world.add(myBox);
  
  //Add a second box to the world
  myBox2 = new PPBox(50, 20);
  myBox2.setPosition(280, 270);
  myBox2.setStrokeWidth(1);
  myBox2.setRotation(-0.3);
  myBox2.setFillColor(new Color(220,40,100));
  myBox2.setStrokeColor(new Color(40,40,40));
  world.add(myBox2);
  
  //Add a third box to the world
  myBox3 = new PPBox(90, 40);
  myBox3.setPosition(270, 220);
  myBox3.setStrokeWidth(1);
  myBox3.setRotation(-0.3);
  myBox3.setFillColor(new Color(180,220,100));
  myBox3.setStrokeColor(new Color(40,40,40));
  world.add(myBox3);
  
  //Add a circle to the world
  myCircle1 = new PPCircle(20);
  myCircle1.setPosition(305, 23);
  myCircle1.setStrokeWidth(1);
  myCircle1.setRotation(-0.3);
  myCircle1.setFillColor(new Color(180,20,200));
  myCircle1.setStrokeColor(new Color(40,40,40));
  world.add(myCircle1);
  
  //Set world edges in dark gray
  world.setEdges(this, new Color (40, 40, 40));

}

void draw () {
  //Clear screen
  background(255); 
  //Draw world
  world.draw(this);
}