Hi Guys, im trying to make the Greenballs move at my Screen, but somehow it isnt showing up. But it compiles just fine.
I changed the example, to just show the balls on the screen.
Just the ground is drawned, the balls arent.
What am i doing wrong ?
package grassproject;
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;
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;
private Body[] ballModels;
private static final float BALL_RADIUS = 0.15f;
//Ground ::
private Sprite groundSprite;
private Texture groundTexture;
// Render general
private SpriteBatch batch;
@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);
}
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 createBalls() {
BodyDef ballBodyDef = new BodyDef();
ballBodyDef.type = BodyDef.BodyType.DynamicBody;
CircleShape shape = new CircleShape();
shape.setRadius(BALL_RADIUS);
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.5f;
fd.shape = shape;
ballModels = new Body[MAX_BALLS];
for (int i = 0; i < MAX_BALLS; i++) {
ballModels[i] = world.createBody(ballBodyDef);
ballModels[i].createFixture(fd);
}
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.5f;
fd.restitution = 0.5f;
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();
world.step(1 / 60f, 10, 10);
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);
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
groundSprite.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() {
}
}