Created
September 27, 2013 22:54
-
-
Save darkwave/6736346 to your computer and use it in GitHub Desktop.
WIP for the Indie Speed Run 2013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Element { | |
Body body; | |
float w, h; | |
String spriteFilename; | |
BodyType bodyType; | |
Element(float x, float y, float w, float h, String _spriteFilename, boolean obstacle) { | |
if (obstacle) | |
bodyType = BodyType.STATIC; | |
else | |
bodyType = BodyType.DYNAMIC; | |
spriteFilename = _spriteFilename; | |
this.w = w; | |
this.h = h; | |
makeBody(new Vec2(x, y), w, h); | |
} | |
// This function adds the rectangle to the box2d world | |
void makeBody(Vec2 center, float w_, float h_) { | |
// Define a polygon (this is what we use for a rectangle) | |
PolygonShape sd = new PolygonShape(); | |
float box2dW = box2d.scalarPixelsToWorld(w_/2); | |
float box2dH = box2d.scalarPixelsToWorld(h_/2); | |
sd.setAsBox(box2dW, box2dH); | |
// Define a fixture | |
FixtureDef fd = new FixtureDef(); | |
fd.shape = sd; | |
// Parameters that affect physics | |
fd.density = 1; | |
fd.friction = 0.3; | |
fd.restitution = 0.5; | |
// Define the body and make it from the shape | |
BodyDef bd = new BodyDef(); | |
bd.type = bodyType; | |
bd.position.set(box2d.coordPixelsToWorld(center)); | |
body = box2d.createBody(bd); | |
body.createFixture(fd); | |
} | |
// This function removes the particle from the box2d world | |
void killBody() { | |
box2d.destroyBody(body); | |
} | |
// Is the particle ready for deletion? | |
boolean done() { | |
// Let's find the screen position of the particle | |
Vec2 pos = box2d.getBodyPixelCoord(body); | |
// Is it off the bottom of the screen? or has been pressed | |
if (pos.y > height+w*h || (dist(pos.x, pos.y, mouseX, mouseY) < 10 && mousePressed)) { | |
killBody(); | |
return true; | |
} | |
return false; | |
} | |
void display() { | |
// We look at each body and get its screen position | |
Vec2 pos = box2d.getBodyPixelCoord(body); | |
// Get its angle of rotation | |
float a = body.getAngle(); | |
pushMatrix(); | |
translate(pos.x,pos.y); | |
rotate(-a); | |
image(getImage(spriteFilename), 0, 0, w, h); | |
popMatrix(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pbox2d.*; | |
import org.jbox2d.collision.shapes.*; | |
import org.jbox2d.common.*; | |
import org.jbox2d.dynamics.*; | |
PBox2D box2d; | |
ArrayList<Element> elements = new ArrayList<Element>(); | |
HashMap<String, PImage> asset = new HashMap<String, PImage>(); | |
void setup() { | |
size(640, 480, P3D); | |
imageMode(CENTER); | |
rectMode(CENTER); | |
//create the physics world | |
box2d = new PBox2D(this); | |
box2d.createWorld(); | |
// We are setting a custom gravity | |
box2d.setGravity(0, -2); | |
} | |
void draw() { | |
background(255); | |
box2d.step(); | |
for (Element e: elements) | |
e.display(); | |
//"pruning function" | |
for (int i = elements.size() -1; i >= 0; i--) { | |
Element e = elements.get(i); | |
if (e.done()) | |
elements.remove(i); | |
} | |
} | |
//predefined Processing method to capture mouse pressed event | |
void mouseReleased() { | |
Element e = new Element(mouseX, mouseY, 50, 50, "tavolo.png", true); | |
elements.add(e); | |
} | |
//this function implement a simple "cache" so Element can store just a filename String | |
PImage getImage(String filename) { | |
if (asset.containsKey(filename)) { | |
return asset.get(filename); | |
} | |
else { | |
PImage newImage = loadImage(filename); | |
asset.put(filename, newImage); | |
return newImage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment