Ok So this is just a “protoype” code
It is supposed to have a bouncing ball and it works.
package com.Fawaz.Tut;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
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.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class Box2d implements ApplicationListener {
OrthographicCamera camera;
World world;
Box2DDebugRenderer renderer;
Sprite Circle;
float width, height;
CircleShape circleShape;
FPSLogger logger;
Body circleBody;
@Override
public void create() {
}
@Override
public void resize(int width, int height) {
Circle = new Sprite(new Texture(Gdx.files.internal("data/Circle.png")));
camera = new OrthographicCamera(width,height);
camera.position.set(width /2 , height / 2, 0);
camera.update();
world = new World(new Vector2(0, -9.8f), false);
renderer = new Box2DDebugRenderer();
logger = new FPSLogger();
BodyDef circleDef = new BodyDef();
circleDef.type = BodyType.DynamicBody;
circleDef.position.set(width/2, height/2);
circleBody = world.createBody(circleDef);
circleShape = new CircleShape();
circleShape.setRadius(45);
FixtureDef circleFixture = new FixtureDef();
circleFixture.shape = circleShape;
circleFixture.density = 0.4f;
circleFixture.friction = 0.2f;
circleFixture.restitution = 1;
circleBody.createFixture(circleFixture);
circleBody.setUserData(Circle);
BodyDef groundDef = new BodyDef();
groundDef.position.set(0, 3);
Body groundBody = world.createBody(groundDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox(camera.viewportWidth * 2, 0);
groundBody.createFixture(groundBox, 0);
}
@Override
public void render() {
Sprite sprite;
SpriteBatch batch = new SpriteBatch();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer.render(world, camera.combined);
batch.begin();
sprite = (Sprite) circleBody.getUserData();
sprite.setPosition(circleBody.getPosition().x - (sprite.getWidth() /2) , circleBody.getPosition().y - (sprite.getHeight()/2));
sprite.setSize(45*2, 45*2);
sprite.draw(batch);
batch.end();
world.step(1/45f, 6, 2);
logger.log();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
world.dispose();
}
}
Now this works as it produces this
But the question is should i scale from pixel to meters
or is this perfectly fine
and if i should
Should you tell me how that would go