Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Physics Body Editor (aka box2d-editor)  (Read 1370 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-05-28 11:55:34 »

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 Cheesy
Offline Phibedy

Senior Devvie


Medals: 9



« Reply #1 - Posted 2013-05-28 12:01:21 »

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/
Offline Andre Lopes
« Reply #2 - Posted 2013-05-28 12:28:58 »

Ok , whats the diff between RUbe and the other one?

I mean, can i load Rube stuff with libgdx ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-05-28 12:34:10 »

err Rube is not free.
Not using it Tongue
Offline Andre Lopes
« Reply #4 - Posted 2013-05-28 13:25:29 »

Hm, its kinda hard finding a good tutorial about how to load box2D stuff into the game.

I found this :
http://rotatingcanvas.com/using-box2d-in-libgdx-game-part-i/

Hope will help.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2013-05-28 16:48:06 »

Yup, I use it, very useful and easy.
Offline Andre Lopes
« Reply #6 - Posted 2013-05-28 19:46:00 »

Ok, i got this :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
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 ? Cheesy
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #7 - Posted 2013-05-28 21:46:14 »

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.
Offline Andre Lopes
« Reply #8 - Posted 2013-05-30 21:47:30 »

Hm, i have some questions..
1) What is that Tween Engine

2)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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 :

Quote
      // 4. Create the body fixture automatically by using the loader.
      loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH);
      bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();

"test01" ? What does that means and what attachFixture means?
Offline Andre Lopes
« Reply #9 - Posted 2013-05-30 22:20:41 »

Ah, i opened with the editor and i saw what that test01 means.

Well, but i dont get how to start the other examples Tongue

1  
world = new World(new Vector2(0, -10), true);


Wth does that means?
 

And this?
1  
2  
3  
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH*h/w);
      camera.position.set(0, camera.viewportHeight/2, 0);
      camera.update();



And this?
1  
2  
3  
// Update
      tweenManager.update(1/60f);
      world.step(1/60f, 10, 10);


1  
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);


1  
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 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
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);
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #10 - Posted 2013-05-30 23:12:49 »

1  
world = new World(new Vector2(0, -10), true);


Wth does that means?
 

And this?
1  
2  
3  
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH*h/w);
      camera.position.set(0, camera.viewportHeight/2, 0);
      camera.update();



And this?
1  
2  
3  
// Update
      tweenManager.update(1/60f);
      world.step(1/60f, 10, 10);


1  
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);


1  
batch.setProjectionMatrix(camera.combined);

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.
Offline Andre Lopes
« Reply #11 - Posted 2013-05-31 01:24:52 »

yay, ty!
I will work more on that!

But, if i take out the Tween Engine, what modifications do i need to do ?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #12 - Posted 2013-05-31 01:26:35 »

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.
Offline Andre Lopes
« Reply #13 - Posted 2013-05-31 01:44:46 »

yay, ty.
I did some modifications in editor and was really cool.

Now i will research those objects and change them to learn.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (10 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (17 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!