Examples
10 - Using restitution
Restitution is the energy transfer that occurs between two bodies when they hit each other. A restitution of 1 means a total energy transfer without energy loss ( such as a pool ball) and a restitution of 0 means a total energy loss without any energy transfer.
In this exemple, 3 pairs of balls hit each other with different restitution values. The two balls of the first pair have a restitution of 1; the second pair has a restitution of 0.5; the third pair has a restitution of 0.2. See what happend when balls hit each other.
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 PPhys2D Circle
PPCircle ball1;
PPCircle ball2;
PPCircle ball3;
PPCircle ball4;
PPCircle ball5;
PPCircle ball6;
void setup () {
//Set size and framerate
frameRate(30);
size(500,500);
//Set a standard world gravity
world.setGravity(0, 0);
//Set world edges in dark gray
world.setEdges(this, new Color (40, 40, 40));
//Add ball 1 to the world
ball1 = new PPCircle(25);
ball1.setPosition(50, 150);
ball1.setStrokeWidth(1);
ball1.setFillColor(new Color(40,40,100));
ball1.setStrokeColor(new Color(40,40,40));
ball1.addForce(1000000, 0);
ball1.setRestitution(1);
ball1.setDamping(0.5);
world.add(ball1);
//Add ball 2 to the world
ball2 = new PPCircle(25);
ball2.setPosition(450, 150);
ball2.setStrokeWidth(1);
ball2.setFillColor(new Color(40,40,100));
ball2.setStrokeColor(new Color(40,40,40));
ball2.addForce(-1000000, 0);
ball2.setRestitution(1);
ball2.setDamping(0.5);
world.add(ball2);
//Add ball 3 to the world
ball3 = new PPCircle(25);
ball3.setPosition(50, 250);
ball3.setStrokeWidth(1);
ball3.setFillColor(new Color(100,40,40));
ball3.setStrokeColor(new Color(40,40,40));
ball3.addForce(1000000, 0);
ball3.setRestitution(0.5);
ball3.setDamping(0.5);
world.add(ball3);
//Add ball 4 to the world
ball4 = new PPCircle(25);
ball4.setPosition(450, 250);
ball4.setStrokeWidth(1);
ball4.setFillColor(new Color(100,40,40));
ball4.setStrokeColor(new Color(40,40,40));
ball4.addForce(-1000000, 0);
ball4.setRestitution(0.5);
ball4.setDamping(0.5);
world.add(ball4);
//Add ball 5 to the world
ball5 = new PPCircle(25);
ball5.setPosition(50, 350);
ball5.setStrokeWidth(1);
ball5.setFillColor(new Color(40,100,40));
ball5.setStrokeColor(new Color(40,40,40));
ball5.addForce(1000000, 0);
ball5.setRestitution(0.2);
ball5.setDamping(0.5);
world.add(ball5);
//Add ball 6 to the world
ball6 = new PPCircle(25);
ball6.setPosition(450, 350);
ball6.setStrokeWidth(1);
ball6.setFillColor(new Color(40,100,40));
ball6.setStrokeColor(new Color(40,40,40));
ball6.addForce(-1000000, 0);
ball6.setRestitution(0.2);
ball6.setDamping(0.5);
world.add(ball6);
}
void draw () {
//Clear screen
background(255);
//Draw world
world.draw(this);
}
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 PPhys2D Circle
PPCircle ball1;
PPCircle ball2;
PPCircle ball3;
PPCircle ball4;
PPCircle ball5;
PPCircle ball6;
void setup () {
//Set size and framerate
frameRate(30);
size(500,500);
//Set a standard world gravity
world.setGravity(0, 0);
//Set world edges in dark gray
world.setEdges(this, new Color (40, 40, 40));
//Add ball 1 to the world
ball1 = new PPCircle(25);
ball1.setPosition(50, 150);
ball1.setStrokeWidth(1);
ball1.setFillColor(new Color(40,40,100));
ball1.setStrokeColor(new Color(40,40,40));
ball1.addForce(1000000, 0);
ball1.setRestitution(1);
ball1.setDamping(0.5);
world.add(ball1);
//Add ball 2 to the world
ball2 = new PPCircle(25);
ball2.setPosition(450, 150);
ball2.setStrokeWidth(1);
ball2.setFillColor(new Color(40,40,100));
ball2.setStrokeColor(new Color(40,40,40));
ball2.addForce(-1000000, 0);
ball2.setRestitution(1);
ball2.setDamping(0.5);
world.add(ball2);
//Add ball 3 to the world
ball3 = new PPCircle(25);
ball3.setPosition(50, 250);
ball3.setStrokeWidth(1);
ball3.setFillColor(new Color(100,40,40));
ball3.setStrokeColor(new Color(40,40,40));
ball3.addForce(1000000, 0);
ball3.setRestitution(0.5);
ball3.setDamping(0.5);
world.add(ball3);
//Add ball 4 to the world
ball4 = new PPCircle(25);
ball4.setPosition(450, 250);
ball4.setStrokeWidth(1);
ball4.setFillColor(new Color(100,40,40));
ball4.setStrokeColor(new Color(40,40,40));
ball4.addForce(-1000000, 0);
ball4.setRestitution(0.5);
ball4.setDamping(0.5);
world.add(ball4);
//Add ball 5 to the world
ball5 = new PPCircle(25);
ball5.setPosition(50, 350);
ball5.setStrokeWidth(1);
ball5.setFillColor(new Color(40,100,40));
ball5.setStrokeColor(new Color(40,40,40));
ball5.addForce(1000000, 0);
ball5.setRestitution(0.2);
ball5.setDamping(0.5);
world.add(ball5);
//Add ball 6 to the world
ball6 = new PPCircle(25);
ball6.setPosition(450, 350);
ball6.setStrokeWidth(1);
ball6.setFillColor(new Color(40,100,40));
ball6.setStrokeColor(new Color(40,40,40));
ball6.addForce(-1000000, 0);
ball6.setRestitution(0.2);
ball6.setDamping(0.5);
world.add(ball6);
}
void draw () {
//Clear screen
background(255);
//Draw world
world.draw(this);
}