/
Box2DHelper.java
34 lines (26 loc) · 1.12 KB
/
Box2DHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package uk.co.carelesslabs.box2d;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class Box2DHelper {
public static Body createBody(World world, float width, float height, float xOffset, float yOffset, Vector3 pos, BodyDef.BodyType type) {
Body body;
BodyDef bodyDef = new BodyDef();
bodyDef.position.set( (pos.x + width/2) + xOffset, (pos.y + height/2) + yOffset);
bodyDef.angle = 0;
bodyDef.fixedRotation = true;
bodyDef.type = type;
body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
PolygonShape boxShape = new PolygonShape();
boxShape.setAsBox(width / 2, height / 2);
fixtureDef.shape = boxShape;
fixtureDef.restitution = 0.4f;
body.createFixture(fixtureDef);
boxShape.dispose();
return body;
}
}