Im having a hard time with box2d, seriously, i was able to draw balls on the screen and made them crash at the ground and rebounce.
I took out the camera thing to see if it improved, well, it did.
Now 0,0 is really 0,0
But if i draw my grass blocks on the screen, that i made with box2d Editor the balls get really small, i mean, VERY VERY small.
Before that, i had a normal class with the camera optimized like in the example, and the grass sprites simple fly over the screen or doesnt show up. It gets all bugged.
Though, the balls go down, rebounce and down again, its preety cool and fun and funny.
But i wanted to replace some ground with the grassBlocks, like this :
package grassproject;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import java.util.Random;
/**
*
* @author André Vinícius Lopes
*/
public class App extends ApplicationAdapter {
private static final float VIEWPORT_WIDTH = 15;
private OrthographicCamera camera;
private float w;
private float h;
//
private World world;
//Ball
private Sprite[] ballSprites;
private Texture ballTexture;
private int MAX_BALLS = 20;
private Body[] ballModels;
private static final float BALL_RADIUS = 0.15f;
private Sprite grassSprite;
private Texture grassTexture;
//Ground ::
private Sprite groundSprite;
private Texture groundTexture;
// Render general
private SpriteBatch batch;
private final Random rand = new Random();
private final TweenManager tweenManager = new TweenManager();
private Body grassModel;
private float GRASS_WIDTH = 512;
private Vector2 grassModelOrigin;
@Override
public void create() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
//
world = new World(new Vector2(0, -10), true);
batch = new SpriteBatch();
//
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * h / w);
camera.position.set(0, camera.viewportHeight/2, 0);
camera.update();
createSprites();
createBalls();
createGround();
}
private void createSprites() {
ballTexture = new Texture(Gdx.files.internal("imagens/ball.png"));
ballTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
ballSprites = new Sprite[MAX_BALLS];
for (int i = 0; i < MAX_BALLS; i++) {
ballSprites[i] = new Sprite(ballTexture);
ballSprites[i].setSize(BALL_RADIUS * 2, BALL_RADIUS * 2);
ballSprites[i].setOrigin(BALL_RADIUS, BALL_RADIUS);
}
grassTexture = new Texture(Gdx.files.internal("imagens/grassSmall.png"));
createGrass();
grassSprite = new Sprite(grassTexture);
groundTexture = new Texture(Gdx.files.internal("imagens/white.png"));
groundSprite = new Sprite(groundTexture);
groundSprite.setSize(VIEWPORT_WIDTH, 1);
groundSprite.setPosition(-VIEWPORT_WIDTH / 2, 0);
groundSprite.setColor(Color.GREEN);
}
private void createGrass() {
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("GrassProject"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.position.set(10,10);
bd.type = BodyDef.BodyType.StaticBody;
// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;
// 3. Create a Body, as usual.
grassModel = world.createBody(bd);
// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(grassModel, "grass02", fd, 1);
grassModelOrigin = loader.getOrigin("grass02", 1).cpy();
}
private void createBalls() {
BodyDef ballBodyDef = new BodyDef();
ballBodyDef.type = BodyDef.BodyType.DynamicBody;
//ballBodyDef.position.x = getRandom();
//ballBodyDef.position.y = 15;
CircleShape shape = new CircleShape();
shape.setRadius(BALL_RADIUS);
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.4f;
fd.restitution = 0.5f;
fd.shape = shape;
ballModels = new Body[MAX_BALLS];
Vector2 vec = new Vector2();
for (int i = 0; i < MAX_BALLS; i++) {
ballModels[i] = world.createBody(ballBodyDef);
ballModels[i].createFixture(fd);
float tx = rand.nextFloat() * 1.0f - 0.5f;
float ty = camera.position.y + camera.viewportHeight / 2 + BALL_RADIUS;
float angle = rand.nextFloat() * MathUtils.PI * 2;
ballModels[i].setLinearVelocity(vec.set(0, 0));
ballModels[i].setAngularVelocity(0);
ballModels[i].setTransform(vec.set(tx, ty), angle);
ballModels[i].setBullet(true);
ballModels[i].setActive(true);
}
shape.dispose();
}
private void createGround() {
BodyDef bd = new BodyDef();
bd.position.set(0,0);
bd.type = BodyDef.BodyType.StaticBody;
PolygonShape shape = new PolygonShape();
shape.setAsBox(VIEWPORT_WIDTH, 1);
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.8f;
fd.restitution = 1f;
fd.shape = shape;
world.createBody(bd).createFixture(fd);
shape.dispose();
}
@Override
public void render() {
GL10 gl = Gdx.gl10;
gl.glClearColor(1, 1, 1, 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
tweenManager.update(1 / 60f);
world.step(1 / 60f, 10, 10);
Vector2 grassPos = grassModel.getPosition().sub(grassModelOrigin);
grassSprite.setPosition(grassPos.x, grassPos.y);
grassSprite.setOrigin(grassModelOrigin.x, grassModelOrigin.y);
for (int i = 0; i < MAX_BALLS; i++) {
Vector2 ballPos = ballModels[i].getPosition();
ballSprites[i].setPosition(ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2);
ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees);
if (i == 0) {
System.out.println(ballPos.x + " ; " + ballPos.y);
}
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
groundSprite.draw(batch);
grassSprite.draw(batch);
for (int i = 0; i < MAX_BALLS; i++) {
ballSprites[i].draw(batch);
}
batch.flush();
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}