Box2D Forums

It is currently Sat May 25, 2013 9:24 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Dec 04, 2011 8:18 am 
Offline

Joined: Sun Dec 04, 2011 4:52 am
Posts: 2
My Bodies seem to be correctly created and affected by gravity, but they do not interact with anything. They are quite happy to just fall forever in whatever direction they are sent until they end up off screen. I can put other bodies in the way or crash them in to each other with MouseJoints, but unless I take direct control of one they just continue in their path indefinitely.

Most of my code is very similar to the HelloWorld from the manual, but as that is C++ there may be some unintentional differences there. This is running on Android.
Code:
   private PhysicsThread thread;
   SurfaceHolder surfaceHolder;
   private World world;
   private BodyDef groundBodyDef = new BodyDef();
   private BodyDef bodyDef = new BodyDef();
   private FixtureDef fixtureDef = new FixtureDef();
   private PolygonShape groundBox = new PolygonShape();
   private Body groundBody, body, circleA, circleB, circleC;
   private CircleShape csA, csB, csC;
   private BodyDef circleADef = new BodyDef(), circleBDef = new BodyDef(),
         circleCDef = new BodyDef();
   private MouseJointDef md;
   private MouseJoint mj;
   private int pitch;
   private int roll;

   public PhysicsView(Context context, AttributeSet attrs) {
      super(context, attrs);
      getHolder().addCallback(this);
      thread = new PhysicsThread(getHolder(), this);
      setFocusable(true);
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width,
         int height) {
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
      Vec2 gravity = new Vec2((float) 0.0, (float) 10.0);
      boolean doSleep = true;
      
       
      world = new World(gravity, doSleep);
   
      groundBodyDef = new BodyDef();
      groundBodyDef.position.set(50.0f, 50.0f);
      groundBody = world.createBody(groundBodyDef);
                                       
      groundBox = new PolygonShape();
      groundBox.setAsBox(50.0f, 50.0f);
      groundBody.createFixture(groundBox, 0.0f);

      // dynamic Body
      bodyDef.type = BodyType.DYNAMIC;
      bodyDef.position.set(0.0f, 4.0f);
      body = world.createBody(bodyDef);

      PolygonShape dynamicBox = new PolygonShape();
      dynamicBox.setAsBox(1.0f, 1.0f);

      // Circle A
      circleADef.type = BodyType.DYNAMIC;
      circleADef.position.set(100.0f, 100.0f);
      circleA = world.createBody(circleADef);

      csA = new CircleShape();
      csA.m_p.set(2.0f, 3.0f);
      csA.m_radius = 10.0f;

      // Circle B
      circleBDef.type = BodyType.DYNAMIC;
      circleBDef.position.set(200.0f, 200.0f);
      circleB = world.createBody(circleBDef);

      csB = new CircleShape();
      csB.m_p.set(2.0f, 3.0f);
      csB.m_radius = 50.5f;

      // Circle C – Static for testing
      circleCDef.type = BodyType.STATIC;
      circleCDef.position.set(200.0f, 400.0f);
      circleC = world.createBody(circleCDef);

      csC = new CircleShape();
      csC.m_p.set(2.0f, 3.0f);
      csC.m_radius = 50.5f;

      fixtureDef.shape = dynamicBox;
      fixtureDef.density = 1.0f;
      fixtureDef.friction = 0.3f;

      body.createFixture(fixtureDef);

      thread.setRunning(true);
      thread.start();

   }


The gravity and joints are set in other methods and seem to be functioning correctly. I'm sure it's a stupid problem but I've been unable to fix it in a couple of days work working solid. I've tried playing around with lots of different variables for groundBodyDef.position.set() and groundBody.setAsBox().

Ultimately I would like the objects should crash into the edges of the screen and each other.


Top
 Profile  
 
PostPosted: Mon Dec 05, 2011 6:28 am 
Offline

Joined: Sun Dec 04, 2011 4:52 am
Posts: 2
I worked this out - I wasn't binding the circles to the fixtures. I've got them colliding with themselves now, just trying to find the damn walls! (they're bouncing off something out of screen... I think).


Top
 Profile  
 
PostPosted: Sat Dec 31, 2011 2:41 pm 
Offline

Joined: Sat Dec 31, 2011 2:39 pm
Posts: 1
hi.. can u please explain how you binding the circles to the fixtures?

im having the same problem heh.. did u find the walls ? ;p


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group