android - libgdx - I can't see my object rendering -
i new libgdx. want render ball on screen, can see empty black screen.
main
public class main implements applicationlistener { @override public void render() { if (!paused) { worldcontroller.update(gdx.graphics.getdeltatime()); } gdx.gl.glclearcolor(0, 0, 0, 0x255/255f); gdx.gl.glclear(gl10.gl_color_buffer_bit); worldrenderer.render(); } }
worldrenderer
public class worldrenderer implements disposable { public void render() { batch.setprojectionmatrix(camera.combined); batch.begin(); (ball b : getworldcontroller().balls) b.render(batch); batch.end(); } }
ball
public class ball extends abstractgameobject { private textureregion textureregion; public ball() { texture texture = new texture(gdx.files.internal("images/ball.png")); dimension.x = 1; dimension.y = 1; // center image on game object origin.set(dimension.x / 2, dimension.y / 2); // bounding box collision detection bounds.set(0, 0, dimension.x, dimension.y); // set physics values terminalvelocity.set(3f, 4f); friction.set(12f, 0f); acceleration.set(0f, -25f); position.x = 5; position.y = 5; textureregion = new textureregion(texture, 0, 0, dimension.x, dimension.y); } @override public void render(spritebatch batch) { batch.draw( textureregion, position.x, position.y, origin.x, origin.y, dimension.x, dimension.y, scale.x, scale.y, rotation, false); } }
abstract game object:
public abstract class abstractgameobject { public vector2 position; public vector2 dimension; public vector2 origin; public vector2 scale; public float rotation; public vector2 velocity; public vector2 terminalvelocity; public vector2 friction; public vector2 acceleration; public rectangle bounds; public abstractgameobject() { this.position = new vector2(); this.dimension = new vector2(); this.origin = new vector2(); this.scale = new vector2(); this.velocity = new vector2(); this.terminalvelocity = new vector2(); this.friction = new vector2(); this.acceleration = new vector2(); this.bounds = new rectangle(); } public void updatemotionx(float deltatime) { if (velocity.x != 0) { // apply friction if (velocity.x > 0) { velocity.x = math.max(velocity.x - friction.x * deltatime, 0); } else { velocity.x = math.min(velocity.x + friction.x * deltatime, 0); } } // apply acceleration velocity.x += acceleration.x * deltatime; // make sure acceleration not exceed terminal velocity velocity.x = mathutils.clamp(velocity.x, -terminalvelocity.x, terminalvelocity.x); } public void updatemotiony(float deltatime) { if (velocity.y != 0) { if (velocity.y > 0) { velocity.y = math.max(velocity.y - friction.y * deltatime, 0); } else { velocity.y = math.min(velocity.y + friction.y * deltatime, 0); } } // apply acceleration velocity.y += acceleration.y * deltatime; // make sure acceleration not exceed terminal velocity velocity.y = mathutils.clamp(velocity.y, -terminalvelocity.y, terminalvelocity.y); } public void update(float deltatime) { updatemotionx(deltatime); updatemotiony(deltatime); position.x += velocity.x * deltatime; position.y += velocity.y * deltatime; } public abstract void render (spritebatch batch); }
what missing? doing wrong?
you need set camera, in override of screen resize:
@override public void resize(int width, int height) { this.camera = new orthographiccamera(width, height); this.camera.position.set(width / 2f, height / 2f, 0); this.camera.update(); }
this set 1 pixel 1 unit, might want increase dimensions of ball. bottom left of screen 0,0. i'd recommend placing ball in middle of screen.
Comments
Post a Comment