Reputation: 435
thanks in advance for any help!
I'm learning the box2d
tutorial about adding bodies to my game however it seems they are just drawing with the debugrenderer.
I am just practicing, and I have a zelda snes style game map and hero character loading fine. In my gamemap I have objects (rectangle map objects) shoing tree stumps rectangles (which I want to use to stop hero walking through a tree).
Can Box2d
be used in this example? I dont want any gravity/friction/mass
etc but what I want to do is make 'static bodies' from the tree stump rectangles which I can detect collision with my hero's rectangle. Of course I can just write my own code to check collision but it is always buggy and I am struggling to think of the correct way to loop all the tree stump rectangles and stop the hero walking through it.
(NOTE: I also am part way through writing a platformer. It is all working well and he lands on the platforms but Im not using Box2d
and for example it doesnt fall off when you walk over edge of platform it keeps going until you jump then it detects where platform is. - hence why I wanted to learn box2d
)
Can box2d
make these static bodies (in the way I say above) and if so , how can you link the bodies to the sprite you already have (AND the rectangle array I pulled from my TMX file
)
Hope this is clear for people. Many thanks
Upvotes: 0
Views: 918
Reputation: 29
You should create fixtures for character and trees
vertexArray = new Vector2[3];
vertexArray[0] = new Vector2(0, 0);
vertexArray[1] = new Vector2(2, 0);
vertexArray[2] = new Vector2(1, 3);
treePolygonShape = new PolygonShape();
treePolygonShape.set(vertexArray);
fixtureTree = new FixtureDef();
fixtureTree.shape = treePolygonShape;
fixtureTree.filter.categoryBits = Game.TREE; // 0x0001 short int
fixtureTree.filter.maskBits = Game.CHARACTER; //0x0002 short int
characterShape = new CircleShape();
shape.setRadius(1);
fixtureCharacter = new FixtureDef();
fixtureCharacter.shape = characterShape;
fixtureCharacter.filter.categoryBits = Game.CHARACTER;
fixtureCharacter.filter.maskBits = Game.TREE;
...
create bodies
characterBodyDef.type = BodyDef.BodyType.KinematicBody; //I suppose your body is kinematic but it can be dynamic too
Body body = world.createBody(characterBodyDef);
body.setUserData(characterView); //display object of your character which is linked to the physical body in this way
characterView.setBody(body); // you can create this setter to have a reference to the physical body from display object
body.createFixture(fixtureCharacter);
...
treeBodyDef.type = BodyDef.BodyType.StaticBody;
treeBodyDef.position.set(treeX, treeY); //you can set tree position here , or later using setTransform(x, y, angle) function
Body treeBody = world.createBody(treeBodyDef);
treeBody.setUserData(treeView); //display object of your tree linked to the body
treeView.setBody(treeBody); // you can create this setter to have a reference to the physical body from display object
treeBody.createFixture(fixtureTree);
your display object coordinates should follow to the physical body's position (you can multiply them with some coefficient like PIXELS_TO_METERS = 100 for convenience)
Now you can
body.setLinearVelocity(velocityX, velocityY);
to your character's body and in render (draw or act) function you can sync display objects coordinates with body's position to set a position for trees you can
treeBody.setTransform(x, y, angle)
use this function
I hope this will help you with your game ;) good luck
Upvotes: 1