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.
IndexProcessing DevelopmentLibraries,  Tool Development › BoxWrap2d physics library - seeking feedback
Pages: 1 2 3 
BoxWrap2d physics library - seeking feedback (Read 20618 times)
Re: BoxWrap2d physics library - seeking feedback
Reply #30 - Jul 7th, 2009, 2:55pm
 
pla, these are interesting questions.
Since I am learning to use JBox2D via BoxWrap2D, I tried to answer them, as they are useful.

The first one wasn't too hard, at least if you had a look at BoxWrap2D implementation: it is a rather thin wrapper around JBox2D, trying to hide some complexities like world coordinates. But access to getPosition() is raw, expressed precisely in world coordinates: you have to convert them in screen coordinates.

The second one is harder: MouseJoint is implemented in JBox2D but not yet in BoxWrap2D, so I had to write my own wrapper. Usage isn't obvious, but I found an AS3 implementation and I guessed (I don't know AS3... but it isn't so hard to understand) how to adapt to Processing.
The results of the experiments are as follow: I planned to show that in a tutorial I am writing, but I share it early.
Code:
import org.jbox2d.common.*;
import org.jbox2d.collision.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.dynamics.contacts.*;
// BoxWrap2D
import org.jbox2d.p5.*;

// A reference to the physics engine
Physics physics;
// Reference to the world as we need it for more advanced stuff...
World m_world;
// Some elements we need
MouseJoint m_mouseJoint;
Body movedBody;
Body circle, ground;

void setup()
{
 // Medium sized scene
 size(640, 480);
 // Physics is computed 60 times per second, so let's draw at same rate
 frameRate(60);
 // Nicer graphisc
 smooth();

 PFont f = loadFont("Verdana-12.vlw");
 textFont(f);

 // Set up everything physics
 InitScene();
 // And add object to the scene
 CreateObjects();
}

void draw()
{
 // Not much to do here, most drawing is handled by BoxWrap2D
 background(255);
 // Show position of latest moved body
 Vec2 posW;
 if (movedBody == null)
 {
   posW = circle.getPosition();
 }
 else
 {
   posW = movedBody.getPosition();
 }
 Vec2 posS = physics.worldToScreen(posW);
 String position = String.format("Pos: %.2f, %.2f", posS.x, posS.y);
 text(position, 10, 20);
}

void mousePressed()
{
 if (m_mouseJoint == null)
 {
   movedBody = GetBodyAtMouse();
   if (movedBody != null)
   {
m_mouseJoint = createMouseJoint(movedBody, mouseX, mouseY);
   }
 }
}

void mouseReleased()
{
 if (m_mouseJoint != null)
 {
   m_world.destroyJoint(m_mouseJoint);
   m_mouseJoint = null;
 }
}

void mouseDragged()
{
 if (m_mouseJoint != null)
 {
   Vec2 v = physics.screenToWorld(mouseX, mouseY);
   m_mouseJoint.setTarget(v);
 }
}

void keyPressed()
{
 // Can be used to reset the sketch, for example
 physics.destroy();
 physics = null;
 InitScene();
}

void InitScene()
{
 // Set up the engine with the sketch's dimensions
 physics = new Physics(this, width, height);
 m_world = physics.getWorld();
 ground = physics.createRect(
20, height - 40,
width - 20, height - 20
 );
 physics.setDensity(1.0);
}

void CreateObjects()
{
 // Middle of the world
 float hw = width / 2.0;
 float hh = height / 2.0;
 // A round object in the middle of the scene (center coordinates, radius)
 circle = physics.createCircle(hw, hh, 50.0);
 // And two rectangles not far (coordinates of top-left, and bottom-right corners)
 physics.createRect(
hw - 150, hh - 50,
hw - 75, hh + 50
 );
 physics.createRect(
hw + 75, hh - 40,
hw + 175, hh + 40
 );
 // A polygon, defined by a list of vertices
 physics.createPolygon(
hw + 150, hh - 100,
hw, hh - 150,
hw - 150, hh - 100
 );
}

MouseJoint createMouseJoint(Body body, float x, float y)
{
 Vec2 v = physics.screenToWorld(x, y);
 MouseJointDef mjd = new MouseJointDef();
 mjd.body1 = body; // Not used, avoid a NPE
 mjd.body2 = body;
 mjd.target = v;
 mjd.maxForce = 3000.0 * body.m_mass;
 return (MouseJoint) m_world.createJoint(mjd);
}

// Idea taken from source seen at The Stem > Box2D Joints #2 - Revolute Joints <http://blog.thestem.ca/archives/102>
Body GetBodyAtMouse()
{
 // Create a small box at mouse point
 Vec2 v = physics.screenToWorld(mouseX, mouseY);
 AABB aabb = new AABB(new Vec2(v.x - 0.001, v.y - 0.001), new Vec2(v.x + 0.001, v.y + 0.001));
 // Look at the shapes intersecting this box (max.: 10)
 org.jbox2d.collision.Shape[] shapes = m_world.query(aabb, 10);
 if (shapes == null)
   return null;  // No body there...
 for (int is = 0; is < shapes.length; is++)
 {
   org.jbox2d.collision.Shape s = shapes[is];
   if (!s.m_body.isStatic())  // Don't pick static shapes
   {
// Ensure it is really at this point
if (s.testPoint(s.m_body.getXForm(), v))
 return s.m_body; // Return the first body found
   }
 }
 return null;
}
Re: BoxWrap2d physics library - seeking feedback
Reply #31 - Jul 9th, 2009, 3:07am
 
ahh of course ! thanks alot fot the hint !...you know, somebody should to do a docu-app, like for shoes! would be a fun project to do I think!
Re: BoxWrap2d physics library - seeking feedback
Reply #32 - Jul 9th, 2009, 4:14am
 
pla wrote on Jul 9th, 2009, 3:07am:
you know, somebody should to do a docu-app, like for shoes!

Uuuu  Shocked I don't see what you mean there.

I take the opportunity to plug in my tutorial thread I promised here: JBox2D with BoxWrap2D Tutorial.
Re: BoxWrap2d physics library - seeking feedback
Reply #33 - Jul 9th, 2009, 5:26am
 
Quote:
you know, somebody should to do a docu-app, like for shoes!
Uuuu  Shocked I don't see what you mean there.

Shoes is a GUI-toolkit, I would like to post a link here to show what I intended for,but I can't post links (I can't with less then 5 posts  Cry), so if you're interested you can google for: ruby shoes. Then in news,you see that app. It's a interactive manual to learn shoes, really cool and helpful ! Smiley

PhiLho  wrote on Jul 9th, 2009, 4:14am:
I take the opportunity to plug in my tutorial thread I promised here: ...

Woa, neat ! I'll take that tour today and translate it to ruby for me Smiley THX !
Re: BoxWrap2d physics library - seeking feedback
Reply #34 - Jul 10th, 2009, 1:37pm
 
pla wrote on Jul 10th, 2009, 1:16pm:
why do you use createMouseJoint without the physics reference! how this can work ..or is this just a proposal


In my sketch, which works for me, the physics reference is global to the sketch.

"i can't get the mousejoint to run"
You have a crash, an error message, an exception, or you just can't drag shapes
Ensure you have the latest BoxWrap2D (with JBox2D 2.0.1).
Re: BoxWrap2d physics library - seeking feedback
Reply #35 - Jul 10th, 2009, 1:55pm
 
nevermind, I'm just stupid Roll Eyes
Re: BoxWrap2d physics library - seeking feedback
Reply #36 - Sep 4th, 2009, 10:45pm
 
I'm trying to set this up and getting a simple error: "The package org.jbox2d does not exist. You might be missing a library."

Tried copying boxwrap2d.jar into my libraries dir, but this had no effect.

Am I doing something wrong or is this a bug?

I am a total noob to both java and processing!

Thanks,
=T=
Re: BoxWrap2d physics library - seeking feedback
Reply #37 - Sep 5th, 2009, 5:07am
 
Where is "your library dir"
Did you followed the instructions at Libraries
Re: BoxWrap2d physics library - seeking feedback
Reply #38 - Sep 10th, 2009, 5:58pm
 
Alright, I got it runing, sort of. I had to follow tacitdynamite's suggestion and comment out the "import" statements for "testbed" and "blob"
Maximum Vertices?
Reply #39 - Jan 13th, 2010, 10:00am
 
Does anyone know hot to increase the maximum vertices number for creating polygons?

I get an out of bounds error when calling createPolygon() if I have > 8 vertices.


Loads of searching on the Box2D forums seems to reckon there is a variable in a class source header file that can be changed:
Quote:
"The maximum number of vertices is fixed by setting b2_maxPolyVertices."


.. but I can't find any way to change the maximum vertices in Jbox2D source.
Any ideas how to increase the maximum vertex count in Jbox2D?
Thanks
Pages: 1 2 3