Examples
Throwing
Click and hold the ball to grab it using your mous. Drag and release to throw it.
Once again, physic calculations are done really easily using PPhys2D.
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 Circle
PPCircle myCircle;
//Mouse velocity variables
boolean mousePressed = false;
float oldX;
float oldY;
void setup () {
//Set size and framerate
frameRate(30);
size(500,500);
//Set a standard world gravity
world.setGravity(0, 400);
//Add a circle to the world
myCircle = new PPCircle(75);
myCircle.setPosition(220, 250);
myCircle.setRestitution(0.85);
myCircle.setMass(10000);
myCircle.setFriction(1);
//Attach "/data/panet.png" to the body
myCircle.attachImage(loadImage("ball.png"));
world.add(myCircle);
//Set world edges in dark gray
world.setEdges(this, new Color (40, 40, 40));
}
void draw () {
//Clear screen
background(255);
//Check if a key has been pressed add add force
checkMouse();
//Draw world
world.draw(this);
}
void mousePressed() {
//Defines mouse over bounds
float minX = (myCircle.getX() - myCircle.getSize()/2);
float maxX = (myCircle.getX() + myCircle.getSize()/2);
float minY = (myCircle.getY() - myCircle.getSize()/2);
float maxY = (myCircle.getY() + myCircle.getSize()/2);
//If mouse is over, set mousePressed as TRUE
if(mouseX > minX && mouseX < maxX && mouseY > minY && mouseY < maxY) mousePressed = true;
}
void mouseReleased() {
if(mousePressed) {
//If mouse was pressed over ball, then apply forces
mousePressed = false;
float velocityX = myCircle.getX() - oldX;
float velocityY = myCircle.getY() - oldY;
myCircle.addForce(velocityX*80000, velocityY*80000);
}
}
void checkMouse() {
if(mousePressed) {
//Defines bounds
float minX = myCircle.getSize()/2;
float maxX = width - myCircle.getSize()/2;
float minY = myCircle.getSize()/2;
float maxY = height-myCircle.getSize()/2;
//If mouse is inside bounds, save position values and move ball
if(mouseX > minX && mouseX < maxX && mouseY > minY && mouseY < maxY) {
oldX = myCircle.getX();
oldY = myCircle.getY();
myCircle.setPosition(mouseX, mouseY);
}
//Reset velocity of ball
myCircle.resetForces();
myCircle.adjustAngularVelocity(-myCircle.getAngularVelocity());
}
}
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 Circle
PPCircle myCircle;
//Mouse velocity variables
boolean mousePressed = false;
float oldX;
float oldY;
void setup () {
//Set size and framerate
frameRate(30);
size(500,500);
//Set a standard world gravity
world.setGravity(0, 400);
//Add a circle to the world
myCircle = new PPCircle(75);
myCircle.setPosition(220, 250);
myCircle.setRestitution(0.85);
myCircle.setMass(10000);
myCircle.setFriction(1);
//Attach "/data/panet.png" to the body
myCircle.attachImage(loadImage("ball.png"));
world.add(myCircle);
//Set world edges in dark gray
world.setEdges(this, new Color (40, 40, 40));
}
void draw () {
//Clear screen
background(255);
//Check if a key has been pressed add add force
checkMouse();
//Draw world
world.draw(this);
}
void mousePressed() {
//Defines mouse over bounds
float minX = (myCircle.getX() - myCircle.getSize()/2);
float maxX = (myCircle.getX() + myCircle.getSize()/2);
float minY = (myCircle.getY() - myCircle.getSize()/2);
float maxY = (myCircle.getY() + myCircle.getSize()/2);
//If mouse is over, set mousePressed as TRUE
if(mouseX > minX && mouseX < maxX && mouseY > minY && mouseY < maxY) mousePressed = true;
}
void mouseReleased() {
if(mousePressed) {
//If mouse was pressed over ball, then apply forces
mousePressed = false;
float velocityX = myCircle.getX() - oldX;
float velocityY = myCircle.getY() - oldY;
myCircle.addForce(velocityX*80000, velocityY*80000);
}
}
void checkMouse() {
if(mousePressed) {
//Defines bounds
float minX = myCircle.getSize()/2;
float maxX = width - myCircle.getSize()/2;
float minY = myCircle.getSize()/2;
float maxY = height-myCircle.getSize()/2;
//If mouse is inside bounds, save position values and move ball
if(mouseX > minX && mouseX < maxX && mouseY > minY && mouseY < maxY) {
oldX = myCircle.getX();
oldY = myCircle.getY();
myCircle.setPosition(mouseX, mouseY);
}
//Reset velocity of ball
myCircle.resetForces();
myCircle.adjustAngularVelocity(-myCircle.getAngularVelocity());
}
}