Annonce

>>> Bienvenue sur codelab! >>> Première visite ? >>> quelques mots sur codelab //// une carte des membres//// (apéros) codelab


#1 2011-12-07 10:38:39 PBox2d

cgiles
membre
Lieu: Est de la France
Date d'inscription: 2005-05-25
Messages: 329

PBox2d



Bonjour,
Je découvre box2d et les différent wrapper processing qui existe pour celui ci.
Alors que je faisais un test faisant évoluer la gravité, je me suis rendu compte que une fois les objets immobiles, ils se retrouvent collés a la paroi, sans possiblité de les décoller...

Est ce que quelqu'un a déja eu se soucis ?
code principal :

import pbox2d.*;
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.*;

ArrayList<Box> boxes;
PBox2D box2d;
Boundary bord1;
Boundary bord2;
Boundary bord3;
Boundary bord4;
Box b1;
Box b2;
Box b3;
void setup() {
  size(400, 300);
  smooth();
  // Create ArrayLists	
  boxes = new ArrayList<Box>();
  box2d = new PBox2D(this);
  box2d.createWorld();
  bord1 = new Boundary(width/2, height-10, width, 10);
  bord2 = new Boundary(width/2, 0, width, 10);
   bord3 = new Boundary(0, height/2, 10, height);
      bord4 = new Boundary(width, height/2, 10, height);
  // We are setting a custom gravity
  box2d.setGravity(0, -20);
  b1 = new Box(100, 50, 1);
  b2 = new Box(200, 50, 50);
  b3 = new Box(300, 50, 100);
}

void draw() {
  background(255);
  box2d.step();
  bord1.display();
  bord2.display();
  bord3.display();
  // When the mouse is clicked, add a new Box object

  b1.display();
  b2.display();
  b3.display();
  if (mousePressed) {
    box2d.setGravity(0, map(mouseY, 0, height, 50, -50));
  }
  /* if (mousePressed) {
   Box p = new Box(mouseX,mouseY);
   boxes.add(p);
   }
   
   // Display all the boxes
   for (Box b: boxes) {
   b.display();
   }*/
}

Classe Box

// A rectangular box
class Box  {
 
  // We need to keep track of a Body and a width and height
  Body body;
 
  float w;
  float h;
  Box(float x, float y,float poids) {
 
    w = random(4,16);
    h = random(4,16);
 
    // Define a polygon (this is what we use for a rectangle)
    PolygonDef sd = new PolygonDef();
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    sd.setAsBox(box2dW, box2dH);
 
    // Parameters that affect physics
    sd.density = poids;
    sd.friction = 0.3;
    sd.restitution = 0.5;
 
    // Define the body and make it from the shape
    BodyDef bd = new BodyDef();
    Vec2 center = new Vec2(x,y);
    bd.position.set(box2d.coordPixelsToWorld(center));
 
    body = box2d.createBody(bd);
    body.createShape(sd);
    body.setMassFromShapes();
 
    // Give it some initial random velocity
    body.setLinearVelocity(new Vec2(random(-5,5),random(2,5)));
    body.setAngularVelocity(random(-5,5));
 }
  void display() {
    // We look at each body and get its screen position
    Vec2 pos = box2d.getBodyPixelCoord(body);
    // Get its angle of rotation
    float a = body.getAngle();
 
    rectMode(CENTER);
    pushMatrix();
    translate(pos.x,pos.y);
    rotate(-a);
    fill(175);
    stroke(0);
    rect(0,0,w,h);
    popMatrix();
  }
}

Classe Boundaries

// The Nature of Code
// <http://www.shiffman.net/teaching/nature>
// Spring 2010
// PBox2D example

// A fixed boundary class

class Boundary {

  // A boundary is a simple rectangle with x,y,width,and height
  float x;
  float y;
  float w;
  float h;
  // But we also have to make a body for box2d to know about it
  Body b;

  Boundary(float x_,float y_, float w_, float h_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;

    // Figure out the box2d coordinates
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    Vec2 center = new Vec2(x,y);

    // Define the polygon
    PolygonDef sd = new PolygonDef();
    sd.setAsBox(box2dW, box2dH);
    sd.density = 0;    // No density means it won't move!
    sd.friction = 0.1;

    // Create the body
    BodyDef bd = new BodyDef();
    bd.position.set(box2d.coordPixelsToWorld(center));
    b = box2d.createBody(bd);
    b.createShape(sd);
  }

  // Draw the boundary, if it were at an angle we'd have to do something fancier
  void display() {
    fill(0);
    stroke(0);
    rectMode(CENTER);
    rect(x,y,w,h);
  }

}

Hors ligne

 

#2 2011-12-07 16:32:38 Re : PBox2d

emoc
@#@*$
Lieu: Quimper
Date d'inscription: 2008-01-28
Messages: 1576
Site web

Re: PBox2d



Salut, je viens d'essayer ton code. J'ai l'impression que ce phénomène est du aux valeurs physiques attribuées aux objets. Par exemple, regarde ce que ça fait quand tu passes la valeur de sd.friction à 0 dans la classe Boundary, les objets continuent à bouger!

Hors ligne

 

#3 2011-12-09 14:59:15 Re : PBox2d

cgiles
membre
Lieu: Est de la France
Date d'inscription: 2005-05-25
Messages: 329

Re: PBox2d



Oui j'avais vu cela aussi, mais d'une manière pas très réelle.
J'ai testé BoxWrap2d, et elle marche beaucoup mieux, et simple plus simple que le wrapper de shiffman.
Mais je pense que le top serait de directement utiliser jbox2d, malheureusement, je ne le trouve pas déja compiler...
A suivre

Hors ligne

 

fil rss de cette discussion : rss

Pied de page des forums

Powered by FluxBB

codelab, graphisme & code : emoc / 2008-2025