Examples

Space shooter

This is a basic space shooter engine. Use ARROWS to move the spaceship and SPACEBAR to shoot. If a bullet reach the target, then the «hits counter» increase.

You should also take a look at the simplicity of the collision detection between the bullets and the target, which is done in few lines.

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
PPPoly myPoly;

//Create a PPhys2D Circle
PPCircle myCircle;

//Load bullet image
PImage bulletImage;

//Le pointage
int score = 0;

//Create a Timer
int targetMillis;

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

//Array for bullets
ArrayList bullets = new ArrayList();

//Font
PFont myFont;

void setup() 
{
  //Set basic settings
  frameRate(30);
  size(400, 400);
  
  //Set a standard world gravity
  world.setGravity(0, 0);
  
  //Add a polygon to the world
  myPoly = new PPPoly();
  myPoly.setPosition(250, 100);
  myPoly.setStrokeWidth(1);
  myPoly.setRotation(2*PI);
  
  //Create shape for polygon  
  myPoly.vertex(0, -56);
  myPoly.vertex(16, -33);
  myPoly.vertex(15, -22);
  myPoly.vertex(30, -5);
  myPoly.vertex(15, -5);
  myPoly.vertex(15, 6);
  myPoly.vertex(9, 6);
  myPoly.vertex(9, 17);
  myPoly.vertex(14, 21);
  myPoly.vertex(8, 21);
  myPoly.vertex(12, 34);
  myPoly.vertex(9, 45);
  myPoly.vertex(0, 54);
  myPoly.vertex(-8, 42);
  myPoly.vertex(-12, 33);
  myPoly.vertex(-13, 22);
  myPoly.vertex(-19, 22);
  myPoly.vertex(-14, 14);
  myPoly.vertex(-14, 8);
  myPoly.vertex(-18, 8);
  myPoly.vertex(-18, -3);
  myPoly.vertex(-31, -3);
  myPoly.vertex(-16, -20);
  myPoly.vertex(-16, -30);
  
  //Attach "/data/spaceship.png" to the body
  myPoly.attachImage(loadImage("spaceship.png"));
  world.add(myPoly);
  
  //Add a static circle to the world
  myCircle = new PPCircle(75);
  myCircle.setPosition(208, 250);
  myCircle.setStaticBody(true);
  //Attach "/data/panet.png" to the body
  myCircle.attachImage(loadImage("target.png"));
  world.add(myCircle);
  
  //Set world edges in dark gray
  world.setEdges(this, new Color (40, 40, 40));  
  
  //Load bullet image
  bulletImage = loadImage("bullet.png"); 
 
 //Load score font
  myFont = loadFont("SansSerif-12.vlw"); 
 
 //Start timer 
 targetMillis = millis();
}

void draw() 
{
  //Reset background
  background(255);
  
  //Check Bullets collisions
  checkBullets(); 
 
  //Check pressed key
  checkKey();  
  
  //Draw world
  world.draw(this);
  
  //Draw score
  textAlign(CENTER);
  textFont(myFont);  
  text("Hits : " + score, width/2, 40, width, 50);
   
}

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

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

void checkKey() {    

  if( keyIsPressed[38]) {
    //UP
    float rotation = myPoly.getRotation()-PI/2.0;
    float force = 40000.0f;
    myPoly.addForce(cos(rotation)*force, sin(rotation)*force);    
  }
  
  if(keyIsPressed[40]) {
    //DOWN
    float rotation = myPoly.getRotation()-PI/2.0;
    float force = -40000.0f;
    myPoly.addForce(cos(rotation)*force, sin(rotation)*force);
  }
  
  if(keyIsPressed[37]) {
    //LEFT
    myPoly.adjustAngularVelocity(-0.1);
  }
  
  if(keyIsPressed[39]) { 
    //RIGHT
    myPoly.adjustAngularVelocity(0.1);
  }
  
  if(keyIsPressed[32]) { 
    //SPACEBAR
    shoot();
  }
}

void shoot() {
    
  //If timer is done
  if(millis()  > targetMillis) {
    //Create a new bullet
    PPCircle bullet = new PPCircle(5);
    bullet.setFillColor(new Color(255,0,0));
    bullet.attachImage(bulletImage);
    bullet.setStrokeWidth(1);
    //Add force depending on spaceship position
    float rotation = myPoly.getRotation()-PI/2.0;
    float force = 3000000.0f;
    bullet.addForce(cos(rotation)*force, sin(rotation)*force);
    //Set Position
    bullet.setPosition(myPoly.getX() + cos(rotation)*70, myPoly.getY() + sin(rotation)*70);
    //Add the new ball to the world
    bullets.add(bullet);
    world.add((PPCircle)(bullets.get(bullets.size()-1)));
    //Restart Timer
    targetMillis = millis() + 200;     
  }  
}

void checkBullets() {
  //Check each bullet position
  for(int i=0; i <bullets.size(); i++) {  
    PPCircle bullet = (PPCircle) bullets.get(i);
    
    if(bullet.isTouchingBody(myCircle)) {
      //HIT
      score ++;
      //Remove bullet since it has it the target
      world.remove(bullet);
      bullets.remove(bullet);
    }
    
    if(bullet.isTouchingBody(myPoly) || bullet.getX()  > width-10 || bullet.getX()  < 10 || bullet.getY()  > height-10 || bullet.getY()  < 10) {
      //Remove bullet since it is beyond edges
      world.remove(bullet);   
      bullets.remove(bullet);   
    }
  }
}