Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to constrain body movement when using box2d ?

$
0
0

Hi, I'm trying to limit the movement of my hero in a 2d game using box2d. Here is the MCVE of what I'm trying to do. As in my previous post, 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.

//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 with boundary() function :

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.
  void boundary() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    if (pos.x < r) {
    pos.x = r;
    }
    if (pos.x > width-r) {
    pos.x = width-r;
    }
    if (pos.y < r) {
    pos.y = r;
    }
    if (pos.y > height-r) {
    pos.y = height-r;
    }
  }

  // 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.


Viewing all articles
Browse latest Browse all 2896

Trending Articles