Examples

03 - Add forces

Starting from example 2, we add a vertical force to the box, giving the impression that the box tries to fly away.

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;

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(50, 90);
  myBox.setPosition(250, 100);
  myBox.setStrokeWidth(1);
  myBox.setFillColor(new Color(40,40,100));
  myBox.setStrokeColor(new Color(40,40,40));
  world.add(myBox);
  
  //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);
  
  //Add force depending on current velocity
  if(myBox.getVelocityY()  >  100) myBox.addForce(0, -2000000.0f);
  
  
}