How to limit body movement in Processing Box2d ?

Discuss issues specific the Java port of Box2D
Sam Marchand
Posts: 2
Joined: Tue Oct 24, 2017 9:53 am

How to limit body movement in Processing Box2d ?

Postby Sam Marchand » Tue Oct 24, 2017 10:11 am

Hi, I'm trying to limit the movement of my hero in a 2d game using box2d in Processing 3. Here is the MCVE of what I'm trying to do. No error pops up but my hero can still roam outside the screen. I'm still new to box2d, any insight/solution would be appreciated.

Code: Select all

//I wont use all of these for this MCVE,
//but its usually what I import for Box2d and collisions.
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.*;
 
// A reference to our box2d world
Box2DProcessing box2d;
 
//Declaring my hero
Hero hero;
 
void setup() {
  size(400, 300);
  smooth();
 
  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();
 
  // We are setting a custom gravity
  box2d.setGravity(0, -0.0005);
 
  //Initializing hero
  hero = new Hero(20, 20, 20);
}
 
void draw() {
  background(255);
 
  // Thru time
  box2d.step();
 
  //Show and move Hero
  hero.display();
  hero.mover();
  hero.boundary(); //This is what I call to limit the range of my hero
}


And here is my hero class :

Code: Select all

class Hero {
  Body body;
  float r;
 
  Hero(float x, float y, float r_) {
    r = r_;
    // This function puts the hero in the Box2d world
    makeBody(x, y, r);
    body.setUserData(this);
  }
 
  void display() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    pushMatrix();
    translate(pos.x, pos.y);
    fill(0, 44, 188);
    noStroke();
    ellipse(0, 0, r*2, r*2);
    popMatrix();
  }
 
  void mover() {
    Vec2 move = new Vec2(0, 0);
    Vec2 pos = body.getWorldCenter();
 
    if ((keyPressed == true) && ((key == 'w') || (key == 'W'))) {
      move.y += 100;
    }
    if ((keyPressed == true) && ((key == 's') || (key == 'S'))) {
      move.y -= 100;
    }
    if ((keyPressed == true) && ((key == 'a') || (key == 'A'))) {
      move.x -= 100;
    }
    if ((keyPressed == true) && ((key == 'd') || (key == 'D'))) {
      move.x += 100;
    }
    body.applyForce(move, pos);
  }
 
  //Here is the method that would limit the movement of my hero if it worked.
  //Box2d's origin is in the middle of the screen but Processing's origin is top left corner.
  void boundary() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    if (pos.x < r) {
    pos.x = -width/2;
    }
    if (pos.x > width-r) {
    pos.x = width/2;
    }
    if (pos.y < r) {
    pos.y = -height/2;
    }
    if (pos.y > height-r) {
    pos.y = height/2;
    }
  } 
 
  // Here's our function that adds the particle to the Box2D world
  void makeBody(float x, float y, float r) {
    // Define a body
    BodyDef bd = new BodyDef();
    // Set its position
    bd.position = box2d.coordPixelsToWorld(x, y);
    bd.type = BodyType.DYNAMIC;
    body = box2d.createBody(bd);
 
    // Make the body's shape a circle
    CircleShape cs = new CircleShape();
    cs.m_radius = box2d.scalarPixelsToWorld(r);
 
    FixtureDef fd = new FixtureDef();
    fd.shape = cs;
    // Parameters that affect physics
    fd.density = 1;
    fd.friction = 0.01;
    fd.restitution = 0.3;
 
    // Attach fixture to body
    body.createFixture(fd);   
  }
}


NB : I would like to avoid creating static walls since I have other objects that need to move outside the screen range.

Sam Marchand
Posts: 2
Joined: Tue Oct 24, 2017 9:53 am

Re: How to limit body movement in Processing Box2d ?

Postby Sam Marchand » Fri Oct 27, 2017 8:13 am

I have a found way to limit body movement. Here's my new boundary() function:

Code: Select all

  void boundary() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    if (pos.x < r) {
    pos.x = r;
    box2d.destroyBody(body);
    makeBody(pos.x, pos.y, r);
    }
    if (pos.x > width-r) {
    pos.x = width-r;
    box2d.destroyBody(body);
    makeBody(pos.x, pos.y, r);
    }
    if (pos.y < r) {
    pos.y = r;
    box2d.destroyBody(body);
    makeBody(pos.x, pos.y, r);
    }
    if (pos.y > height-r) {
    pos.y = height-r;
    box2d.destroyBody(body);
    makeBody(pos.x, pos.y, r);
    }
  } 


The trick was to destroy and make body at the desired limit.


Return to “Java”



Who is online

Users browsing this forum: No registered users and 1 guest