Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  How do I combine a sprite with a Box2D body?  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
Offline icantcode

Senior Newbie


Medals: 1



« Posted 2014-05-15 22:00:54 »

How do I set my sprites x/y coordinates to my body's x/y position?
Also I scaled everything down by one hundred since Box2D measures in meters, so am I
also supposed to scale my image down by 100 or something?? This is weird. Code below.

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  
  public class myGame implements ApplicationListener{
   OrthographicCamera camera;
   
   World myWorld;
   
   //Ground
   BodyDef floorDef;
   Body floor;
   FixtureDef floorFix;
   PolygonShape floorShape;
   //
   
   //Sprite
   Texture texture;
   Sprite sprite;
   
   BodyDef entityDef;
   Body entity;
   FixtureDef entityFix;
   PolygonShape entityShape;
   //
   
   SpriteBatch batch;
   Box2DDebugRenderer renderer;
   
   @Override
   public void create() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, Gdx.graphics.getWidth() / 100, Gdx.graphics.getHeight() /100);
      camera.update();
     
      renderer = new Box2DDebugRenderer();
     
      myWorld = new World(new Vector2(0, -10), true);
     
      batch = new SpriteBatch();
     
      //Ground
      floorDef = new BodyDef();
      floorDef.type = BodyType.StaticBody;
      floor = myWorld.createBody(floorDef);
      floorShape = new PolygonShape();
      floorShape.setAsBox(camera.viewportWidth, 20f/100f);
      floorFix = new FixtureDef();
      floorFix.shape = floorShape;
      floor.createFixture(floorFix);
      //
     
      //Sprite
      texture = new Texture(Gdx.files.internal("data/myEntity.png"));
      sprite = new Sprite(texture);
     
      entityDef = new BodyDef();
      entityDef.type = BodyType.DynamicBody;
      entityDef.position.set(new Vector2(440 /100, 400 /100));
      entity = myWorld.createBody(entityDef);
      entityShape = new PolygonShape();
      entityShape.setAsBox(16f /100f, 16f / 100f);
      entityFix = new FixtureDef();
      entityFix.shape = entityShape;
      entity.createFixture(entityFix);
     
   }

   @Override
   public void resize(int width, int height) {
   
     
   }

   @Override
   public void render() {
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
     
     
      renderer.render(myWorld, camera.combined);
      myWorld.step(1/60f, 6, 2);
   }

   @Override
   public void pause() {
   
     
   }

   @Override
   public void resume() {
     
     
   }

   @Override
   public void dispose() {
     
     
   }
   
   
   
   
   
}
Offline Jacob Pickens
« Reply #1 - Posted 2014-05-15 23:57:58 »

Draw the sprite at the body.position.x or body.position.y

Also, do the same with the bodies rotation. Quite self-explanatory.
Offline icantcode

Senior Newbie


Medals: 1



« Reply #2 - Posted 2014-05-16 00:22:08 »

So I added that inside the render method but my image appears in the bottom left corner of the screen. Also he's really tiny. The image jumps up/down but only a miniscule fraction of what my body does.


1  
2  
3  
4  
5  
// sprite is my texture, and entity is my body...
   sprite.setPosition(entity.getPosition().x, entity.getPosition().y);
      batch.begin();
      sprite.draw(batch);
      batch.end();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jacob Pickens
« Reply #3 - Posted 2014-05-16 00:24:27 »

Box2D uses meters not pixels. So, you would want to define what a meter is according to pixels in your game. And then convert the x andy from Box2D into pixels with that conversion if Im not mistaken.
Offline icantcode

Senior Newbie


Medals: 1



« Reply #4 - Posted 2014-05-16 00:40:44 »

Awesome! I multiplied the position by 100 and got it to work. But my image is still small and my image is out of sync with the body. For instance, the body jumps higher than my image (I added some restitution). How do I scale my image to the same size as the body shape?

Edit: It's small because I made it 16 x 16px and since I'm not used to working with pixel art I thought it would look bigger. But I till can't figure out how to scale it to the same size as the shape.
Offline Jacob Pickens
« Reply #5 - Posted 2014-05-16 14:56:37 »

Awesome! I multiplied the position by 100 and got it to work. But my image is still small and my image is out of sync with the body. For instance, the body jumps higher than my image (I added some restitution). How do I scale my image to the same size as the body shape?

Edit: It's small because I made it 16 x 16px and since I'm not used to working with pixel art I thought it would look bigger. But I till can't figure out how to scale it to the same size as the shape.

Was this problem solved? If not I believe your problem there is that you also have to do body.size or something along those line and convert that from meters to pixels as well. Grin Hope that works!
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!