How do I increase moving speed of body?
- by Siddharth
How to move ball speedily on the screen using box2d in libGDX?
package com.badlogic.box2ddemo;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
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.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class Box2DDemo implements ApplicationListener {
private SpriteBatch batch;
private TextureRegion texture;
private World world;
private Body groundDownBody, groundUpBody, groundLeftBody, groundRightBody,
ballBody;
private BodyDef groundBodyDef1, groundBodyDef2, groundBodyDef3,
groundBodyDef4, ballBodyDef;
private PolygonShape groundDownPoly, groundUpPoly, groundLeftPoly,
groundRightPoly;
private CircleShape ballPoly;
private Sprite sprite;
private FixtureDef fixtureDef;
private Vector2 ballPosition;
private Box2DDebugRenderer renderer;
Vector2 vector2;
@Override
public void create() {
texture = new TextureRegion(new Texture(
Gdx.files.internal("img/red_ring.png")));
sprite = new Sprite(texture);
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
batch = new SpriteBatch();
world = new World(new Vector2(0.0f, 0.0f), false);
groundBodyDef1 = new BodyDef();
groundBodyDef1.type = BodyType.StaticBody;
groundBodyDef1.position.x = 0.0f;
groundBodyDef1.position.y = 0.0f;
groundDownBody = world.createBody(groundBodyDef1);
groundBodyDef2 = new BodyDef();
groundBodyDef2.type = BodyType.StaticBody;
groundBodyDef2.position.x = 0f;
groundBodyDef2.position.y = Gdx.graphics.getHeight();
groundUpBody = world.createBody(groundBodyDef2);
groundBodyDef3 = new BodyDef();
groundBodyDef3.type = BodyType.StaticBody;
groundBodyDef3.position.x = 0f;
groundBodyDef3.position.y = 0f;
groundLeftBody = world.createBody(groundBodyDef3);
groundBodyDef4 = new BodyDef();
groundBodyDef4.type = BodyType.StaticBody;
groundBodyDef4.position.x = Gdx.graphics.getWidth();
groundBodyDef4.position.y = 0f;
groundRightBody = world.createBody(groundBodyDef4);
groundDownPoly = new PolygonShape();
groundDownPoly.setAsBox(480.0f, 10f);
fixtureDef = new FixtureDef();
fixtureDef.density = 0f;
fixtureDef.restitution = 1f;
fixtureDef.friction = 0f;
fixtureDef.shape = groundDownPoly;
fixtureDef.filter.groupIndex = 0;
groundDownBody.createFixture(fixtureDef);
groundUpPoly = new PolygonShape();
groundUpPoly.setAsBox(480.0f, 10f);
fixtureDef = new FixtureDef();
fixtureDef.friction = 0f;
fixtureDef.restitution = 0f;
fixtureDef.density = 0f;
fixtureDef.shape = groundUpPoly;
fixtureDef.filter.groupIndex = 0;
groundUpBody.createFixture(fixtureDef);
groundLeftPoly = new PolygonShape();
groundLeftPoly.setAsBox(10f, 320f);
fixtureDef = new FixtureDef();
fixtureDef.friction = 0f;
fixtureDef.restitution = 0f;
fixtureDef.density = 0f;
fixtureDef.shape = groundLeftPoly;
fixtureDef.filter.groupIndex = 0;
groundLeftBody.createFixture(fixtureDef);
groundRightPoly = new PolygonShape();
groundRightPoly.setAsBox(10f, 320f);
fixtureDef = new FixtureDef();
fixtureDef.friction = 0f;
fixtureDef.restitution = 0f;
fixtureDef.density = 0f;
fixtureDef.shape = groundRightPoly;
fixtureDef.filter.groupIndex = 0;
groundRightBody.createFixture(fixtureDef);
ballPoly = new CircleShape();
ballPoly.setRadius(16f);
fixtureDef = new FixtureDef();
fixtureDef.shape = ballPoly;
fixtureDef.density = 1f;
fixtureDef.friction = 1f;
fixtureDef.restitution = 1f;
ballBodyDef = new BodyDef();
ballBodyDef.type = BodyType.DynamicBody;
ballBodyDef.position.x = (int) 200;
ballBodyDef.position.y = (int) 200;
ballBody = world.createBody(ballBodyDef);
ballBody.setLinearVelocity(200f, 200f);
// ballBody.applyLinearImpulse(new Vector2(250f, 250f),
// ballBody.getLocalCenter());
ballBody.createFixture(fixtureDef);
renderer = new Box2DDebugRenderer(true, false, false);
}
@Override
public void dispose() {
ballPoly.dispose();
groundLeftPoly.dispose();
groundUpPoly.dispose();
groundDownPoly.dispose();
groundRightPoly.dispose();
world.destroyBody(ballBody);
world.dispose();
}
@Override
public void pause() {
}
@Override
public void render() {
world.step(1f/30f, 3, 3);
Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
vector2 = ballBody.getLinearVelocity();
System.out.println("X=" + vector2.x + " Y=" + vector2.y);
ballPosition = ballBody.getPosition();
renderer.render(world,batch.getProjectionMatrix());
// int preX = (int) (vector2.x / Math.abs(vector2.x));
// int preY = (int) (vector2.y / Math.abs(vector2.y));
//
// if (Math.abs(vector2.x) == 0.0f)
// ballBody1.setLinearVelocity(1.4142137f, vector2.y);
// else if (Math.abs(vector2.x) < 1.4142137f)
// ballBody1.setLinearVelocity(preX * 5, vector2.y);
//
// if (Math.abs(vector2.y) == 0.0f)
// ballBody1.setLinearVelocity(vector2.x, 1.4142137f);
// else if (Math.abs(vector2.y) < 1.4142137f)
// ballBody1.setLinearVelocity(vector2.x, preY * 5);
batch.draw(sprite, (ballPosition.x - (texture.getRegionWidth() / 2)),
(ballPosition.y - (texture.getRegionHeight() / 2)));
batch.end();
}
@Override
public void resize(int arg0, int arg1) {
}
@Override
public void resume() {
}
}
I implement above code but I can not achieve higher moving speed of the ball