Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sholnk.box2dtry;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- import com.badlogic.gdx.physics.box2d.CircleShape;
- import com.badlogic.gdx.physics.box2d.Fixture;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.World;
- public abstract class EntityBase {
- protected BodyDef bodyDef = new BodyDef();
- protected Body body;
- protected Texture texture;
- protected CircleShape circle = new CircleShape();
- protected FixtureDef fixtureDef = new FixtureDef();
- protected Fixture fixture;
- protected Vector2 vel = new Vector2();
- protected Vector2 pos = new Vector2();
- protected Vector2 dimensions = new Vector2();
- public EntityBase(World world, Texture texture) {
- this.texture = texture;
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.position.set(0, 0);
- body = world.createBody(bodyDef);
- circle.setRadius(texture.getWidth() / 2f);
- dimensions.x = circle.getRadius() * 2;
- dimensions.y = circle.getRadius() * 2;
- fixtureDef.shape = circle;
- fixtureDef.density = 0.5f;
- fixtureDef.friction = 0.4f;
- fixtureDef.restitution = 0.6f; // Make it bounce a little bit
- fixture = body.createFixture(fixtureDef);
- }
- public void render(SpriteBatch batch) {
- batch.draw(texture, body.getPosition().x, body.getPosition().y);
- }
- public abstract void update();
- public Vector2 getPosition() {
- return body.getPosition();
- }
- public float getWidth() {
- return dimensions.x;
- }
- public float getHeight() {
- return dimensions.y;
- }
- public void setPosition(Vector2 pos) {
- body.setTransform(pos, 0);
- this.pos = pos;
- }
- public void setPosition(float x, float y) {
- pos.x = x; pos.y = y;
- body.setTransform(pos, 0);
- }
- public Body getBody() {
- return body;
- }
- public void dispose() {
- circle.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement