We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › boxwrap2d - compound shapes possible
Page Index Toggle Pages: 1
boxwrap2d - compound shapes possible? (Read 684 times)
boxwrap2d - compound shapes possible?
Oct 29th, 2008, 10:34pm
 
I'm trying to get my head around what I hope is a simple task in boxwrap2d.

My aim is to have graphical text in a sketch where the individual letters are effected by physical forces (specifically gravity).

My thinking is that the way to do this is to create compound shapes that approximate the outlines of the individual letters in the font I'm using. From there I need to figure out how to map the text onto the jbox shapes, but one thing at a time.

I initially tried playing with different joint types to tie together a series of rectangular bodies to make up the letter outlines. This seemed worth a try as the joint classes are easily usable and documented for boxwrap2d. However none of my tests have come up with a rigid composite shape, so I'm guessing I need to figure out how to use compound shapes (http://www.jbox2d.org/demos/compoundshapes.html) in boxwrap2d.

Any suggestions would be much appreciated. Thanks, Patrick
Re: boxwrap2d - compound shapes possible?
Reply #1 - Oct 29th, 2008, 10:49pm
 
Here's an example of the jointed method I tried

Code:

import org.jbox2d.testbed.tests.*;
import org.jbox2d.testbed.*;
import org.jbox2d.dynamics.contacts.*;
import org.jbox2d.p5.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.common.*;
import org.jbox2d.util.blob.*;
import org.jbox2d.collision.*;
import org.jbox2d.testbed.timingTests.*;
import org.jbox2d.dynamics.joints.*;

// To work around threading problems with size
int count = 0;

// Physics things we must store
Physics physics;
Body body, body2;

void setup() {
size(640,480,P3D);
frameRate(60);
}

void draw() {
background(0);

//This is a hack to make sure that the sketch size is
//fully initialized before initializing the scene,
//which depends on the width and height values.
//Once Processing 1.0 is released, this should no longer
//be necessary.
++count;
if ( count == 10 ) {
initScene();
}
}

void initScene() {
physics = new Physics(this, width, height);
physics.setDensity(1.0f);
Body b1 = null;
Body b2 = null;


body = physics.createRect(200, 10, 260, 30);

body2 = physics.createRect(220, 30, 240, 90);


physics.createDistanceJoint(body, body2, 220, 30, 220, 30);

physics.createDistanceJoint(body, body2, 240, 30, 240, 30);

physics.createDistanceJoint(body, body2, 230, 20, 230, 40);
}
Re: boxwrap2d - compound shapes possible?
Reply #2 - Oct 30th, 2008, 8:58pm
 
I think I've answered my own question. I have no idea if this is the proper way to do a compound shape, but it seems to work.

Code:

/*
Falling Letters
Patrick Dinnen 2008
using jbox2d library to add real gravity to text

v0.1
- basic outline seems to work
*/

import org.jbox2d.testbed.tests.*;
import org.jbox2d.testbed.*;
import org.jbox2d.dynamics.contacts.*;
import org.jbox2d.p5.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.common.*;
import org.jbox2d.util.blob.*;
import org.jbox2d.collision.*;
import org.jbox2d.testbed.timingTests.*;
import org.jbox2d.dynamics.joints.*;

// To work around threading problems with size
int count = 0;

// Physics things we must store
Physics physics;
Body body, body2;

void setup() {
size(640,480,P3D);
frameRate(60);
}

void draw() {
background(0);

//This is a hack to make sure that the sketch size is
//fully initialized before initializing the scene,
//which depends on the width and height values.
//Once Processing 1.0 is released, this should no longer
//be necessary.
++count;
if ( count == 10 ) {
initScene();
}
}

void initScene() {
physics = new Physics(this, width, height);

Vec2 v;

BodyDef bd;

PolygonDef pd;

bd = new BodyDef();

Body teeBody = physics.getWorld().createBody(bd);

pd = new PolygonDef();

// create a rectangular polygon to represent the t's crossbar
v = physics.screenToWorld(100,200);
pd.addVertex(v);
v = physics.screenToWorld(100,220);
pd.addVertex(v);
v = physics.screenToWorld(160,220);
pd.addVertex(v);
v = physics.screenToWorld(160,200);
pd.addVertex(v);

pd.density = 1.0;

// add the crossbar to the t's body
teeBody.createShape(pd);


// create a rectangular polygon to represent the t's vertical
pd = new PolygonDef();

v = physics.screenToWorld(120,220);
pd.addVertex(v);
v = physics.screenToWorld(120,280);
pd.addVertex(v);
v = physics.screenToWorld(140,280);
pd.addVertex(v);
v = physics.screenToWorld(140,220);
pd.addVertex(v);

pd.density = 1.0;

// add the vertical to the t's body
teeBody.createShape(pd);

// give the body mass based on the shapes it's made up of
teeBody.setMassFromShapes();
}

Page Index Toggle Pages: 1