Box2D Forums

It is currently Thu May 23, 2013 11:58 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sun Jan 08, 2012 4:40 pm 
Offline

Joined: Sat Nov 26, 2011 6:47 pm
Posts: 12
I have two bodies.
One body is for my player, another is for a ground sensor.
(this is for a platform game by the way).

I'm using the box2d implementation used in the AndEngine lib for Android development.

My current problem is, my player is a dynamic body, while the ground sensor is a static body.

I need the following:
- Have the ground sensor follow my player around
- Allow my player to be collision free of the ground sensor

I have created a line joint that joins the two bodies together.

The ground sensor basically is just going to be used in my contact listener to tell if the player has hit the ground
(so that I don't jump infinitely in the air).
I've placed it perfectly where my player's feet are supposed to be.

The problem is, since it's a static body...my player just floats on top of it, not being able to move (since I've placed it directly
on my players foot, he can't move).

How would I make it so the player body can flow freely through the ground sensor body, as if it isn't there?
I just need the ground sensor body to follow my player (staying relative to his foot).

Code:
      spr = new AnimatedSprite(x, y, playerTextureRegion);
      setPosition(120, 120);
      spr.setCurrentTileIndex(0);

      bodyFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
      bodyFixtureDef.restitution = 0.0f;

      body = PhysicsFactory.createBoxBody(world, spr, BodyType.DynamicBody,
            bodyFixtureDef);
      body.setFixedRotation(true);
      body.setUserData("player");
      FixtureDef groundsensorFixtureDef = PhysicsFactory.createFixtureDef(0,
            0.5f, 0.5f);
      groundsensorFixtureDef.isSensor = true;

                //Place the block sprite (sprite for the ground sensor) at the player's foot area
      final AnimatedSprite block = new AnimatedSprite(x
            + (spr.getWidth() / 2) - 3, y + spr.getHeight() + 3,
            testTextureRegion);
      Body ground_sensor = PhysicsFactory.createBoxBody(world, block,
            BodyType.StaticBody, groundsensorFixtureDef);
      ground_sensor.setUserData("groundSensor");

      final LineJointDef lineJointDef = new LineJointDef();
                lineJointDef.initialize(body, ground_sensor, body.getWorldCenter(), new Vector2(1.0f, 0.0f));
                lineJointDef.enableMotor = false;
                lineJointDef.motorSpeed = 10;
                lineJointDef.maxMotorForce = 100;

                //Join the player's sprite and body together
      world.registerPhysicsConnector(new PhysicsConnector(spr, body, true,
            true));

                //Join the ground sensor's sprite and body together
      world.registerPhysicsConnector(new PhysicsConnector(block,
            ground_sensor, true, true));

      scene.attachChild(spr);
      scene.attachChild(block);


Top
 Profile  
 
PostPosted: Sun Jan 08, 2012 6:10 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
You can put more than one fixture on a body.
So if the sensor is attached at the foot of the player body, it will always be in the right place and the two fixtures will be 'collision free'.


Top
 Profile  
 
PostPosted: Sun Jan 08, 2012 7:01 pm 
Offline

Joined: Sat Nov 26, 2011 6:47 pm
Posts: 12
I'm a bit confused then.
So here's my code for my ground fixture then:
Code:
      FixtureDef groundsensorFixtureDef = PhysicsFactory.createFixtureDef(1,
            0.5f, 0.5f);
      groundsensorFixtureDef.isSensor = true;
      
      body.createFixture(groundsensorFixtureDef);


A fixture does not have a method: "setUserData()" so..how would I get the collision in the contact listener?

Also, how do I set this fixture at the foot of the player?

edit:
Well that didn't work.
Quote:
body.createFixture(groundsensorFixtureDef);


I get a null pointer exception when this executes.


Top
 Profile  
 
PostPosted: Sun Jan 08, 2012 8:43 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
I don't know about the Java port, but the original C++ version of Box2D allows for user data in bodies, fixtures and joints.
To put the sensor at the foot of the player you just need to set sensible locations for the main body and the foot sensor. This might be useful: http://www.iforce2d.net/b2dtut/jumpability


Top
 Profile  
 
PostPosted: Sun Jan 08, 2012 9:04 pm 
Offline

Joined: Sat Nov 26, 2011 6:47 pm
Posts: 12
All right so here is my new code:
Code:
      bodyFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
      bodyFixtureDef.restitution = 0.0f;

      body = PhysicsFactory.createBoxBody(world, spr, BodyType.DynamicBody,
            bodyFixtureDef);
      body.setFixedRotation(true);
      body.setUserData("player");
      
      FixtureDef groundsensorFixtureDef = PhysicsFactory.createFixtureDef(1,
            0.5f, 0.5f);

      PolygonShape polyShape = new PolygonShape();
      polyShape.setAsBox(1, 1);
      
      groundsensorFixtureDef.shape = polyShape;
      groundsensorFixtureDef.density = 1.0f;
      groundsensorFixtureDef.isSensor = true;
      Fixture footSensorFixture = body.createFixture(groundsensorFixtureDef);
      footSensorFixture.setUserData("foot");


Now in my contact listener I'm still not getting any collisions between "wall" and "foot", heh, not even "player" and "foot".
And after looking at this:
http://www.iforce2d.net/b2dtut/jumpability
I'm still not sure how to set the actual position of it to the player's foot.

I even tried making the shape as big as I could with: setRadius() but it still couldn't detect a collision...


Top
 Profile  
 
PostPosted: Sun Jan 08, 2012 9:56 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
You will not get any collisions between "player" and "foot" because they are on the same body.

I can't see where you define the shape for the player's body, but if you make it bigger than the foot sensor then the foot sensor will not have a chance to hit the ground.
You can change the position of the foot sensor by using the second form of SetAsBox which takes another parameter to specify the center of the box.


Top
 Profile  
 
PostPosted: Sun Jan 08, 2012 11:51 pm 
Offline

Joined: Sat Nov 26, 2011 6:47 pm
Posts: 12
Hm well the shape of the player is determined by: "spr".
The "spr" (sprite) can be used as an argument in andengine's implementation.

Anyways, the sprite is 33x33 pixels...
So why wouldn't it be working? I made the foot shape bigger than the sprite didn't I?


Top
 Profile  
 
PostPosted: Mon Jan 09, 2012 12:14 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Box2D doesn't know anything about pixels. You mean the player is 33x33 units in the physics world? If so, then the foot sensor being a 2x2 box would be smaller.
I recommend you use the debug draw feature so you can see exactly what is going on.


Top
 Profile  
 
PostPosted: Mon Jan 09, 2012 9:02 pm 
Offline

Joined: Sat Nov 26, 2011 6:47 pm
Posts: 12
Finally got it working!


Top
 Profile  
 
PostPosted: Tue Jan 10, 2012 2:16 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
It would be nice if you could mention something about how you fixed the issue, for people who come to this thread later.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 3 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