JBox2D question

Is this still the right place to talk about jBox2d?

If so... I would like to use jbox2d for collision detection in my game engine, with out the physics. Is that posible?

What I want the user to be able to do is, when they create a object, set wether or not they want it to be controled by the physics engine. However even if they chose for the object to be non-physics, the object can still collide with physics and non-physics objects. Non-physics objects are not nesaseraly static, they can be moved by the user.

my blog about the game engine is here: http://glass-flame.blogspot.com
BBjam wrote:
Is this still the right place to talk about jBox2d?

Sure is.
Quote:
If so... I would like to use jbox2d for collision detection in my game engine, with out the physics. Is that posible?

What I want the user to be able to do is, when they create a object, set wether or not they want it to be controled by the physics engine. However even if they chose for the object to be non-physics, the object can still collide with physics and non-physics objects. Non-physics objects are not nesaseraly static, they can be moved by the user.

It's definitely possible. You'll probably want to still represent your non-moving objects as physics objects, though, as otherwise things will get pretty messy. You might need to switch things between dynamic to static when they are being moved/locked into place.

One thing, though, that confuses me - when you say non-physics objects should still "collide," what do you mean by that? If there is a collision response, then doesn't that mean that the object is physical?

If you're saying that you want to use the collision detection and not apply the collision response, that's another story; it's also possible, as the collision package shouldn't have dependencies outside of itself, but I don't know if anyone has actually tried to do it. At the moment I don't have to time to try, but if that's what you're shooting for let me know and I can at least help figure out what to do.
by still collide I meen that if a physics object B hits a non-physics object A it will ether bounce off (A has infinite mass) or pass through (A is a sensor), ether way it will still generate a collision event in my game engine.

Right now I am writing a little test to learn how to use jBox2D, and I ran into a problem, namely that calling step(10/30, 10) causes my sphere to move about -0.011111112 in the y direction, and no further, no mater how many times I call it. The sphere is not falling out of the AABB, it has mass, gravity is (0,-10).
BBjam wrote:
by still collide I meen that if a physics object B hits a non-physics object A it will ether bounce off (A has infinite mass) or pass through (A is a sensor), ether way it will still generate a collision event in my game engine.

Right, then you'll want to switch things back and forth from static bodies or sensors to regular bodies as needed. I haven't done too much of that myself, so let me know if there are any problems you run into.
BBjam wrote:
Right now I am writing a little test to learn how to use jBox2D, and I ran into a problem, namely that calling step(10/30, 10) causes my sphere to move about -0.011111112 in the y direction, and no further, no mater how many times I call it. The sphere is not falling out of the AABB, it has mass, gravity is (0,-10).

Not sure if this is the issue, might have just been a typo, but 10/30 will evaluate to 0, since you're doing integer math there - what you want would be 10.0f/30 (or more likely, 1.0f/30, which is what I assume you meant). BTW, I would probably suggest using 1.0f/60.0f as your time step, you'll get better results in general.

Assuming that was just a typo, do you have some code I could take a look at? I've never seen something like that happen before, so it would be useful to see what else is going on in your code.

Quote:
BTW. wouldn't it be a good idea to move discation to the sourceforge forums that came with the jBox2D project?

Possibly, though honestly at the moment there are not too many people using/discussing JBox2d, so I'm not sure about whether it would be worth setting up a new forum. I think a more likely case would be that if more people start discussing it, we may branch a sub-forum here rather than setting up another (that's what the AS3 version has done). I'm not sure if there's quite the critical mass to make that worthwhile yet, though.
Right now I am trying to use solve(TimeStep), which is the same as step(), right? what should I set dt and dtRatio to? nothing I have tried so far has worked(same problem a before).
BBjam wrote:
Right now I am trying to use solve(TimeStep), which is the same as step(), right? what should I set dt and dtRatio to? nothing I have tried so far has worked(same problem a before).

Nope, it's not. Sorry about that - that function should be private, as it's internal, and it will be in the next version. In the past the C++ version had all of these things public, and even though it now has everything "locked down," we haven't updated the Java access modifiers yet. That's very high on my todo list, though, so it will definitely show up in the next version. My bad. 🙂

Use World::step(timeStep, iterations) to step your world, instead.
here is the code I am using:
Code:
package src;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JFrame;

import org.jbox2d.collision.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.common.*;

public class BoxFrame extends JFrame implements WindowListener{

   /**
    *
    */
   private static final long serialVersionUID = -6968929917917803979L;
   
   World world;
   Vector<Body> bodys;
   Ellipse2D circle;
   RepaintThread th;
   BufferedImage buff;
   final static float timeStep = 1.0f / 60.0f;
   final static int iterations = 10;

   
   public BoxFrame()
   {
      super("Box Frame");
      
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      buff = new BufferedImage(300,300,BufferedImage.TYPE_INT_ARGB);
      this.setBounds(300, 200, 300, 300);
      AABB wab = new AABB(new Vec2(-50,50), new Vec2(50,-50));
      world = new World(wab,new Vec2(0,-10),true);
      
      bodys = new Vector<Body>(100);
      
/*        { // Floor
            PolygonDef sd = new PolygonDef();
            sd.setAsBox(50.0f, 10.0f);

            BodyDef bd = new BodyDef();
            bd.position = new Vec2(0.0f, -10.0f);
            Body tem = world.createBody(bd);
            bodys.add(tem);
            tem.createShape(sd);
        }*/
      
      
      CircleDef   cd = new CircleDef();
      cd.density = 1.0f;
      cd.friction = 0.1f;
      cd.radius = 1.0f;
      cd.localPosition.set(0, 0);
      cd.restitution = 0.5f;
      
      
      BodyDef bd = new BodyDef();
      bd.position = new Vec2(0,10);
      bd.allowSleep = false;
      
      
      Body b = world.createBody(bd);
      b.createShape(cd);
      b.setMassFromShapes();
      
      bodys.add(b);
      
      circle = new Ellipse2D.Float();
      circle.setFrame(0,0,1,1);
      
      th = new RepaintThread(this,world);
      th.start();
   }
   public static void main(String[] cheese)
   {
      BoxFrame frame = new BoxFrame();
      frame.setVisible(true);
   }
   public void paint(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) buff.getGraphics();
      g2.setColor(Color.WHITE);
      g2.fillRect(0, 0, getWidth(), getHeight());
      g2.setColor(Color.BLACK);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      
      for(Body b : bodys)
      {
         float a = b.getAngle();
         Vec2  p = b.getPosition();
         g2.setColor(Color.BLACK);
         
         AffineTransform t = AffineTransform.getTranslateInstance(0, 0);
         t.concatenate(AffineTransform.getTranslateInstance(getWidth()/2, getHeight()/2));
         t.concatenate(AffineTransform.getScaleInstance(10, 10));
         
         g2.setTransform(t);
         g2.drawRect(-5,-5, 10, 10);
         t.concatenate(AffineTransform.getRotateInstance(a));
         t.concatenate(AffineTransform.getTranslateInstance(p.x, p.y));
         System.out.println("\npos " + p.x + ',' + p.y);
         System.out.println("angle " + a);
         //System.out.println(b.getMass());
         g2.setTransform(t);
         g2.draw(circle);
         g2.setTransform(AffineTransform.getTranslateInstance(0, 0));
      }
      g.drawImage(buff, 0, 0, null);
   }
   @Override
   public void windowActivated(WindowEvent e) {
      // TODO Auto-generated method stub
      
   }
   @Override
   public void windowClosed(WindowEvent e) {
      // TODO Auto-generated method stub
      
   }
   @Override
   public void windowClosing(WindowEvent e) {
      // TODO Auto-generated method stub
      th.end();
   }
   @Override
   public void windowDeactivated(WindowEvent e) {
      // TODO Auto-generated method stub
      
   }
   @Override
   public void windowDeiconified(WindowEvent e) {
      // TODO Auto-generated method stub
      
   }
   @Override
   public void windowIconified(WindowEvent e) {
      // TODO Auto-generated method stub
      
   }
   @Override
   public void windowOpened(WindowEvent e) {
      // TODO Auto-generated method stub
      
   }
   
   private class RepaintThread extends Thread
   {
      Component comp;
      boolean go;
      World world;
      
      RepaintThread(Component c, World w)
      {
         world = w;
         comp = c;
         go = true;
      }
      public void run()
      {
         while(go)
         {
            try{
               world.step(timeStep, iterations);
               comp.repaint();
               sleep(1000/30);
            }
            catch (Exception e) {
               e.printStackTrace();
            }
         }
      }
      public void end()
      {
         go = false;
      }
   }
}


When I run it the sphere moves to 0.0,9.997222 from 0,10 and stays there.
I don't know what the java conventions are, but there is no way I can see that your world aabb, wab, is valid. This is causing your body to freeze on frame 1, I guess.

BTW would you prefer if I moved this query to a separate thread?
BorisTheBrave wrote:
I don't know what the java conventions are, but there is no way I can see that your world aabb, wab, is valid. This is causing your body to freeze on frame 1, I guess.

Yup, that's the problem, it should be
Code:
AABB wab = new AABB(new Vec2(-50,-50), new Vec2(50,50));
Maybe in the next rev I should rework the AABB constructor so it automatically fixes things like this, since it does seem reasonable enough to specify an AABB by marking any two opposite corners.
Quote:
BTW would you prefer if I moved this query to a separate thread?

Sure, that works for me, since it's not specifically Java related.
Sorry to waste time, I thought that I was spessifing the upper-left and lower-right corners of the AABB with positive Y going up, what system dose box2D use?

It seems to work now 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=1650