Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [box2D] how positioning and scaling work  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-06-02 01:29:04 »

Hello,
i start looking for some Box2D examples today and everything is working fine except that i don't understand the creation of a simple rectangle inside Box2D (polygon), if am right the steps needed to create a box2D rectangle are the followings

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
       

      BodyDef polygonDef = new BodyDef();
      polygonDef.position.set(x, y);
      polygonDef.type = BodyType.DynamicBody;

      Body polygonBody = world.createBody(polygonDef);

      PolygonShape polygonShape = new PolygonShape();
      polygonShape.setAsBox(width, height);

      fixture.shape = polygonShape;
      polygonBody.createFixture(fixture);

     


and to create a Rectangle in libGdx we do this :
1  
2  
3  
      shapeRenderer.begin(ShapeType.FilledRectangle);
      shapeRenderer.filledRect(x, y, width, height);
      shapeRenderer.end();



now supposing i create a libGdx rectangle with the following coordinates :
x =100, y=100, width=100, height=100
in order to have a Box2D rectangle in the same position with the same size, i had to use the following coordinates :
x = 30, y=30,width=10,height=10;

i tried to figure out the relation between them but i didn't succeed, i know that box2D use meters instead of pixels but what i don't know is 1meter=??pixels, and how i can use Box2D with "simple" coordination

thank you 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline pitbuller
« Reply #1 - Posted 2013-06-02 08:30:53 »

Use debug renderer with camera that match with box2d coordinates. Meters are simple, pixels are evil. It's easy to know how long car should be in meters but you cannot say  that without context and screen size with pixels.

Everything is logical and easy when you plan game logic with meters and match rendering with physics size and let camera scale that to screen. Zero magic constant, faster and better precision math. No downsides.

Ps. One added bonus is that when you understand benefits of camera with 2d graphics then translation to 3d is lot easier.
Offline Andre Lopes
« Reply #2 - Posted 2013-06-02 13:00:37 »

box2d use meters? Whats the difference then?

Also, that positions could be a camera issue, right?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kutucuk
« Reply #3 - Posted 2013-06-02 13:32:27 »

If I did not scale anything at all, this works for me:
1  
sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);


About scaling, well this is also new for me. First thing to know about this is Box2D uses meter and its calculation are correct if the body dimensions are between 0.1 meters and 10 meters. So, if you don't do any scaling, your 20x20 sprite will be 20 meters x 20 meters in Box2D.

Like I said, I am still experiencing about this. But people use some constants for this, which are:
1  
2  
static final float WORLD_TO_BOX = 0.01f;
   static final float BOX_TO_WORLD = 100f;


Before I started to think about scaling, I thought these are the standard scaling constants but they are not. I mean, you could set WORLD_TO_BOX to 0.1f or even 2f if that's the thing that will make your game work.

Here is something I use to experiment. Do not assume these are correct, because I might encounter some problems with the way I do things later;
I have a class which creates bodies. You pass some variables to that class. The method parameters are like this:
1  
boxFactory.charBody(position, halfX, halfY, density, friction, restitution)

position is a Vector2. The rest are float values. My WORLD_TO_BOX and BOX_TO_WORLD values are 0.1f and 10f. So I have a ship, and I have a body for that. This is my body:
1  
shipBody = boxFactory.charBody(new Vector2(40, 30), 3.2f, 3.2f, 0.1f, 1f, 0f);

I probably should have multiplied 32*WORLD_TO_BOX instead of giving 3.2f but like I said, experimenting right now.

I have a 64x64 image as my ship sprite. So I scale it:
1  
sprite.setScale(MyBox.WORLD_TO_BOX);

MyBox is my class which creates bodies and WORLD_TO_BOX is a static constant of that class.

I am not entirely sure about my camera setup, but this is how I did it:
1  
2  
camera = new OrthographicCamera();
   camera.setToOrtho(false, Gdx.graphics.getWidth() * MyBox.WORLD_TO_BOX, Gdx.graphics.getHeight() * MyBox.WORLD_TO_BOX);

I think this is not correct, because when I put this into resize() method, things are mixed up.

To sum up, 1 meter = (whatever you want) pixels.

This is the thread I tried to get help: http://www.java-gaming.org/topics/libgdx-box2d-scaling-between-world-and-box/29743/view.html
Philbedy has a different approach to that. You might try that as well.

EDIT: I think scaling is almost mandatory, because a 32x32 sprite will be 32x32 meters and this will mess up box2d simulation. I wasn't able to find a good guide (well no guide at all) about how scaling should go. And I think the best way to experiment this is to build a working example without scaling, and then trying to implement scaling to it.
BTW, I just set up a InputProcessor and its touchDown method is like this:
1  
2  
3  
4  
5  
6  
   @Override
   public boolean touchDown(int screenX, int screenY, int pointer, int button) {
      System.out.println("X:" + screenX + " Y: " + screenY);
      System.out.println(world.getEnemyBody().getPosition());
      return true;
   }

And when I click on the enemy body, the output is like this:
Quote
X:800 Y: 376
[80.0:30.0]
This might help you (and me) understand the scaling thing better.
Offline alaslipknot
« Reply #4 - Posted 2013-06-03 02:25:40 »

EDIT : i added "batch.setProjectionMatrix(camera.combined);" and it works

Ps. One added bonus is that when you understand benefits of camera with 2d graphics then translation to 3d is lot easier.
which am currently trying to do, the wiki example of the Orthographic Camera is a kinda hard, i really don't understand why the guys from badLogic don't want to keep it simple  Undecided (am talking about the "mesh" thing which i have zero idea about)
anyway,
i tried to follow the wiki example, but i didn't succeed to do the "camera.translate" and "camera.zoom", however i was able to create  the camera effect by changing the viewPort rectangle x,y,width and height, here is the codes,

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

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class MyGdxGame implements ApplicationListener {
   // input
   private boolean leftPressed, rightPressed, upPressed, downPressed,
         wPressed, xPressed;

   // 2d graphics
   SpriteBatch batch; // used to draw textures
   Texture texture;// the texture
   TextureRegion region;// part of the texture

   // 2d camera
   OrthographicCamera camera;// camera
   float width, height;// width and height of the camera view port
   Rectangle glViewport;// camera view port
   

   @Override
   public void create() {
      // initilizing with and height
      width = 480;
      height = 320;

      // new SpriteBatch
      batch = new SpriteBatch();
      // loading texture file
      texture = new Texture("data/map.png");
      // drawing a part from the texture (startX,startY,endX,endY)
      region = new TextureRegion(texture, 0, 0, 100, 100);

      // setting the camera
      camera = new OrthographicCamera(width, height);
      camera.position.set(width / 2, height / 2, 0);
      glViewport = new Rectangle(0, 0, width, height);

   }

   @Override
   public void render() {

      update();

      GL10 gl = Gdx.graphics.getGL10();

      // Camera --------------------- /
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      gl.glViewport((int) glViewport.x, (int) glViewport.y,
            (int) glViewport.width, (int) glViewport.height);

      camera.update();
      camera.apply(gl);

      // Drawing -------------------/
      batch.begin();
      batch.draw(texture, 10, 10);
      // batch.draw(region, 10,10);
      batch.end();

   }

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

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

   @Override
   public void dispose() {

   }

   private void update() {
      control();
      controlCall();

   }

   private void controlCall() {

      if (rightPressed) {

         camera.zoom -= 1; //doesn't work
         glViewport.x -= 1;
      }

      if (leftPressed) {
         glViewport.x += 1;
      }

      if (upPressed) {
         glViewport.y -= 1;
      }

      if (downPressed) {
         glViewport.y += 1;
      }

      if (wPressed) {
         glViewport.height += 1;
         glViewport.width += 1;
      }

      if (xPressed) {
         glViewport.height -= 1;
         glViewport.width -= 1;
      }

   }

   private void control() {

      leftPressed = Gdx.input.isKeyPressed(Keys.DPAD_LEFT);

      rightPressed = Gdx.input.isKeyPressed(Keys.DPAD_RIGHT);

      upPressed = Gdx.input.isKeyPressed(Keys.DPAD_UP);

      downPressed = Gdx.input.isKeyPressed(Keys.DPAD_DOWN);

      wPressed = Gdx.input.isKeyPressed(Keys.W);

      xPressed = Gdx.input.isKeyPressed(Keys.X);

   }
}
   

i tried to use translate and zoom in this example but as i said, i failed, now my questions are :
-is it okay to manipulate the camera this way ??
-from this point, how to correctly start with box2D ??
a shared code example will be very appreciate it, but please keep it simple  Tongue

thank you very much

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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.

DarkCart (18 views)
2014-10-31 21:44:48

DarkCart (23 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (45 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (31 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

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

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!