Examples

09 - Using polygons

You may also be interested by giving a custom shape to a body and get a more accurate physic simulation. To get this result, you will need to use PPConvexPoly bodies or PPPoly depending on the type of polygon you want to create (Convex or Concave).

In this example, we start from exemple 5A but use a PPPoly instead of a PPBox to get a more accurate collision detection.

After creating the PPPoly, use myPoly.vertex(x,y) to give the polygon the wanted shape. The point (0,0) is the center of gravity. Most of the time, you won't add a vertex at (0,0).

myPoly.vertex(0, -56);
myPoly.vertex(16, -33);
myPoly.vertex(15, -22);
myPoly.vertex(30, -5);

[...]

myPoly.vertex(-18, -3);
myPoly.vertex(-31, -3);
myPoly.vertex(-16, -20);
myPoly.vertex(-16, -30);

The result is :

Since there is no gravity, you need to USE ARROWS in order to apply a force to the ship and move it forward.

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;

//Create an ArrayList 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, 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(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]) {
    //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);
  }
}