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  
  (LibGdx) Sprite not rendering on Box2D body  (Read 259 times)
0 Members and 1 Guest are viewing this topic.
Offline AppleSauce
« Posted 2014-07-08 23:38:14 »

I'm trying to get the Image I have to appear on the box so I can try to scale it. When I call
1  
.setUserData(Sprite);
It doesn't work. Excuse any random crap, I tried to cut out any other code I was working on.


MyGdxGame 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  
public class MyGdxGame extends Game {
   
   @SuppressWarnings("unused")
   private TestScreenA GameScreen;
   
   @Override
   public void create () {
      //loads image
     AssetLoaderTest.load("testImgs/pinkieflie.png");
      AssetLoaderTest.loadTile("testImgs/Colors tile.png");
      AssetLoaderTest.loadAnimation("testImgs/loadingsheet.png");
      AssetLoaderTest.loadSound("testSounds/Peetiay - Overworld (Theme from Super Mario) (promodj.com).mp3");
      AssetLoaderTest.loadFont("testFonts/ARHERMANN.TTF",80 ,Color.PINK);
      GameScreen = new TestScreenA(this);

     
   }
   
   @Override
   public void render () {
      GameScreen.render(0);
     
     
   }
}



Screen 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  
public class TestScreenA implements Screen{
   
   private MyGdxGame game;
   
   //Camera
  private OrthographicCamera camera;
   
   //images etc.
  private SpriteBatch batch;
   
   
   int a = 100, b = 100;
   
   
   Vector3 touch;
   
   //Box2D
  private World world;
   private Box2DDebugRenderer b2dr;
   private Body bd;
   
   
   public TestScreenA(MyGdxGame game) {
      this.game = game;
      //Camera
     camera = new OrthographicCamera();
      camera.setToOrtho(true, 1920, 1080);
      batch = new SpriteBatch();
     
     
      //Box 2d
     //CREATE WORLD
     world = new World(new Vector2(0, 100f), true);
      b2dr = new Box2DDebugRenderer();
     
     
     
      BodyDef bdef2 = new BodyDef();
      bdef2.position.set(165 , 100 );
      bdef2.type = BodyType.DynamicBody;
      bd = world.createBody(bdef2);
     
      PolygonShape shape2 = new PolygonShape();
      shape2.setAsBox(5, 5 );
      FixtureDef fdef2 = new FixtureDef();
      fdef2.shape = shape2;
      fdef2.restitution = 1f;
      bd.createFixture(fdef2);
     
     
     
     
      // create platform
     BodyDef bdef = new BodyDef();
      bdef.position.set(160 , 120 );
      bdef.type = BodyType.StaticBody;
      Body body = world.createBody(bdef);
     

      //WHERE I TRY TO GIVE THE BOX AN IMAGE
     body.setUserData(AssetLoaderTest.s);
     

      PolygonShape shape = new PolygonShape();
      shape.setAsBox(50, 5 );
      FixtureDef fdef = new FixtureDef();
      fdef.shape = shape;
      body.createFixture(fdef);
     
      //platform 2
     bdef.position.set(160 , 280 );
      bdef.type = BodyType.StaticBody;
      body = world.createBody(bdef);
     
      shape.setAsBox(5 , 5);
      fdef.shape = shape;
      body.createFixture(fdef);
     
      //platform 3
     bdef.position.set(200 , 380 );
      bdef.type = BodyType.StaticBody;
      body = world.createBody(bdef);
     
      shape.setAsBox(100 , 5);
      fdef.shape = shape;
      body.createFixture(fdef);
     
      //platform 4
     bdef.position.set(100 , 500);
      bdef.type = BodyType.StaticBody;
      body = world.createBody(bdef);
     
      shape.setAsBox(80 , 5);
      fdef.shape = shape;
      body.createFixture(fdef);
     
      //platform 4
     bdef.position.set(0 , 0);
      bdef.type = BodyType.StaticBody;
      body = world.createBody(bdef);
     
      shape.setAsBox(2 , 500);
      fdef.shape = shape;
      body.createFixture(fdef);
     
      // create falling box
     bdef.position.set(160 , 200 );
      bdef.type = BodyType.DynamicBody;
      body = world.createBody(bdef);
     
      shape.setAsBox(5 , 5);
      fdef.shape = shape;
      fdef.restitution = 3;
      fdef.density = 40;
      body.createFixture(fdef);
     
     
      //falling box 2
     bdef.position.set(180 , 200 );
      bdef.type = BodyType.DynamicBody;
      body = world.createBody(bdef);
     
      shape.setAsBox(5 , 5);
      fdef.shape = shape;
      fdef.restitution = 5;
      fdef.density = 90;
      body.createFixture(fdef);
     
   }
   
   
   public void render(float delta) {
      //clears the screen with white
     Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
      camera.update();
      batch.setProjectionMatrix(camera.combined);
     
      update(delta);
     
     
     
     
      world.step(1.f/60.f, 6, 3);
           
      batch.begin();
     
      b2dr.render(world, camera.combined);
     
     
      batch.end();
   }

   public void update(float delta) {        
     
   }
   
   public void resize(int width, int height) {
     
   }

   //first thing that happens before game starts pretty much
  public void show() {
     
     
     
   }

   
   public void hide() {
     
   }

   
   public void pause() {
     
   }

   
   public void resume() {
     
   }

   
   public void dispose() {
     
   }

}


AssetLoaderTest 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  
public class AssetLoaderTest {
   
   //Images etc.
     
   public static Texture t;
   public static Sprite s;
   
   //Tile Sheet
  static Texture sSheet;
   static Sprite blueTile;
   static Sprite yellowTile;
   
   //Animation
  static Texture sheet;
   static TextureRegion[] sheetFrames;
   static TextureRegion currentFrame;
   static Animation sheetAnim;
   
   
   //Sound
  static Sound marioTheme;
   
   
   //Fonts (you can get a font by C: > windows > fonts)
  static BitmapFont font;
   
   static void load(String assetPath) {
     
      t = new Texture(Gdx.files.internal(assetPath));
      //Automatically scales texture based off of window size
     t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
      //a sprite is pretty much the body and the texture is the texture or image of it
     s = new Sprite(t);
      s.flip(false, true);
     
   }
   
   static void loadTile(String assetPath) {
     
      sSheet = new Texture(assetPath);
     
      blueTile = new Sprite(sSheet, 0, 0, 16, 16);
      yellowTile = new Sprite(sSheet, 16, 0, 16, 16); //add 16 since they're 16 bit
     //don't need to flip. Maybe cuz you can't tell if it's flipped
     
   }
   
   static void loadAnimation(String assetPath) {
     
      sheet = new Texture(Gdx.files.internal(assetPath));//sprite sheet and sprite dimaensions down there
     TextureRegion[][] temp = TextureRegion.split(sheet, 16, 16);
      //images in our frame
     sheetFrames = new TextureRegion[12];
     
      int index = 0;
      //3 is amout of rows 4 is cols
     for (int a = 0; a < 3; a++) {
         
         for (int b = 0; b < 4; b++) {
           
            sheetFrames[index++] = temp[a][b];
           
         }
      }
     
      //12 is number of images
     for (int a = 0; a < 12; a++) {
         
         sheetFrames[a].flip(false, true);
      }
      //0.2f is the time
     sheetAnim = new Animation(0.1f, sheetFrames);
     
   }

   
   static void loadSound(String assetPath) {
     
      marioTheme = Gdx.audio.newSound(Gdx.files.internal(assetPath));
     
   }

   @SuppressWarnings("deprecation")
   static void loadFont(String assetPath, int fontSize,Color fontColor) {
      FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(assetPath));
      //@param size
     font = gen.generateFont(fontSize);
      font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
      font.setColor(fontColor);
      //@Param flips font
     font.setScale(1, -1);
     
   }

}



I can render them the images with batch.draw(); but I can't draw it on the box.
Offline Longarmx
« Reply #1 - Posted 2014-07-08 23:45:40 »

Setting the userdata won't render it for you. It is simply a way to associate sprites with bodies. You have to loop through the bodies, get the sprite for the specific body, and render that.
See this video for a tutorial from Dermetfan:

https://www.youtube.com/watch?v=1cB-iWycUH4&list=PLXY8okVWvwZ2Ph9LKWiNBZ1GRAc_TyDru&index=7

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.

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

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

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

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

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

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

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

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

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

BurntPizza (54 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!