/** * Based on fisica.FPoly by Ricard Marxer. * http://www.ricardmarxer.com/fisica/ * http://www.gitorious.com/fisica/fisica/blobs/master/src/fisica/FPoly.java * * Copyright (c) 2010 LAFKON/Benjamin Stephan. * * This is free software, and you may redistribute it under the GPL. * This Software comes with absolutely no warranty. * For details see the license (http://www.lafkon.net/gpl.txt) * * http://www.forkable.eu/generators/va300/ * http://www.lafkon.net/what/is/va300 */ // This is the batch-PDF-generating version of the va300 generator. // It generates x variations (specified in i/posternum.i) as printable PDFs. // Also check the live-version made for continuous live projection. See URLs above. import org.jbox2d.common.*; import org.jbox2d.collision.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.dynamics.*; import org.jbox2d.util.nonconvex.*; import processing.pdf.*; import geomerative.*; import java.util.ArrayList; public class FPolyPlus extends fisica.FBody { protected org.jbox2d.util.nonconvex.Polygon m_polygon; protected boolean m_closed; protected ArrayList m_vertices; protected int startX, startY; protected boolean visibility = true; protected RPoint[] pnts; protected PShape drawShape; protected boolean resting = false; protected boolean customStyle = false; public FPolyPlus(){ super(); m_closed = false; m_vertices = new ArrayList(); } // Adds vertices to the shape of the poly. Overwritten from FBody public void vertex(float x, float y){ m_vertices.add(Fisica.screenToWorld(x, y)); } //Overwritten from FBody protected void processBody(Body bd, ShapeDef sd){ org.jbox2d.util.nonconvex.Polygon.decomposeConvexAndAddTo(m_polygon, bd, (PolygonDef)sd); } //Overwritten from FBody protected ShapeDef getShapeDef() { PolygonDef pd = new PolygonDef(); m_vertices.add(new Vec2((Vec2)m_vertices.get(m_vertices.size()-1))); m_closed = true; Vec2[] vertices = new Vec2[m_vertices.size()]; m_vertices.toArray(vertices); m_polygon = new org.jbox2d.util.nonconvex.Polygon(vertices); pd.density = m_density; pd.friction = m_friction; pd.restitution = m_restitution; pd.isSensor = m_sensor; return pd; } // the simplified, joined outline for the collision/physics-processing public void setCollisionShape(RPoint[] points) { pnts = points; for ( int i = 0; i < pnts.length; i++ ) { this.vertex(pnts[i].x+startX, pnts[i].y+startY); } } // the original shape that is actually drawn public void setDrawShape(PShape drawshape) { drawShape = drawshape; } // disable style used in SVG, draw with custom settings public void setCustomStyle(boolean flag) { customStyle = true; } public void setVisible(boolean vis) { visibility = vis; } public void toggleVisible() { visibility = !visibility; } // return whether this body is "more-or-less" resting/still. public boolean isResting() { if((getVelocityY()) < 0.2 && (getVelocityX()) < 0.2 && frameCount > 100 && getAngularVelocity() < 0.2) { resting = true; } else { resting = false; } return resting; } //Overwritten from FBody public void draw(PGraphics canvas) { if(visibility) { canvas.pushStyle(); canvas.pushMatrix(); canvas.translate(getX(), getY()); canvas.rotate(getRotation()); canvas.ellipseMode(PConstants.CENTER); canvas.rectMode(PConstants.CENTER); appletFillStroke(canvas); if(customStyle) { drawShape.disableStyle(); setStrokeWeight(0.15); canvas.fill(255); canvas.stroke(max(255-frameCount*2,0)); } else { setStrokeWeight(0.2); canvas.fill(255); canvas.stroke(0); canvas.smooth(); } // draw DrawShape if(drawShape != null) { canvas.pushMatrix(); canvas.translate(startX, startY); drawShape.draw(canvas); canvas.popMatrix(); } canvas.popMatrix(); canvas.popStyle(); }//visibility }//end draw }