https://code.google.com/p/box2d-editor/
Does you guys use it?
Is it supported by libgdx nowadays, it is ,isnt it ?
Im planning to use it, so i just wanted a few opinions before that
https://code.google.com/p/box2d-editor/
Does you guys use it?
Is it supported by libgdx nowadays, it is ,isnt it ?
Im planning to use it, so i just wanted a few opinions before that
I used it to create my first objects, but it doesnât support joints/dynamic bodies, thatâs why I started writing my own one that fits my needs. To start with, the editor usefull.
You could also have a look at rube: https://www.iforce2d.net/rube/
Ok , whats the diff between RUbe and the other one?
I mean, can i load Rube stuff with libgdx ?
err Rube is not free.
Not using it
Hm, its kinda hard finding a good tutorial about how to load box2D stuff into the game.
I found this :
Hope will help.
Yup, I use it, very useful and easy.
Ok, i got this :
package box2dtest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
/**
*
* @author AndrĂ© VinĂcius Lopes
*/
public class Box2DTest implements ApplicationListener {
private SpriteBatch spritebatch;
private BodyEditorLoader bel;
@Override
public void create() {
FileHandle internal = Gdx.files.internal("GrassProject");
bel = new BodyEditorLoader(internal);
spritebatch = new SpriteBatch();
}
@Override
public void resize(int width, int height) {
}
@Override
public void render() {
spritebatch.begin();
//???
spritebatch.flush();
spritebatch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
I dont have a freaking clue of what to do now.
Im using physics body editor .
Im using the fix of box2d as well. So it compile and run.
How do i draw that on the screen ?
Like I said, use the test code, dissect how it works.
Post here: http://www.aurelienribon.com/blog/projects/physics-body-editor/
Direct code here: https://code.google.com/p/box2d-editor/source/browse/loader-libgdx-demo/src/aurelienribon/bodyeditor/App.java
You may or may not use tween engine but it doesnât really affect it.
Hm, i have some questionsâŠ
What is that Tween Engine
private void createBottle() {
// 0. Create a loader for the file saved from the editor.
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/test.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
// 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.
bottleModel = world.createBody(bd);
// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH);
bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();
}
I dont get this part :
âtest01â ? What does that means and what attachFixture means?
Ah, i opened with the editor and i saw what that test01 means.
Well, but i dont get how to start the other examples
world = new World(new Vector2(0, -10), true);
Wth does that means?
And this?
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH*h/w);
camera.position.set(0, camera.viewportHeight/2, 0);
camera.update();
And this?
// Update
tweenManager.update(1/60f);
world.step(1/60f, 10, 10);
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
batch.setProjectionMatrix(camera.combined);
I dont get those lines⊠If anyone can give me a hand, or a tutorial. I have read some, but still confusing.
Heres the whole class :
package aurelienribon.bodyeditor;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
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.Texture.TextureFilter;
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.BodyDef.BodyType;
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;
public class App extends ApplicationAdapter {
// -------------------------------------------------------------------------
// Static fields
// -------------------------------------------------------------------------
private static final float VIEWPORT_WIDTH = 10;
private static final float BOTTLE_WIDTH = 8;
private static final float BALL_RADIUS = 0.15f;
private static final int MAX_BALLS = 200;
// -------------------------------------------------------------------------
// Public API
// -------------------------------------------------------------------------
// Models
private World world;
private Body bottleModel;
private Vector2 bottleModelOrigin;
private Body[] ballModels;
// Render
private Texture bottleTexture;
private Sprite bottleSprite;
private Texture ballTexture;
private Sprite[] ballSprites;
private Texture whiteTexture;
private Sprite groundSprite;
// Render general
private SpriteBatch batch;
private BitmapFont font;
private OrthographicCamera camera;
// Misc
private final TweenManager tweenManager = new TweenManager();
private final Random rand = new Random();
@Override
public void create() {
// Models initialization
world = new World(new Vector2(0, -10), true);
createGround();
createBottle(); // <-- this method uses the BodyEditorLoader class
createBalls();
// Render initialization
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.BLACK);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH*h/w);
camera.position.set(0, camera.viewportHeight/2, 0);
camera.update();
createSprites();
// Input initialization
Gdx.input.setInputProcessor(new InputAdapter() {
@Override public boolean touchDown(int x, int y, int pointer, int button) {
restart();
return true;
}
});
// Run
restart();
}
private void createGround() {
BodyDef bd = new BodyDef();
bd.position.set(0, 0);
bd.type = 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();
}
private void createBottle() {
// 0. Create a loader for the file saved from the editor.
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/test.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
// 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.
bottleModel = world.createBody(bd);
// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH);
bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();
}
private void createBalls() {
BodyDef ballBodyDef = new BodyDef();
ballBodyDef.type = 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 createSprites() {
bottleTexture = new Texture(Gdx.files.internal("data/gfx/bottle.png"));
bottleTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
bottleSprite = new Sprite(bottleTexture);
bottleSprite.setSize(BOTTLE_WIDTH, BOTTLE_WIDTH*bottleSprite.getHeight()/bottleSprite.getWidth());
ballTexture = new Texture(Gdx.files.internal("data/gfx/ball.png"));
ballTexture.setFilter(TextureFilter.Linear, 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);
}
whiteTexture = new Texture(Gdx.files.internal("data/gfx/white.png"));
groundSprite = new Sprite(whiteTexture);
groundSprite.setSize(VIEWPORT_WIDTH, 1);
groundSprite.setPosition(-VIEWPORT_WIDTH/2, 0);
groundSprite.setColor(Color.BLACK);
}
@Override
public void dispose() {
bottleTexture.dispose();
ballTexture.dispose();
batch.dispose();
font.dispose();
world.dispose();
}
@Override
public void render() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
// Update
tweenManager.update(1/60f);
world.step(1/60f, 10, 10);
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
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);
}
// Render
GL10 gl = Gdx.gl10;
gl.glClearColor(1, 1, 1, 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
groundSprite.draw(batch);
bottleSprite.draw(batch);
for (int i=0; i<MAX_BALLS; i++) {
ballSprites[i].draw(batch);
}
batch.end();
batch.getProjectionMatrix().setToOrtho2D(0, 0, w, h);
batch.begin();
font.draw(batch, "Touch the screen to restart", 5, h-5);
batch.end();
}
// -------------------------------------------------------------------------
// Internals
// -------------------------------------------------------------------------
private void restart() {
bottleModel.setTransform(0, 3, 0.2f);
bottleModel.setLinearVelocity(0, 0);
bottleModel.setAngularVelocity(0);
Vector2 vec = new Vector2();
for (int i=0; i<MAX_BALLS; i++) {
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].setActive(false);
ballModels[i].setLinearVelocity(vec.set(0, 0));
ballModels[i].setAngularVelocity(0);
ballModels[i].setTransform(vec.set(tx, ty), angle);
}
tweenManager.killAll();
Tween.call(new TweenCallback() {
private int idx = 0;
@Override public void onEvent(int type, BaseTween<?> source) {
if (idx < ballModels.length) {
ballModels[idx].setAwake(true);
ballModels[idx].setActive(true);
idx += 1;
}
}
}).repeat(-1, 0.1f).start(tweenManager);
}
}
First snippet is initializing the box2D world. The Vector2 is used for constant force aka gravity. http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/physics/box2d/World.html
Second snippet doesnât even have anything to do with box2d, just standard initialization of a camera and centering it at x=0 and vertically at the middle.
Third code snippet, like I said, is related to one of Aurelien Ribonâs other libraries, Tween Engine. You may want to use it later but for a simple test just exclude it. The world.step simply tells the world to step through its loop like libgdx steps through the render() loop. Also in the world api.
Fourth, simple vector math. Taking the overall position at the corner and subtracting the origin.
Fifth, setting the projection matrix, or where to draw.
Might be time to start being a bit more independent?..you could have easily googled these, and thatâs how I solved my problems the first time.
yay, ty!
I will work more on that!
But, if i take out the Tween Engine, what modifications do i need to do ?
None, really. Just delete any code that is related to the Tween Engine and run it.
yay, ty.
I did some modifications in editor and was really cool.
Now i will research those objects and change them to learn.