/** * 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 live-version, made for continuous live projection! // For a pdf-outputting-version check the URLs above. // NOTE: These customized FPolyMinus and FPolyPlus-Classes would not be necessary // the way they are for this live-version. They are relicts from the pdf-output version. // A overwritten FBox would be the easier and more appropriate way for this live-version. import org.jbox2d.common.*; import org.jbox2d.collision.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.dynamics.*; import org.jbox2d.util.nonconvex.*; import geomerative.*; import java.util.ArrayList; public class FPolyMinus 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; /** * Constructs a polygonal body that can be added to a world. It creates an empty polygon, before adding the blob to the world use {@link #vertex(float,float) vertex} to define the shape of the polygon. */ public FPolyMinus(){ super(); m_closed = false; m_vertices = new ArrayList(); startX = mouseX; startY = mouseY; } /** * Adds vertices to the shape of the poly. This method must called before adding the body to the world. * * @param x x coordinate of the vertex to be added * @param y y coordinate of the vertex to be added */ public void vertex(float x, float y){ /* if (m_vertices.size() >= Settings.maxPolygonVertices ) { throw new IllegalArgumentException("The maximum number of vertices allowed for polygon bodies is: " + Settings.maxPolygonVertices); } */ m_vertices.add(Fisica.screenToWorld(x, y)); } 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); } } public void setDrawShape(PShape drawshape) { drawShape = drawshape; } public void setVisible(boolean vis) { visibility = vis; } public void toggleVisible() { visibility = !visibility; } public boolean isResting() { if((getVelocityY()) < 0.2 && (getVelocityX()) < 0.2 && frameCount > 100 && getAngularVelocity() < 0.2) { resting = true; } else { resting = false; } //return resting; return false; } protected void processBody(Body bd, ShapeDef sd){ org.jbox2d.util.nonconvex.Polygon.decomposeConvexAndAddTo(m_polygon, bd, (PolygonDef)sd); } 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; } 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); //==================================================== canvas.fill(255); canvas.stroke(0); setStrokeWeight(0.2); canvas.smooth(); // draw DrawShape ---------- if(drawShape != null) { canvas.pushMatrix(); canvas.translate(startX, startY); drawShape.draw(canvas); canvas.popMatrix(); /* // draw CollisonShape------------- setStrokeWeight(1); //canvas.noFill(); canvas.fill(255,255,255); canvas.stroke(0,255,0); canvas.beginShape(); for(int i = 0; i