Giter VIP home page Giter VIP logo

box2d-for-processing's Introduction

box2d-for-processing's People

Contributors

freekdb avatar shiffman avatar snunsan avatar thecowcoder avatar wanbinkimoon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

box2d-for-processing's Issues

// Is it off the bottom of the screen?

i'm looking at the examples and i'm at ApplyForceAttractMouse (so the first one).

in the box class there is:

// 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?
if (pos.y > height+w*h) {
killBody();
return true;
}
return false;
}

if you have this line:

if (pos.y > height+w*h) {

w*h can be quite large, w+h should be ok or max(w, h) but w+h will be faster.

EdgeChainDef missing?

Hi there!

I am trying to run the Sensebloom OSCeleton examples (skeleton tracking with a Kinect) and am getting an error:

...cannot find a class or type named "EdgeChainDef"

Here's a relevant excerpt from the Sensebloom "Stickmanetic" Processing sketch:

for (Skeleton s: skels.values()) {
box2d.destroyBody(s.body);
s.body = box2d.world.createBody(s.bd);
s.edges = new EdgeChainDef();
s.edges.setIsLoop(false); // We could make the edge a full loop
s.edges.friction = 0.1; // How much friction
s.edges.restitution = 1.3; // How bouncy
}

It seems like EdgeChainDef is missing from the PBox2D library - if so, do you have any suggestions for how to make this work? Many thanks!

Alison Kotin

AssertionError occurred on box2d's step method

When i add some JointPairs to the world there is a Assertion Error comming from the box2d's step method that called inside draw loop. here is the full code i've tried to understand by nature of code examples.

MainProgram.pde

import shiffman.box2d.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;

Box2DProcessing box2d;

ArrayList<Boundary> boundaries;
ArrayList<JointPair> pairs;


void setup() {
  size(640, 360);

  box2d = new Box2DProcessing(this);
  box2d.createWorld();

  pairs = new ArrayList<JointPair>();
  boundaries = new ArrayList<Boundary>();

  boundaries.add(new Boundary(width/4, height-5, width/2-50, 10));
  boundaries.add(new Boundary(3*width/4, height-50, width/2-50, 10));
}

void draw() {
  background(255);

  box2d.step();

  for (Boundary bounds : boundaries) {
    bounds.display();
  }

  for (int i=pairs.size()-1; i>0; i--) {
    pairs.get(i).display();
    if (pairs.get(i).isPairOffScreen()) {
      pairs.remove(i);
    }
  }
}

void mousePressed() {
  JointPair p = new JointPair(mouseX, mouseY);
  pairs.add(p);
}

Boundary.pde

class Boundary{
  float x,y,w,h;
  
  Body body;
  
  Boundary(float _x, float _y, float _w, float _h){
    x = _x;
    y = _y;
    w = _w;
    h = _h;
    
    //Define the shape
    PolygonShape ps = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    ps.setAsBox(box2dW,box2dH);
    
    //create body definition
    BodyDef bd = new BodyDef();
    bd.type = BodyType.STATIC;
    bd.position.set(box2d.coordPixelsToWorld(x,y));
    body = box2d.createBody(bd);
    
    //put all together
    body.createFixture(ps,1);
  }
  
  void display() {
    fill(0);
    stroke(0);
    rectMode(CENTER);
    rect(x,y,w,h);
  }

}

Box.pde

class Box {
  Body body;
  float w, h;

  Box(float x, float y) {
    w = 10;
    h = 10;

    makeBody(new Vec2(x, y), w, h);
  }

  void display() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    float a = body.getAngle();

    rectMode(CENTER);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(-a);
    fill(127);
    stroke(0);
    strokeWeight(2);
    rect(0, 0, w, h);
    popMatrix();
  }

  boolean isOffScreen() {
    Vec2 pos = box2d.getBodyPixelCoord(body);

    if (pos.x > width || pos.x < 0 || pos.y > height || pos.y <0) {
      killBody();
      return true;
    } else {
      return false;
    }
  }
  
  void killBody(){
    box2d.destroyBody(body);
  }

  void makeBody(Vec2 center, float _w, float _h) {

    //create shape here
    PolygonShape ps = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(_w/2);
    float box2dH = box2d.scalarPixelsToWorld(_h/2);
    ps.setAsBox(box2dW, box2dH);

    //create fixture definition
    FixtureDef fd = new FixtureDef();
    fd.shape = ps;


    fd.density = 1;
    fd.friction = 0.3;
    fd.restitution = 0.5;

    //create body definition 
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.position.set(box2d.coordPixelsToWorld(center));

    body = box2d.createBody(bd);
    body.createFixture(fd);

    //body.setLinearVelocity(new Vec2(random(-5,5), random(2,5)));
    //body.setAngularVelocity(random(-5,5));
  }
}

JointPair.pde

class JointPair {
  Box b1, b2;

  float len;

  JointPair(float x, float y) {
    len = 50;

    b1 = new Box(x, y);
    b2 = new Box(x+5, y+5);

    DistanceJointDef djd = new DistanceJointDef();
    djd.bodyA = b1.body;
    djd.bodyB = b2.body;

    djd.length = box2d.scalarPixelsToWorld(len);

    djd.frequencyHz = 3; 
    djd.dampingRatio = 0.1;

    DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd);
  }

  void display(){
    Vec2 box1 = box2d.getBodyPixelCoord(b1.body);
    Vec2 box2 = box2d.getBodyPixelCoord(b2.body);
    
    stroke(0);
    strokeWeight(2);
    line(box1.x,box1.y,box2.x,box2.y);
    
    b1.display();
    b2.display();
    
  }
  
  boolean isPairOffScreen(){
    if(b1.isOffScreen() && b2.isOffScreen()){
      b1.killBody();
      b2.killBody();
      return true;
    }else{
      return false;
    }
  }
}

The constructor PolygonShape() is undefined

I'm sorry if this looks like a silly question, but I can't wrap my head around it, not for lack of trying. I'm reading the book "The Nature of Code" and started using the physics engine Box2D through Daniel Shiffman's PBox2D library. When trying to run some simple example code from the book (Example 5.1), I keep getting an error message telling me that the constructor PolygonShape() cannot be found. I also noticed that this method does not appear in the examples that are to be found in the library zip file. Was there an update in the PBox2D library that made PolygonShape obsolete? And so is it just the example in the book that is not up to date?

change library name

to follow convention, only core libraries should start with P. This should be

JBox2D-Processing

?

Fixing the default scale factor

Currently, the default scale factor is set to 10. This means that 200 pixels is equal to 20 box2d meters. According to box2d docs, you should keep all objects from 0.1 to 10 meters. My pull request changes the default scale factor to 200, so that 200 pixels is equal to 1 box2d meter. This way, all of the box2d bodies are more likely to stay within the recommended size. Since I am changing the scale factor, my pull request also includes some slight changes to the QuickTest class (gravity is changed to 10.0F instead of 20.0F now that physics objects weigh less, and the simulation size is increased so that the physics objects stay within the recommended 0.1 to 10 meter range).

I also changed the step method to use a timeStep of 1.0F / parent.frameRate instead of a constant 1.0F / 60.0F, and added frameRate(60.0F); to QuickTest. This way, the physics simulation will always run at the same speed regardless of how many frames the computer is running it at. I also changed the velocity iterations to 8 and the position iterations to 3 (because these are box2d's recommended values).

error message/pbox2d?

I keep getting the following error message when trying to run "OSCeleton/Stickmanetic.pde"--any suggestions? I am using your latest box2d buid, latest Processing build, running the OSCeleton from my mac terminal, etc...Thanks!! [email protected]

"Exception in thread "Animation Thread" java.lang.NoSuchMethodError: org.jbox2d.dynamics.World.(Lorg/jbox2d/common/Vec2;)V
at pbox2d.PBox2D.createWorld(PBox2D.java:107)
at pbox2d.PBox2D.createWorld(PBox2D.java:97)
at pbox2d.PBox2D.createWorld(PBox2D.java:91)
at Stickmanetic.setup(Stickmanetic.java:59)
at processing.core.PApplet.handleDraw(PApplet.java:1608)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)

debugdraw

probably should implement something for this

Class name not documented anywhere

I've been reading The Nature of Code and in there the class name is PBox2D. Since the readme refers to that for documentation, it took a while to find that the new class name is Box2DProcessing. It would be useful if this were noted in the readme/book.

[Help] Yet Another Problem with Placing Stuff in Box2D...

Hello people,
Just to introduce the situation, I am a physics teacher and educator and I am also an aspiring artist, so you can imagine the power I felt in my hands when I first discovered Processing from Shiffman's YT channel and then I the videos on Box2D.

So in my skecth I have a gas container with a piston (to show the kinetic theory of gases) and this container position is determined by the Vec2 pos_pote. The problem is that when I try to place it elsewhere from the middle of the scene (width/2, height/2), the rectangles are draw properly but the fixtures of Box2D become a little bit shifted. The farther from the center of the screen pos_pote is, the more shifted the box2D fixtures become. I've been trying to debugg this for over three days, and I still don't know what is going on. Well, see for yourself:

kinetic_gas.zip
[click on the screen to place particles and notice the collision hitboxes are all shifted to the right from the containers perspective, in this case, pos_pote = (width/2+100, height/2), so things are misplaced only on the x axis.]

I did not found anyone on the web with the same problem, so I'm desperate. If anyone could help me it would be totally awesome. Thanks very much,
Daniel.

Missing LICENSE?

First, the standard disclaimer: I am not a lawyer, and this does not constitute legal or financial advice.

Generally, IMHO, it is a good idea to use FSF or OSI Approved Licenses (which can be found here https://www.gnu.org/licenses/licenses.html and here http://opensource.org/licenses/category)

The Free Software Foundation has a useful guide for choosing a license: https://www.gnu.org/licenses/license-recommendations.html

I often reference the Software Freedom Law Center's Legal Primer for both practical and academic purposes (highly recommended): https://www.softwarefreedom.org/resources/2008/foss-primer.html#x1-60002.2

https://tldrlegal.com/ is quite a useful resource for comparing the various FOSS licenses out there once you have some context

To get ahold of actual lawyers/advisors who help FOSS projects, you can reach out to the FSF, SFLC, and OSI at:
[email protected]
[email protected]
[email protected]

Hope this helps, and happy hacking!

Better way to create a customShape from OpenCV blob

I'm working in some examples using Box2D and OpenCV. My question is related to the best way to create a custom shape coming from OpenCV contour.
I already resolved how to find the triangles of the shape using Delaunay. Then the first question is, how to update the custom shape because the triangles are always changing? My first attempt was to create the Box2D shape in one cycle and delete it the next cycle, however, it is not stable, it gives me java.lang.AssertionError. Is there a way to just update the custom shape instead of creating and erasing every two cycles?

No Such Method - just getting started NOC Chapter 5

Having added the JBox2D and Box2D for Processing libraries for NOC chapter 5 I get compile error running the code examples. The libraries are recognised as I can declare my first Box2DProcessing object instance (which is a change from the PBox2D object convention in the book text).
Having declared it and calling createWorld() on it I get a compile error.
"you may be using a library that incompatible with this version of processing".

Below is the simplest version of code that fails to compile. Processing 2.2.1, and current release of Box2DProcessing library and JBox2D (2.2.1). Any help appreciated?

import shiffman.box2d.;
import org.jbox2d.collision.shapes.
;
import org.jbox2d.common.;
import org.jbox2d.dynamics.
;

Box2DProcessing box2d;

void setup() {
size(640, 360);
box2d = new Box2DProcessing(this);
box2d.createWorld();
}

converting between Box2D world coordinates and screen coordinates

According to "The Nature of Code", the coordinate system for Box2D is a standard cartesian one, whereas for Processing it's one with the origin at the top left of the window, with y-coordinate increasing downward and x-coordinate to the right.

However, testing the coordinate conversion functions in pbox2d gives a different picture:

import pbox2d.*;
import org.jbox2d.common.*;

boolean once = false;
PBox2D box2d;

void setup()
{
  box2d = new PBox2D(this);
  box2d.createWorld();
  size(500, 500);
}

void draw()
{
  if (!once)
  {

    Vec2[] screenPositions = new Vec2[3];
    screenPositions[0] = new Vec2(250, 250); // center of screen
    screenPositions[1] = new Vec2(0, 0); // upper left corner
    screenPositions[2] = new Vec2(500, 500); // lower right corner

    for ( int i = 0; i < 3; i++ )
    {
      Vec2 worldPos = box2d.coordPixelsToWorld(screenPositions[i]);
      print("screenPos.x = " + screenPositions[i].x + " screenPos.y = " + screenPositions[i].y);
      println();
      print("worldPos.x = " + worldPos.x + " worldPos.y = " + worldPos.y);
      println();
      println();

    }
    once = true;
  }
}

gives the following output in the message area:

screenPos.x = 250.0 screenPos.y = 250.0
worldPos.x = 20.0 worldPos.y = 20.0

screenPos.x = 0.0 screenPos.y = 0.0
worldPos.x = -5.0 worldPos.y = 45.0

screenPos.x = 500.0 screenPos.y = 500.0
worldPos.x = 45.0 worldPos.y = -5.0

So it seems that a screen coordinate with x = 0 corresponds to a world Position with x coordinate = -5, and a screen coordinate with y = 500 (or, generally speaking, the width of the window, as I've confirmed by testing with different sizes of the window) results in a world position with y coordinate = -5.0. The scaling factor is always 1/10.

Is there a reason why the origin of the Box2D world coordinate system doesn't correspond to the center of the screen? I don't have much familiarity with either box2d or Processing, and I jumped directly into chapter 5 of "The Nature of Code", so apologies if I'm missing something that ought to be common knowledge.

library version

HI Dan,
I am a beginner to processing,and I want to use the kinect with it to make some visual effect,like the video shows: https://vimeo.com/49516871 , but the code doesn't work ,maybe some library version was too old.Do you kown how to make it work again? look for your reply.(I use kinect xbox one with kinect adapter for windows and processing 2.2.1)

Thanks,
Nicole

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.