Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  [Box2D, libGDX] Drawing a sprite in a body  (Read 1764 times)
0 Members and 2 Guests are viewing this topic.
Offline SrFurias

Senior Newbie





« Posted 2013-06-08 14:37:07 »

First of all, I'm spanish and my English not is perfect, but i'm trying dot you understand me.

I'm trying to put a sprite in a body (or a texture, the theme is dot be a image), so, for do this, I have downloaded Box 2D editor and save my file. After, I find a tutorial for implement the body for my game, but when I run my game it look like this:



Body is moving independently the sprite, so it's no cool...

How can I put my sprite in the body correctly?

My code:

Base.java

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  
package com.unclain.serie2.a;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
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.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;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;

public class Base implements Screen{

   private static final float QUIBBY_WIDTH = 8;
   
   private World world;
   private OrthographicCamera camera;
   private Box2DDebugRenderer renderer;
   private Body bcirculo;
   
   private Texture texture;
   private SpriteBatch sb;
   private Sprite s;
   
   private static final float MUNDO_A_BOX = 0.01f;
   private static final float BOX_DEL_MUNDO = 100f;
   
   private Vector2 quibbyModelOrigin;
   
   private Body quibby;
   
   public void render(float delta) {
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glClearColor(0, 0.5f, 0, 1);
     
      float tw = Gdx.graphics.getWidth();
      float th = Gdx.graphics.getHeight();
     
      camera = new OrthographicCamera(tw / 2, th / 2);
     
      renderer = new Box2DDebugRenderer();
     
      renderer.render(world, camera.combined);
     
      world.step(1/40f, 6, 6);
     
      if(Gdx.input.isKeyPressed(Keys.D)){
         bcirculo.setLinearVelocity(new Vector2(50f, 0));
      }
     
      else if(Gdx.input.isKeyPressed(Keys.A)){
         bcirculo.setLinearVelocity(new Vector2(-50f, 0));
      }
     
      else if(Gdx.input.isKeyPressed(Keys.S)){
         bcirculo.setLinearVelocity(new Vector2(0, -50f));
      }
     
      else if(Gdx.input.isKeyPressed(Keys.W)){
         bcirculo.setLinearVelocity(new Vector2(0, 50f));      
      }
     
      else if(Gdx.input.isKeyPressed(Keys.R)){
         quibby.setLinearVelocity(new Vector2(0, 50f));
         s.setRotation(0);
      }
     
      else if(Gdx.input.isKeyPressed(Keys.UP)){
         quibby.setLinearVelocity(new Vector2(0, 50f));
      }
     
      else if(Gdx.input.isKeyPressed(Keys.DOWN)){
         quibby.setLinearVelocity(new Vector2(0, -50f));
      }
     
      else if(Gdx.input.isKeyPressed(Keys.RIGHT)){
         quibby.setLinearVelocity(new Vector2(50f, 0));
      }
     
      else if(Gdx.input.isKeyPressed(Keys.LEFT)){
         quibby.setLinearVelocity(new Vector2(-50f, 0));
      }
     
      sb = new SpriteBatch();
      sb.begin();
      s.draw(sb);
      sb.end();
     
      Vector2 bottlePos = quibby.getPosition().sub(quibbyModelOrigin);
       
       s.setPosition(bottlePos.x + s.getWidth() * 2, bottlePos.y + s.getHeight() * 2);
       //s.setRegion(0, 0, 115, 135);
      s.setOrigin(quibbyModelOrigin.x, quibbyModelOrigin.y);
       s.setRotation(quibby.getAngle() * MathUtils.radiansToDegrees);
       
   }

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

   public void show() {
      world = new World(new Vector2(0, -60), true);
      camera = new OrthographicCamera(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
     
      BodyDef bdsuelo = new BodyDef();
      bdsuelo.position.set(new Vector2(0, -70));
     
      Body bsuelo = world.createBody(bdsuelo);
     
      PolygonShape ssuelo = new PolygonShape();
      ssuelo.setAsBox((camera.viewportWidth) * 2, 10.0f);
     
      bsuelo.createFixture(ssuelo, 0.0f);
     
      ssuelo.dispose();
     
      BodyDef bdcirculo = new BodyDef();
      bdcirculo.type = BodyType.DynamicBody;
      bdcirculo.position.set(new Vector2(0, 10));
     
      bcirculo = world.createBody(bdcirculo);
     
      CircleShape scircle = new CircleShape();
      scircle.setRadius(5.4f);
     
      FixtureDef fdcircle = new FixtureDef();
      fdcircle.density = 0.6f;
      fdcircle.friction = 0.5f;
      fdcircle.restitution = 0.5f;
      fdcircle.shape = scircle;
     
      Fixture fcircle = bcirculo.createFixture(fdcircle);
     
      texture = new Texture(Gdx.files.internal("MuñecoFeo256x256.png"));
     
      s = new Sprite(texture);
     
      scircle.dispose();
     
      crearQuibby();
     
   }
   
   public void crearQuibby(){
      BodyEditorLoaderFix loader = new BodyEditorLoaderFix(Gdx.files.internal("data/Serie2b.json"));
     
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DynamicBody;
     
      FixtureDef fd = new FixtureDef();
      fd.density = 1;
      fd.friction = 0.5f;
      fd.restitution = 0.3f;
     
      quibby = world.createBody(bd);
      quibby.setUserData(s);
     
      loader.attachFixture(quibby, "MuñecoFeo.png", fd, QUIBBY_WIDTH);
      quibbyModelOrigin = loader.getOrigin("MuñecoFeo.png", QUIBBY_WIDTH).cpy();
   }


   public void hide() {
     
   }

   public void pause() {
     
   }

   public void resume() {
     
   }

   public void dispose() {
     
   }
   
   
   
}


The other class contain a setScreen(new Base()); method.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-06-08 16:00:35 »

You need to look into box2d scaling: http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html
Basically, box2d requires you to scale from pixels to meters. You can't do 1:1 otherwise it will screw up, so choose something reasonable and read the article.
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #2 - Posted 2013-06-08 16:30:06 »

^ yup, this. I think you already have the conversion/scaling variables in your code, but you don't use them in your code that you posted:

1  
2  
   private static final float MUNDO_A_BOX = 0.01f;
   private static final float BOX_DEL_MUNDO = 100f;


So basically, what you'd want to try is multiply the x and y coordinates of Sprite s by BOX_DEL_MUNDO when you're setting its position.


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2013-06-08 17:51:53 »

Use camera that size is specified in meters.
Offline dermetfan
« Reply #4 - Posted 2013-06-09 16:16:46 »

It might not be the best way to understand how it works, but this might do the job for you.

Offline SrFurias

Senior Newbie





« Reply #5 - Posted 2013-06-11 18:23:42 »

I tryied to multiplicate the position of the sprite * BOX_DEL_MUNDO but now I can't see it...

So, I have problems with Jymmt's link, there are any tutorial for do this? I don't understand the render part of it.  Roll Eyes
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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!