Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: [libGDX] [box2D] Scale a Sprite to a Body on: 2013-08-30 20:34:59
I tryed put it but now the world don't render...   Lips Sealed
2  Game Development / Newbie & Debugging Questions / Re: [LibGdx] Trouble parsing XML? on: 2013-08-28 23:46:02
Try put "" in Element root = reader.parse(items/Items.xml);
3  Game Development / Newbie & Debugging Questions / [libGDX] [box2D] Scale a Sprite to a Body on: 2013-08-28 23:43:58
Hi, I'm tryin to put a Sprite over my bodyplayer, but I can't...


My Sprite moves too fast...

This is my code:


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  
package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
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.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.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class ScreenGame implements Screen{
   
   public BodyDef bDefPlayer;
   public Body bPlayer;
   public Fixture fPlayer;
   public FixtureDef fDefPlayer;
   public PolygonShape shape;
   
   public BitmapFont font;
   
   public SpriteBatch spriteBatch;
   
   public World world;
   public OrthographicCamera camera;
   public Box2DDebugRenderer renderer;
   
   public MyGdxGame game;
   
   public EntityPlayerController playerController;
   
   public Texture texture;
   public Sprite sprite;
   
   public static float WORLD_TO_BOX = 0.01f;
   public static float BOX_TO_WORLD = 100.0f;
   
   public ScreenGame(MyGdxGame game){
      this.game = game;
   }
   
   @Override
   public void render(float delta) {
      // TODO Auto-generated method stub
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glClearColor(0, 0, 0, 1);
     
      world.step(delta, 6, 6);
      renderer.render(world, camera.combined);
     
      playerController.jump();
     
      spriteBatch.setProjectionMatrix(camera.combined);
      spriteBatch.begin();
      sprite.setPosition(BOX_TO_WORLD * bPlayer.getPosition().x - sprite.getWidth() / 2,
            BOX_TO_WORLD * bPlayer.getPosition().y - sprite.getHeight() / 2);
      sprite.draw(spriteBatch);
      spriteBatch.end();
     
      playerController.drawFont();
     
      camera.position.x = bPlayer.getPosition().x;
      camera.position.y = bPlayer.getPosition().y;
      camera.update();
     
      if(Gdx.input.isKeyPressed(Keys.T)) camera.zoom++;
      else if(Gdx.input.isKeyPressed(Keys.Y)) camera.zoom--;
     
      if(camera.zoom >= 40) camera.zoom = 40;
      else if(camera.zoom <= 1) camera.zoom = 1;
     
   }

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void show() {
      // TODO Auto-generated method stub
     world = new World(new Vector2(0, -60), true);
      camera = new OrthographicCamera(Gdx.graphics.getWidth() / 2,
            Gdx.graphics.getHeight() / 2);
      renderer = new Box2DDebugRenderer();
      font = new BitmapFont();
      spriteBatch = new SpriteBatch();
     
      createGround();
      createPlayer();
     
      playerController = new EntityPlayerController(this, world, bPlayer, fPlayer, fDefPlayer, camera);
     
      texture = new Texture(Gdx.files.internal("images/Cuadrado.png"));
      sprite = new Sprite(texture);
      bPlayer.setUserData(sprite);
     
      sprite.setPosition(BOX_TO_WORLD * bPlayer.getPosition().x - sprite.getWidth() / 2,
            BOX_TO_WORLD * bPlayer.getPosition().y - sprite.getHeight() / 2);
     
      /*jumperSprite.setPosition(
            PIXELS_PER_METER * jumper.getPosition().x
                  - jumperSprite.getWidth() / 2,
            PIXELS_PER_METER * jumper.getPosition().y
                  - jumperSprite.getHeight() / 2);
      jumperSprite.draw(spriteBatch);*/

   
   }

   @Override
   public void hide() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void resume() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void dispose() {
      // TODO Auto-generated method stub
     world.dispose();
      font.dispose();
      spriteBatch.dispose();
   }
   
   public Body createPlayer(){
      bDefPlayer = new BodyDef();
      bDefPlayer.position.set(new Vector2(-40, 0));
      bDefPlayer.type = BodyType.DynamicBody;
     
      bPlayer = world.createBody(bDefPlayer);
      bPlayer.setFixedRotation(true);
      bPlayer.setBullet(true);
      bPlayer.setAwake(true);
     
      shape = new PolygonShape();
      shape.setAsBox(5, 10);
     
      fDefPlayer = new FixtureDef();
      fDefPlayer.density = 0f;
      fDefPlayer.restitution = 0f;
      fDefPlayer.friction = 0f;
      fDefPlayer.shape = shape;
     
      fPlayer = bPlayer.createFixture(fDefPlayer);
     
      shape.dispose();
     
      return bPlayer;
   }

   public void createGround(){
      BodyDef bdground = new BodyDef();
      bdground.position.set(0, -40);
     
      Body bground = world.createBody(bdground);
     
      PolygonShape ps = new PolygonShape();
      ps.setAsBox(Gdx.graphics.getWidth(), 10);
     
      Fixture fground = bground.createFixture(ps, 0.5f);
     
      ps.dispose();
   }
   
.



I don't know scale a Sprite... I have try it so many times but I can't WTF!!!!

Can someone help me?

Thanks.
4  Game Development / Newbie & Debugging Questions / [libGDX] [box2D] RevoluteJoint don't work on: 2013-08-08 18:59:16
I'm trying to make a RevoluteJoint with a body (with CircleShape) and another body (with PolygonShape), but it don't change the rotation and I don't know why not...

I have a class with the joint (the bodies and all inside it) for use it more easily:


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  
package unclain.gametest1.objects;

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.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.physics.box2d.joints.RevoluteJoint;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;

public class BasicJointPlatform {
   
   public World world;
   
   public RevoluteJointDef rjd;
   
   public BodyDef bdbodyA;
   public Body bbodyA;
   public FixtureDef fdbodyA;
   public Fixture fbodyA;
   
   public BodyDef bdbodyB;
   public Body bbodyB;
   public FixtureDef fdbodyB;
   public Fixture fbodyB;
   
   public float x;
   public float y;
   
   public BasicJointPlatform(World world, float x, float y, float rectWidth, float rectHeight){
      this.world = world;
      this.x = x;
      this.y = y;
     
      bodyA(world);
      bodyB(world, x, y, rectWidth, rectHeight);
     
      rjd = new RevoluteJointDef();
      rjd.bodyA = bbodyA;
      rjd.bodyB = bbodyB;
      rjd.maxMotorTorque = 10000f;
      rjd.enableMotor = true;
     
     
      RevoluteJoint rj = (RevoluteJoint) world.createJoint(rjd);
      rj.setMotorSpeed(1f);
     
   }
   
   public void bodyA(World world){
      bdbodyA = new BodyDef();
      bdbodyA.position.set(new Vector2(x, y + 15));
      bdbodyA.type = BodyType.StaticBody;
     
      bbodyA = world.createBody(bdbodyA);
     
      CircleShape cs = new CircleShape();
      cs.setRadius(4f);
     
      fdbodyA = new FixtureDef();
      fdbodyA.density = 1f;
      fdbodyA.shape = cs;
     
      fbodyA = bbodyA.createFixture(fdbodyA);
     
      cs.dispose();
   }
   
   public void bodyB(World world, float x, float y, float rectWidth, float rectHeight){
      bdbodyB = new BodyDef();
      bdbodyB.position.set(new Vector2(x, y));
      bdbodyB.type = BodyType.StaticBody;
     
      bbodyB = world.createBody(bdbodyB);
     
      PolygonShape ps = new PolygonShape();
      ps.setAsBox(rectWidth, rectHeight);
     
      fdbodyB = new FixtureDef();
      fdbodyB.density = 1f;
      fdbodyB.shape = ps;
     
      fbodyB = bbodyB.createFixture(fdbodyB);
     
      ps.dispose();
   }

}


and another class with all the joints:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package unclain.gametest1;

import unclain.gametest1.objects.BasicJointPlatform;

import com.badlogic.gdx.physics.box2d.World;

public class Platforms {
   
   public World world;
   
   public BasicJointPlatform platform1, platform2, platform3, platform4;
   
   public Platforms(World world){
      this.world = world;
   }
   
   public void createPlatforms(){
      platform1 = new BasicJointPlatform(world, 50, -30, 8f, 30f);
      platform2 = new BasicJointPlatform(world, 150, -30, 8f, 30f);
      platform3 = new BasicJointPlatform(world, 250, -30, 8f, 30f);
      platform4 = new BasicJointPlatform(world, 350, -30, 8f, 30f);
   }

}



But when I run the game, it don't do anything...

Why my revolutejoint don't work?




The joint don't work when I collide with it...


5  Game Development / Newbie & Debugging Questions / [libGDX] [box2D] TextButton don't work on: 2013-07-22 21:48:09
Hi, I have a problem. I have a TextButton implemented on my MenuScreen, but it don't work. When I click it, it don't do anything...

This is my code:

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  
package com.unclain.udevelop.prueba1;

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.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.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;

public class MenuScreen implements Screen{

   private Stage stage;
   private SpriteBatch spriteBatch;
   private Skin skin;
   private TextureAtlas textureAtlas;
   private TextButton textButton;
   private TextButtonStyle textButtonStyle;
   private BitmapFont buttonLevelsFont;
   private Texture background;
   private Sprite window;
   private Texture textureWindow;
   private Sprite mainImage;
   
   public MenuScreen instance = this;
   
   public GameScreen gamescreen;
   
   public MenuScreen(GameScreen gamescreen){
      this.gamescreen = gamescreen;
   }
   
   @Override
   public void render(float delta) {
      // TODO Auto-generated method stub
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glClearColor(0, 0, 0, 1);
      stage.act(delta);
     
      spriteBatch.begin();
      spriteBatch.draw(background,0, 0);
      window.draw(spriteBatch);
      mainImage.draw(spriteBatch);
      mainImage.draw(spriteBatch);
      buttonLevelsFont.draw(spriteBatch, "Demostración de mecánicas de Unclain Entertainment 2013 - Powered by libGDX and Box2D physics.", 6, 18);
      spriteBatch.end();
      stage.draw();
     
      if(Gdx.input.isKeyPressed(Keys.ENTER)) gamescreen.udevelop.setScreen(new ScreenLevels(instance));
     
   }

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub
     stage.addActor(textButton);
      background.setFilter(TextureFilter.Linear, TextureFilter.Linear);
   }

   @Override
   public void show() {
      Gdx.input.setInputProcessor(stage);
      stage = new Stage();
      spriteBatch = new SpriteBatch();
      textureAtlas = new TextureAtlas(Gdx.files.internal("AdyliumUserInterface.pack"));
      skin = new Skin(textureAtlas);
     
      buttonLevelsFont = new BitmapFont();
     
      textButtonStyle = new TextButtonStyle();
      textButtonStyle.up = skin.getDrawable("BotonNivelesA");
      textButtonStyle.down = skin.getDrawable("BotonNivelesB");
      textButtonStyle.over = skin.getDrawable("BotonNivelesB");
      textButtonStyle.
     
      textButton = new TextButton("", textButtonStyle);
      textButton.setX(Gdx.graphics.getWidth() / 2 - (textButton.getWidth() / 2));
      textButton.setY(Gdx.graphics.getHeight() / 2);
      textButton.setTouchable(Touchable.enabled);
     
      background = new Texture(Gdx.files.internal("BackgroundMenuScreen1.png"));
     
      textureWindow = new Texture(Gdx.files.internal("WindowMenuScreen.png"));
      window = new Sprite(textureWindow);
      window.setX(Gdx.graphics.getWidth() / 2 - (window.getRegionWidth() / 2));
      window.setY(Gdx.graphics.getHeight() / 2 - (window.getRegionHeight() / 2) + 20);
      window.scale(0.1f);
           
      Texture textureMainImage = new Texture(Gdx.files.internal("Logo.png"));
      mainImage = new Sprite(textureMainImage);
      mainImage.setX(Gdx.graphics.getWidth() / 2 - 128);
      mainImage.setY((Gdx.graphics.getHeight() / 2) + 86);
     
   }

   @Override
   public void hide() {
     
   }

   @Override
   public void pause() {
     
   }

   @Override
   public void resume() {
     
   }

   @Override
   public void dispose() {
      skin.dispose();
      stage.dispose();
      textureAtlas.dispose();
      buttonLevelsFont.dispose();
   }

}


Why it don't work?

Thanks.
6  Game Development / Newbie & Debugging Questions / Re: [libGDX] [box2D] How to use Contact Listener? on: 2013-07-19 21:09:19
Here's my 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  
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  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
package com.unclain.udevelop.prueba1;

import box2dLight.PointLight;
import box2dLight.RayHandler;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.unclain.udevelop.prueba1.contacts.Test;
import com.unclain.udevelop.prueba1.ingame.EntityPlayer;
import com.unclain.udevelop.prueba1.utils.CharacterController;
import com.unclain.udevelop.prueba1.utils.ContactCheck;
import com.unclain.udevelop.prueba1.utils.MovingPlatform;
import com.unclain.udevelop.prueba1.utils.SpriteAnimation;

public class ScreenTestOne implements Screen {
   
   OrthographicCamera cam;
   Array<MovingPlatform> platforms = new Array<MovingPlatform>();
   MovingPlatform groundedPlatform = null;
   BitmapFont font;
   
   public GameScreen gamescreen;
   
   public World world;
   
   public OrthographicCamera camera;
   
   private Box2DDebugRenderer renderer;
   
   private RayHandler rayHandler;

   private ContactCheck contactCheck;
   
   private CharacterController playerController;
     
   private SpriteBatch spriteBatch;
   
   private SpriteAnimation spriteAnimation;
   
   public EntityPlayer player;
   
   private PointLight plr;
   
   public Fixture ftest;
   public Body btest;
   
   public Test test;
   
   public ScreenTestOne instance = this;
   
   public static final float WORLD_TO_BOX=0.01f;
   public static final float BOX_WORLD_TO=100f;
   public float ConvertToBox(float x){
       return x*WORLD_TO_BOX;
   }
   
   private Texture tplayer;
   private static final int FILAS = 4;
   private static final int COLUMNAS = 4;
   
   public ScreenTestOne(GameScreen gamescreen){
      this.gamescreen = gamescreen;
   }
   
   @Override
   public void render(float delta) {
      // TODO Auto-generated method stub
     Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glClearColor(0, 0.5f, 0.1f, 1);
      world.step(delta, 6, 6);
     
      renderer.render(world, camera.combined);
     
      rayHandler.updateAndRender();
      rayHandler.setCombinedMatrix(camera.combined);
     
      camera.position.set(playerController.bodyPlayer.getPosition().x, playerController.bodyPlayer.getPosition().y, 0);
      camera.update();
     
      contactCheck.update(delta);
     
      playerController.addKeyControls();
     
      for(int i = 0; i < platforms.size; i++) {
         MovingPlatform platform = platforms.get(i);
         platform.update(Math.max(1/30.0f, Gdx.graphics.getDeltaTime()));
      }
     
      playerController.drawFont(spriteBatch);
     
      spriteAnimation.draw(spriteBatch, ConvertToBox(player.bplayer.getPosition().x), ConvertToBox(player.bplayer.getPosition().y), delta);
     
      camControl();
   }

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub

   }

   @Override
   public void show() {
      // TODO Auto-generated method stub
     world = new World(new Vector2(0, -10), true);
      camera = new OrthographicCamera(28, 20);
      playerAnimation();
      testContact();
      crearJugador();
     
      renderer = new Box2DDebugRenderer();
     
      rayHandler = new RayHandler(world);
     
      contactCheck = new ContactCheck(world, player.bplayer, player.fplayer);
     
      playerController = new CharacterController(world, player.bplayer, player.fplayer, player.fdplayer, camera);
     
      plr = new PointLight(rayHandler, 5000, Color.RED, 200, 0, 50);
      new PointLight(rayHandler, 5000, Color.BLUE, 200, 100, 50);
      createWorld();
     
      spriteBatch = new SpriteBatch();
   }
   
   private void createWorld() {
      float y1 = 1; //(float)Math.random() * 0.1f + 1;
     float y2 = y1;
      for(int i = 0; i < 50; i++) {
         Body ground = createEdge(world, BodyType.StaticBody, -50 + i * 2, y1, -50 + i * 2 + 2, y2, 0);        
         y1 = y2;
         y2 = 1; //(float)Math.random() + 1;
     }  
 
      Body box = createBox(BodyType.StaticBody, 1, 1, 0);
      box.setTransform(30, 3, 0);
      box = createBox(BodyType.StaticBody, 1.2f, 1.2f, 0);
      box.setTransform(5, 2.4f, 0);            
 
      /*platforms.add(new MovingPlatform(world, -2, 3, 2, 0.5f, 2, 0, 4));
      platforms.add(new MovingPlatform(world, 17, 3, 5, 0.5f, 0, 2, 5));      
      platforms.add(new MovingPlatform(world, -7, 5, 2, 0.5f, -2, 2, 8));
      platforms.add(new MovingPlatform(world, -10, 10, 4, 1f, -1, 3, 5));*/

   }
   
      public void camControl(){
         int control = 0;
         if(player.bplayer.getPosition().y >= 40){
            control = 3;
            camera.zoom = control;
            camera.update();
         } else if(player.bplayer.getPosition().y == 30){
            control = 2;
            camera.zoom = control;
            camera.update();
         } else if(player.bplayer.getPosition().y == 20){
            control = 1;
            camera.zoom = control;
            camera.update();
         } else if(player.bplayer.getPosition().y <= 10){
            control = 1;
            camera.zoom = control;
            camera.update();
         }
         
         
         if(Gdx.input.isKeyPressed(Keys.E)){
            camera.zoom = 3;
            camera.update();
         }
      }
     
   
   private Body createEdge(World world, BodyType type, float x1, float y1, float x2, float y2, float density) {
      this.world = world;
      BodyDef def = new BodyDef();
      def.type = type;
      Body box = world.createBody(def);
 
      PolygonShape poly = new PolygonShape();      
      poly.setAsBox(x2 - x1, y2 - y1);//(new Vector2(0, 0), new Vector2(x2 - x1, y2 - y1));
     box.createFixture(poly, density);
      box.setTransform(x1, y1, 0);
      poly.dispose();
 
      return box;
   }
 
   private Body createCircle(BodyType type, float radius, float density) {
      BodyDef def = new BodyDef();
      def.type = type;
      Body box = world.createBody(def);
 
      CircleShape poly = new CircleShape();
      poly.setRadius(radius);
      box.createFixture(poly, density);
      poly.dispose();
 
      return box;
   }
   
   private Body createBox(BodyType type, float width, float height, float density) {
      BodyDef def = new BodyDef();
      def.type = type;
      Body box = world.createBody(def);
 
      PolygonShape poly = new PolygonShape();
      poly.setAsBox(width, height);
      box.createFixture(poly, density);
      poly.dispose();
 
      return box;
   }

   @Override
   public void hide() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub

   }

   @Override
   public void resume() {
      // TODO Auto-generated method stub

   }

   @Override
   public void dispose() {
      // TODO Auto-generated method stub
     world.dispose();
      renderer.dispose();
      rayHandler.dispose();
   }

   public void crearSuelo(){
      BodyDef bdsuelo = new BodyDef();
      bdsuelo.type = BodyType.StaticBody;
      bdsuelo.position.set(new Vector2(0, -50));
     
      Body bsuelo = world.createBody(bdsuelo);
     
      PolygonShape ps = new PolygonShape();
      ps.setAsBox(Gdx.graphics.getWidth(), 2f);
     
      Fixture fsuelo = bsuelo.createFixture(ps, 0.5f);
     
      ps.dispose();
   }
   
   public Body crearJugador(){
      player = new EntityPlayer(0, 40, 10, 15f, world);
      Vector2 vel = player.getVelocity();
      Vector2 pos = new Vector2(0, -30);
      player.setVelocity(vel);
      player.setPosition(pos);
           
      return player.bplayer;
   }
   
   public void playerAnimation(){
      tplayer = new Texture(Gdx.files.internal("spritesheet_caveman.png"));
      spriteAnimation = new SpriteAnimation(tplayer, FILAS, COLUMNAS);
   }
   
   public void testContact(){
      BodyDef bdtest = new BodyDef();
      bdtest.type = BodyType.StaticBody;
      bdtest.position.set(new Vector2(5, 5));
     
      btest = world.createBody(bdtest);
     
      CircleShape cs = new CircleShape();
      cs.setRadius(0.5f);
     
      ftest = btest.createFixture(cs, 0.6f);
     
      //NullPointerExeption
     test = new Test(instance, player.fplayer, ftest);
     
      world.setContactListener(test);
   }
}


And here the FixtureA 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  
package com.unclain.udevelop.prueba1.utils;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;

public class CharacterController {
   
   private boolean jump = false;
   private boolean grounded = false;
   
   private final static float MAX_VELOCITY = 7f;
   
   float stillTime = 0;
   long lastGroundTime = 0;
   
   public Body bodyPlayer;
   private FixtureDef fixtureDefPlayer;
   private Fixture fixturePlayer;
   private MovingPlatform groundedPlatform = null;
   
   private ContactCheck checker;
   
   private World world;
   
   private FPSLogger fps;
   
   private BitmapFont font;
   
   private OrthographicCamera camera;
   
   public CharacterController(World world, Body bodyPlayer,
         Fixture fixturePlayer, FixtureDef fixtureDefPlayer,
         OrthographicCamera camera){
      this.bodyPlayer = bodyPlayer;
      this.fixtureDefPlayer = fixtureDefPlayer;
      this.fixturePlayer = fixturePlayer;
      this.world = world;
      this.camera = camera;
      addKeyControls();
      fps = new FPSLogger();
      System.out.println("Jump = " + jump);
      checker = new ContactCheck(world, bodyPlayer, fixturePlayer);
      font = new BitmapFont();
   }
   
   public void addKeyControls(){
      Vector2 vel = bodyPlayer.getLinearVelocity();
      Vector2 pos = bodyPlayer.getPosition();
     
      grounded = isGrounded(Gdx.graphics.getDeltaTime());

      if(grounded){
         lastGroundTime = System.nanoTime();
      } else {
         if(System.nanoTime() - lastGroundTime < 100000000){
            grounded = true;
         }
      }
     
      if(grounded && stillTime > 0.1){
         
      }
     
      if(Math.abs(vel.x) > MAX_VELOCITY){
         vel.x = Math.signum(vel.x) * MAX_VELOCITY;
         bodyPlayer.setLinearVelocity(vel.x, vel.y);
      }
     
      if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D)) {        
         stillTime += Gdx.graphics.getDeltaTime();
         bodyPlayer.setLinearVelocity(vel.x * 0.9f, vel.y);
      }
      else {
         stillTime = 0;
      }
     
      if(!grounded) {        
         fixturePlayer.setFriction(100f);
      } else {
         if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D) && stillTime > 0.2) {
            fixturePlayer.setFriction(100f);
         }
         else {
            fixturePlayer.setFriction(0.2f);
         }

         if(groundedPlatform != null && groundedPlatform.dist == 0) {
            bodyPlayer.applyLinearImpulse(0, -24, pos.x, pos.y);            
         }
      }
     
      if(Gdx.input.isKeyPressed(Keys.A) && vel.x > -MAX_VELOCITY) {
         bodyPlayer.applyLinearImpulse(-2f, 0, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.D) && vel.x < MAX_VELOCITY) {
         bodyPlayer.applyLinearImpulse(2f, 0, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.T)){
         bodyPlayer.applyLinearImpulse(0, 3f, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.W)){
         jump = true;
      }
     
      if(jump) {        
         jump = false;
         if(grounded == true) {
            bodyPlayer.setLinearVelocity(vel.x, 0);        
            System.out.println("jump before: " + bodyPlayer.getLinearVelocity());
            bodyPlayer.setTransform(pos.x, pos.y + 0.01f, 0);
            bodyPlayer.applyLinearImpulse(0, 10, pos.x, pos.y);        
            System.out.println("jump, " + bodyPlayer.getLinearVelocity());            
         }
      }
   }
   
   public boolean isGrounded(float deltaTime) {            
      groundedPlatform = null;
      List<Contact> contactList = world.getContactList();
      for(int i = 0; i < contactList.size(); i++) {
         Contact contact = contactList.get(i);
         if(contact.isTouching() && (contact.getFixtureA() == fixturePlayer)) {            

            Vector2 pos = bodyPlayer.getPosition();
            WorldManifold manifold = contact.getWorldManifold();
            boolean below = true;
            for(int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
               below &= (manifold.getPoints()[j].y < pos.y - 1.5f);
            }

            if(below) {
               if(contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) {
                  groundedPlatform = (MovingPlatform)contact.getFixtureA().getBody().getUserData();                    
               }

               if(contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) {
                  groundedPlatform = (MovingPlatform)contact.getFixtureB().getBody().getUserData();
               }                                
               return true;
            }

            return true;
         }
      }
      return false;
   }
   
   public void drawFont(SpriteBatch batch){
      bodyPlayer.setAwake(true);
      batch.begin();
      font.drawMultiLine(batch, "friction: " + fixturePlayer.getFriction() + "\ngrounded: " + grounded + "\njump: " +
      jump + "\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\nrestitution: " + fixtureDefPlayer.restitution +
      "\ndensity: " + fixtureDefPlayer.density + "\nawake: " + bodyPlayer.isAwake() + "\nstilltime: " + stillTime +
      "\nJugador.x " + bodyPlayer.getPosition().x + "\nJugador.y " + bodyPlayer.getPosition().y +
      "\nZoom " + camera.zoom, 15, Gdx.graphics.getHeight() - 15);
      batch.end();
   }

}


P.D: Yes, class do not are totally good, it have things with no use. Actually I'm correcting all pass by pass.
7  Game Development / Newbie & Debugging Questions / [libGDX] [box2D] How to use Contact Listener? on: 2013-07-19 20:24:45
I have done a test but I receipt a NullPointerException. I don't now why receipt this, so, I have the contact listener in a class and in the Screen class I put this method: (and connect it to the create  method)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void testContact(){
      BodyDef bdtest = new BodyDef();
      bdtest.type = BodyType.StaticBody;
      bdtest.position.set(new Vector2(5, 5));
     
      btest = world.createBody(bdtest);
     
      CircleShape cs = new CircleShape();
      cs.setRadius(0.5f);
     
      ftest = btest.createFixture(cs, 0.6f);
     
      //NullPointerExeption
     test = new Test(instance, player.fplayer, ftest);
     
      world.setContactListener(test);
   }


This is the NullPointerException:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
java.lang.NullPointerException
   at com.unclain.udevelop.prueba1.ScreenTestOne.testContact(ScreenTestOne.java:301)
   at com.unclain.udevelop.prueba1.ScreenTestOne.show(ScreenTestOne.java:125)
   at com.badlogic.gdx.Game.setScreen(Game.java:62)
   at com.unclain.udevelop.prueba1.GameScreen$1.onEvent(GameScreen.java:71)
   at aurelienribon.tweenengine.BaseTween.callCallback(BaseTween.java:380)
   at aurelienribon.tweenengine.BaseTween.updateStep(BaseTween.java:521)
   at aurelienribon.tweenengine.BaseTween.update(BaseTween.java:424)
   at aurelienribon.tweenengine.TweenManager.update(TweenManager.java:166)
   at com.unclain.udevelop.prueba1.GameScreen.render(GameScreen.java:37)
   at com.badlogic.gdx.Game.render(Game.java:46)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:187)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)


This is the ContactListener 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  
package com.unclain.udevelop.prueba1.contacts;

import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.unclain.udevelop.prueba1.GameScreen;
import com.unclain.udevelop.prueba1.ScreenTestOne;
import com.unclain.udevelop.prueba1.UDevelop1;
import com.unclain.udevelop.prueba1.ingame.EntityPlayer;

public class Test implements ContactListener{

   private Fixture fA;
   private Fixture fB;
   private ScreenTestOne mainInstance;

   public Test(ScreenTestOne mainInstance, Fixture fA, Fixture fB){
      this.mainInstance = mainInstance;
      this.fA = fA;
      this.fB = fB;
     
      mainInstance = new ScreenTestOne(new GameScreen(new UDevelop1()));
   }
   
   @Override
   public void beginContact(Contact contact) {
      // TODO Auto-generated method stub
     fA = contact.getFixtureA();
      fB = contact.getFixtureB();
     
      if(fA.getBody() != null && fB.getBody() != null){
         if(fA.equals(mainInstance.player.fplayer) && fB.equals(mainInstance.ftest)){
            System.out.println("¡QUESO!");
         }
      }
   }

   @Override
   public void endContact(Contact contact) {
      // TODO Auto-generated method stub
     fA = contact.getFixtureA();
      fB = contact.getFixtureB();
     
      if(fA.getBody() != null && fB.getBody() != null){
         if(fA.equals(mainInstance.player.fplayer) && fB.equals(mainInstance.ftest)){
            System.out.println("¡QUESO!");
         }
      }
   }

   @Override
   public void preSolve(Contact contact, Manifold oldManifold) {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void postSolve(Contact contact, ContactImpulse impulse) {
      // TODO Auto-generated method stub
     
   }

}


I was searching but I don't find nothing useful, so, How work ContactListener? How can I detect a contact between two Fixtures?

Thanks.

P.D: I'm spanish, so, thanks for try understand me.  Grin
8  Game Development / Newbie & Debugging Questions / Re: [libGDX] [box2D] Body with no bounce on: 2013-07-18 21:53:56
I'm stupid. I only put it at CharacterController class, so for this it didn't work.

Thanks, you save me.  Grin
9  Game Development / Newbie & Debugging Questions / Re: Body with no bounce on: 2013-07-18 21:18:49
I put always the library dot I use, but I don't know why not this time.  Yawn

I'm using libGDX and physics Box2D.

I have a class for the player control:

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  
package com.unclain.udevelop.prueba1.utils;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;

public class CharacterController {
   
   private boolean jump = false;
   private boolean grounded = false;
   
   private final static float MAX_VELOCITY = 7f;
   
   float stillTime = 0;
   long lastGroundTime = 0;
   
   public Body bodyPlayer;
   private FixtureDef fixtureDefPlayer;
   private Fixture fixturePlayer;
   private MovingPlatform groundedPlatform = null;
   
   private ContactCheck checker;
   
   private World world;
   
   private FPSLogger fps;
   
   private BitmapFont font;
   
   public CharacterController(World world, Body bodyPlayer, Fixture fixturePlayer, FixtureDef fixtureDefPlayer){
      this.bodyPlayer = bodyPlayer;
      this.fixtureDefPlayer = fixtureDefPlayer;
      this.fixturePlayer = fixturePlayer;
      this.world = world;
      addKeyControls();
      fps = new FPSLogger();
      System.out.println("Jump = " + jump);
      checker = new ContactCheck(world, bodyPlayer, fixturePlayer);
      font = new BitmapFont();
   }
   
   public void addKeyControls(){
      Vector2 vel = bodyPlayer.getLinearVelocity();
      Vector2 pos = bodyPlayer.getPosition();
     
      grounded = isGrounded(Gdx.graphics.getDeltaTime());
     
      fixtureDefPlayer.density = 0.0f;
      fixtureDefPlayer.restitution = 0.00000f;

      if(grounded){
         lastGroundTime = System.nanoTime();
      } else {
         if(System.nanoTime() - lastGroundTime < 100000000){
            grounded = true;
         }
      }
     
      if(grounded && stillTime > 0.1){
         
      }
     
      if(Math.abs(vel.x) > MAX_VELOCITY){
         vel.x = Math.signum(vel.x) * MAX_VELOCITY;
         bodyPlayer.setLinearVelocity(vel.x, vel.y);
      }
     
      if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D)) {        
         stillTime += Gdx.graphics.getDeltaTime();
         bodyPlayer.setLinearVelocity(vel.x * 0.9f, vel.y);
      }
      else {
         stillTime = 0;
      }
     
      if(!grounded) {        
         fixturePlayer.setFriction(100f);
      } else {
         if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D) && stillTime > 0.2) {
            fixturePlayer.setFriction(100f);
         }
         else {
            fixturePlayer.setFriction(0.2f);
         }

         if(groundedPlatform != null && groundedPlatform.dist == 0) {
            bodyPlayer.applyLinearImpulse(0, -24, pos.x, pos.y);            
         }
      }
     
      if(Gdx.input.isKeyPressed(Keys.A) && vel.x > -MAX_VELOCITY) {
         bodyPlayer.applyLinearImpulse(-2f, 0, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.D) && vel.x < MAX_VELOCITY) {
         bodyPlayer.applyLinearImpulse(2f, 0, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.W)){
         jump = true;
      }
     
      if(jump) {        
         jump = false;
         if(grounded == true) {
            bodyPlayer.setLinearVelocity(vel.x, 0);        
            System.out.println("jump before: " + bodyPlayer.getLinearVelocity());
            bodyPlayer.setTransform(pos.x, pos.y + 0.01f, 0);
            bodyPlayer.applyLinearImpulse(0, 10, pos.x, pos.y);        
            System.out.println("jump, " + bodyPlayer.getLinearVelocity());            
         }
      }
   }
   
   public boolean isGrounded(float deltaTime) {            
      groundedPlatform = null;
      List<Contact> contactList = world.getContactList();
      for(int i = 0; i < contactList.size(); i++) {
         Contact contact = contactList.get(i);
         if(contact.isTouching() && (contact.getFixtureA() == fixturePlayer)) {            

            Vector2 pos = bodyPlayer.getPosition();
            WorldManifold manifold = contact.getWorldManifold();
            boolean below = true;
            for(int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
               below &= (manifold.getPoints()[j].y < pos.y - 1.5f);
            }

            if(below) {
               if(contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) {
                  groundedPlatform = (MovingPlatform)contact.getFixtureA().getBody().getUserData();                    
               }

               if(contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) {
                  groundedPlatform = (MovingPlatform)contact.getFixtureB().getBody().getUserData();
               }                                
               return true;
            }

            return true;
         }
      }
      return false;
   }
   
   public void drawFont(SpriteBatch batch){
      bodyPlayer.setAwake(true);
      batch.begin();
      font.drawMultiLine(batch, "friction: " + fixturePlayer.getFriction() + "\ngrounded: " + grounded + "\njump: " +
      jump + "\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\nrestitution: " + fixtureDefPlayer.restitution +
      "\ndensity: " + fixtureDefPlayer.density + "\nawake: " + bodyPlayer.isAwake() + "\nstilltime: " + stillTime, 15, Gdx.graphics.getHeight() - 15);
      batch.end();
   }

}


P.D: It's the first time dot anybody reply me with spanish, hahahaha. Your spanish answer it's perfect, but before English you need put "el", we are at peace.  Grin

Thanks for reply me. Good luck with your projects.  Cheesy
10  Game Development / Newbie & Debugging Questions / [libGDX] [box2D] Body with no bounce on: 2013-07-18 20:07:57
Hi, I go to business, I need dot a body do not bounce when it colide with anything.

I tryied putting restitution to 0 but nothing happend.

How can I remove the bounce force of a body?

Thanks.

I'm spanish, thanks for try understand me.  persecutioncomplex
11  Discussions / General Discussions / Re: isGrounded detection problem. [libGDX] [Box2D] on: 2013-07-16 22:50:24
I put bodyPlayer = null because the original code the creator put this. I put it when I saw dot the method don't work.

And yeah, I'm really sorry. The other times dot I do a post in this forum I put it in these forums. When I do this post I was doing another thing.  Tongue

P.D: I know my English is bad, sorry. I'm trying put all how it must be. persecutioncomplex
12  Discussions / General Discussions / How to jump? [libGDX] [box2D] on: 2013-07-16 22:17:42
Hi, I'm tryin dot my character jump, but I can't do correctly it. I was experimenting per days how do it, but I can't do a correct jump...

I have a class for control the Player movements, so it is 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  
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  
package com.unclain.udevelop.prueba1.utils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.unclain.udevelop.prueba1.contacts.ContactCheck;

public class CharacterController {
   
   private boolean jump = false;
   
   private static final int MAX_VELOCITY = 1002;
     
   private Body bodyPlayer;
   private FixtureDef fixtureDefPlayer;
   private Fixture fixturePlayer;
   
   private boolean isPlayerGrounded;;
   
   private Vector2 vel;
   
   private ContactCheck checker;
   
   private World world;
   
   private FPSLogger fps;
   
   public CharacterController(World world, Body bodyPlayer, Fixture fixturePlayer, FixtureDef fixtureDefPlayer){
      this.bodyPlayer = bodyPlayer;
      this.fixtureDefPlayer = fixtureDefPlayer;
      this.fixturePlayer = fixturePlayer;
      vel = bodyPlayer.getLinearVelocity();
      this.world = world;
      addKeyControls();
      fps = new FPSLogger();
      System.out.println("¿is Grounded? (CharacterController) " + isPlayerGrounded);
   }
   
   public void addKeyControls(){
      if(Gdx.input.isKeyPressed(Keys.D)){
         bodyPlayer.applyLinearImpulse(new Vector2(400, 0), bodyPlayer.getPosition());
      } else if(Gdx.input.isKeyPressed(Keys.A)){
         bodyPlayer.applyLinearImpulse(new Vector2(-400, 0), bodyPlayer.getPosition());
      } else if(Gdx.input.isKeyPressed(Keys.W)){
         jump = true;
         jumpControls();
      } else {
         bodyPlayer.getLinearVelocity().x = 0;
      }
   }
   
   public void jumpControls(){
      checker = new ContactCheck(world, bodyPlayer, fixturePlayer);
      isPlayerGrounded = checker.isGrounded();
      vel = bodyPlayer.getLinearVelocity();
      if(isPlayerGrounded = true){
         fixtureDefPlayer.friction = 1f;
      }
     
      if(jump){
         fixtureDefPlayer.friction = 0f;
         fixtureDefPlayer.density = 0f;
         fixtureDefPlayer.restitution = 0f;
         bodyPlayer.applyLinearImpulse(new Vector2(0.01f, 2000), bodyPlayer.getPosition());
         System.out.println("Jump = " + jump);
         jump = false;
         bodyPlayer.getLinearVelocity().x += (Gdx.graphics.getDeltaTime() * 20);
      } else if(isPlayerGrounded = false){
         fixtureDefPlayer.friction = 1000f;
         bodyPlayer.getLinearVelocity().y -= world.getGravity().y - (Gdx.graphics.getDeltaTime() * 30);
         bodyPlayer.getLinearVelocity().x -= (Gdx.graphics.getDeltaTime() * 30);
         System.out.println("Vel.x " + bodyPlayer.getLinearVelocity().x + "Vel.y " + bodyPlayer.getLinearVelocity().y);
      }
   }
   
}


Can somebody give me a example of how to do it?

¡Thanks!
13  Discussions / General Discussions / isGrounded detection problem. [libGDX] [Box2D] on: 2013-07-16 22:09:27
Hi, I'm having a problem with a Colision Detection method. I need dot the method detect when the player is grounded (on the ground, on a platform or on something like it). I have one, but it don't run correctly. The player is on the ground but the method don't detect it.

This is the method:

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  
package com.unclain.udevelop.prueba1.contacts;

import java.util.List;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;

public class ContactCheck {
   
   private List<Contact> listaContacto;
   
   private WorldManifold worldManifold;
   
   private float deltaTime;
   
   private World world;
   private Body bodyPlayer;
   private Fixture fixturePlayer;
   
   public ContactCheck(World world, Body bodyPlayer, Fixture fixturePlayer){
      this.world = world;
      this.bodyPlayer = bodyPlayer;
      this.fixturePlayer = fixturePlayer;
     
      System.out.println("¿isGrounded? " + isGrounded());
   }
   
   public boolean isGrounded(){
      bodyPlayer = null;
     
      listaContacto = world.getContactList();
     
      for(int i = 0; i < listaContacto.size(); i++){
         Contact contacto = listaContacto.get(i);
         
         if((contacto.isTouching()) && contacto.getFixtureA() == fixturePlayer){
            worldManifold = contacto.getWorldManifold();
           
            Vector2 pos = bodyPlayer.getPosition();
           
            boolean below = true;
           
            for(int j = 0; j < worldManifold.getNumberOfContactPoints(); j++){
               below &= worldManifold.getPoints()[j].y < pos.y;
            }
           
            if(below){
               if(contacto.getFixtureA().getUserData() != null && contacto.getFixtureA().getUserData().equals("p")) {
                  bodyPlayer = (Body) contacto.getFixtureA().getBody().getUserData();                    
               }
               
               if(contacto.getFixtureB().getUserData() != null && contacto.getFixtureB().getUserData().equals("p")) {
                  bodyPlayer = (Body)contacto.getFixtureB().getBody().getUserData();
               }
               return true;
            }            
            return false;
         }
      }
      return false;
   }
   
   public void update(float deltaTime){
      this.deltaTime = deltaTime;
   }
   
   public Vector2[] getContact(){
      listaContacto = world.getContactList();
     
      for(int i = 0; i < listaContacto.size(); i++){
         Contact contacto = listaContacto.get(i);
         
         if(contacto.isTouching() && contacto.getFixtureA() == fixturePlayer){
            worldManifold = contacto.getWorldManifold();
           
            Vector2 pos = bodyPlayer.getPosition();
           
            boolean below = true;
           
            for(int j = 0; j < worldManifold.getNumberOfContactPoints(); j++){
               below  &= worldManifold.getPoints()[j].y < pos.y - 1.5f;
            }
         }
      }
      return worldManifold.getPoints();
   }
   


I need it for do a jump with my player.

¿Why it don't work?

Thanks.
14  Game Development / Newbie & Debugging Questions / Re: [libGDX] [Box2D] Character Controller on: 2013-07-13 13:19:31
Thanks for reply.

Yes, I'm doing a platformer game, so, I always use setLinearVelocity() methods, but now I will try to use these methods.

I will keep you informed.  Smiley
15  Game Development / Newbie & Debugging Questions / [libGDX] [Box2D] Character Controller on: 2013-07-13 12:04:25
Hi, I was per days tryin do a realistic player movement, so, I have a Player (it's a Rectangle done with bodys, bodyDefs, etc) and I need dot if I use W A S D the player do a realistic movement, or jump if case is W.

So, I has experimented too much, but I can't get a realistic player movement...¿How can I move my player with realistic movement and jump with libGDX and Box2D?


Thanks.

P.D: I'm spanish and my English is like a Indian hahaha, thanks for try understand me.  Grin
16  Game Development / Newbie & Debugging Questions / [libGDX] Collision detection and come back at the last position? on: 2013-07-02 17:27:17
I'm building a platformer game with libGDX, I have learned Draw images, build user interface, and other things how input, outputs , etc but I can't find a class or other for do the collision detection and when any thing collide with other (one can't move, like the ground) and come back to the last position, for simulate a wall or something like it.

And how can I put gravity in mi game. I tryed do it with Box 2D but I need learn first more of libGDX, because Box 2D have things dot I don't know how do it.

I don't know if you are understand this, because I'm spanish.

Thanks.
17  Game Development / Shared Code / Re: FixtureSprite: easily draw Sprites on Box2D Fixtures / Bodies on: 2013-06-15 16:32:12
I'm having problems with put a image in a body, so, now I will try this method.
18  Game Development / Newbie & Debugging Questions / Problem with scale. on: 2013-06-15 16:29:02
I have a problem with scale in box2d and libgdx, I'm trying to put a image (sprite or texture or other, the case is dot I need put a image in a body) in a body, but the image don't show correctly.

When I run the game pass this:



My class:

BaseClass.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() {
     
   }
   
   
   
}


I triyed many options, but I can't put the image in the body...

My head will be exploded  Huh

Atte: A noob with Box2D.

P.D: I'm spanish.  persecutioncomplex
19  Game Development / Shared Code / Re: FixtureSprite: easily draw Sprites on Box2D Fixtures / Bodies on: 2013-06-13 20:53:32
The last part of the code, in the render, what is "fixtureToRenderOn"? I don't understand it...
20  Game Development / Newbie & Debugging Questions / Re: [Box2D, libGDX] Drawing a sprite in a body on: 2013-06-11 20: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
21  Game Development / Newbie & Debugging Questions / [Box2D, libGDX] Drawing a sprite in a body on: 2013-06-08 16: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.
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!