In this post we would me making moving platforms in box2d. In most of the games especially platformers we have these moving objects which would go on a specific direction for a fixed distance and come back.
A Static Rectangular Platform:
First we will make a simple platform which would be static and can be useful in making ground, walls etc. BodyType would be static and shape can be either circle shape or polygon. We can give any density but it would not matter as it has 0 mass because it is static.
Restitution:- Range (0-1) (low value will give less bounce on a platform and 1 will give elastic bounce)
We can also add friction to it which will make the constantly apply a force in opposite direction on the body moving on the platform.
World world=new World(new Vector2(0,-20); BodyDef bodyDef = new BodyDef(); bodyDef.type=BodyDef.BodyType.StaticBody; bodyDef.position.set(new Vector2(0,0); bodyDef.position.angle=0; Body body=world.createBody(bodyDef); PolygonShape bodyShape = new PolygonShape(); bodyShape.setAsBox(width / (2f),height / (2f )); //Set As Box takes half width and half height as arguments FixtureDef fixtureDef=new FixtureDef(); fixtureDef.density=1f; fixtureDef.restitution=1f; fixtureDef.shape=bodyShape; body.createFixture(fixtureDef); bodyShape.dispose();
The above code is for reference how can a body and fixture be created. While setting the position and dimensions of fixture be careful that they should be in box units i.e. for example if you have a scale 0.01f from World to coordinate to Box2d then if a body is 100pixels wide and high then set the dimensions as 100*0.01f/(2f). Same goes for position.
Rectangular Moving Platform :-
We can use the above the above object but instead of making the BodyType as Static we would be using Kinematic bodytype. The properties of Kinematic bodies is similar to static but they can have a velocity also. So Kinematic register collisions with only dynamic bodies similar to static bodies. No kinematic – kinematic body collision is regsitered and neither kinematic-static body collision.
Setting Velocity to Platform:-
We can set a constant velocity to kinematic bodies which then look like moving platforms we have in games like mario etc. In the game balloon shooter moving balloons, red bricks and birds are all kinematic bodies. For now we would consider a simple case when a platform has to go from a spot “A” to spot “B” and come back and keep on doing that.
We will take a example that we want a body to move from A(200,100) to B(280,180) within a period of 2 seconds. We would consider our scaling factor is 100 i.e. the factor to convert coordinates and fixtures sizes from box2d to world coordinates. So we have distance and time, we can calculate the velocity and set it to the body.
Here time would be 2 seconds. We would have to update the texture position in every frame because it the body would be moving so textures position has to be updated with respect to the body, in same way as handling a dynamic body.
Checking the boundary limits :-
There are two ways to check boundary conditions. One is we can have a timer which updates every frame and checks if 2 seconds (the time limit) is reached we can change the direction of the platform (negate the velocity). Other way is to check if it reached its destination position and then set it to opposite direction. In balloon shooter we are checking with time so every moving platform has a time duration and if it crosses that we just reverse the direction.
If we want to do the other way around by checking the position, then we have to take a range as our boundary limit rather than the exact coordinates. For example if we are checking if body reached 200,100 then if we use conditions like x==200 && y==100 to confirm the arrival then chances are most of the time it wont give correct result. We can check like x<=200 && y<=100 which would be better for this as case we know body velocity has a negative x & y component. Better way is to have a range something like a circular area to check if the body has reached that minimum distance then we can change the direction. We can take for e.g. 2 units as range so we calculate distance of body from the destination and if it equal to or less than 2 then we change the direction. We have to just have such a range which we know it wont cross in one time frame(like it is moving 5 units per frame so it can cross the circular range without the condition becoming true).
Thats all for this post. If you have any queries/suggestions please put them in comments and we would try to answer them.
Thank you for the patience.