Examples
05A - Attach image
Let's attach images to the previously created bodies. Images should be in PNG format if you want to set transparency, and they should be located in the "data" folder of your Processing Sketch.
Applet
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;
//Create an Array to hold keys
boolean[] keyIsPressed = new boolean[512];
void setup () {
//Set size and framerate
frameRate(30);
size(500,500);
//Set a standard world gravity
world.setGravity(0, 200);
//Add a box to the world
myBox = new PPBox(65, 108);
myBox.setPosition(250, 100);
myBox.setStrokeWidth(1);
myBox.setRotation(2*PI);
//Attach "/data/spaceship.png" to the body
myBox.attachImage(loadImage("spaceship.png"));
world.add(myBox);
//Add a static circle to the world
myCircle = new PPCircle(75);
myCircle.setPosition(220, 250);
myCircle.setStaticBody(true);
//Attach "/data/panet.png" to the body
myCircle.attachImage(loadImage("planet.png"));
world.add(myCircle);
//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);
//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
}
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;
//Create an Array to hold keys
boolean[] keyIsPressed = new boolean[512];
void setup () {
//Set size and framerate
frameRate(30);
size(500,500);
//Set a standard world gravity
world.setGravity(0, 200);
//Add a box to the world
myBox = new PPBox(65, 108);
myBox.setPosition(250, 100);
myBox.setStrokeWidth(1);
myBox.setRotation(2*PI);
//Attach "/data/spaceship.png" to the body
myBox.attachImage(loadImage("spaceship.png"));
world.add(myBox);
//Add a static circle to the world
myCircle = new PPCircle(75);
myCircle.setPosition(220, 250);
myCircle.setStaticBody(true);
//Attach "/data/panet.png" to the body
myCircle.attachImage(loadImage("planet.png"));
world.add(myCircle);
//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);
//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
}