Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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] [box2D] Scale a Sprite to a Body  (Read 461 times)
0 Members and 1 Guest are viewing this topic.
Offline SrFurias

Senior Newbie





« Posted 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.
Offline Grunnt

JGO Wizard


Medals: 54
Projects: 9
Exp: 5 years


Complex != complicated


« Reply #1 - Posted 2013-08-30 10:22:31 »

I'm not 100% sure but aren't you supposed to give the Box2D renderer its own projection matrix?

For example in your render code (Matrix debugMatrix was declared earlier):
1  
2  
3  
debugMatrix.set(cam.combined);
debugMatrix.scale(BOX_TO_WORLD, BOX_TO_WORLD, 1f);
renderer.render(world, debugMatrix);

Offline SrFurias

Senior Newbie





« Reply #2 - Posted 2013-08-30 20:34:59 »

I tryed put it but now the world don't render...   Lips Sealed
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phibedy

Senior Member


Medals: 8



« Reply #3 - Posted 2013-08-31 01:33:23 »

Why are you using BOX_TO_WORLD-value if you are using a camera?
You are rendering the box2d-world with the same camera as you do it with your sprite.
1  
2  
      sprite.setPosition(BOX_TO_WORLD * bPlayer.getPosition().x - sprite.getWidth() / 2,
            BOX_TO_WORLD * bPlayer.getPosition().y - sprite.getHeight() / 2);

Just remove BOX_TO_WORLD.
Steps:
1. you zoom in
2. render box2dworld
3. modify the position with BOX_TO_WORLD value
4. render sprite
-> can't work, the camera already did it for you, step 3 isn't needed.
think of a straight line in a coordinate system.
y = x
What you do is  y = BOX_TO_WORLD*x
-> if(box_to_world > 1) the sprite is more distant to Zero
-> if(box_to_world < 1) the sprite is closer to Zero

In my opinion there is no reason for a BOX_TO_WORLD value, if you want a smaller box2d-world use camera and zoom in and it will look the same.
I know that a lot of tutorials are using such values, but I don't agree with them.
hope it helps
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.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!