Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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] Scaling between World and Box  (Read 5107 times)
0 Members and 1 Guest are viewing this topic.
Offline kutucuk

Senior Devvie


Medals: 5
Exp: 3 years



« Posted 2013-05-30 12:33:20 »

So I'm trying to learn my way in Libgdx and Box2d nowadays. But there are some concepts I don't get.

First is, scaling. Both in Box2D Manual (PDF one) and Libgdx documentation, they say that we should scale our bodies and entities between box and world to get desired effects. And they give an example of two constants to achieve this easily:
1  
2  
static final float WORLD_TO_BOX = 0.01f;
   static final float BOX_TO_WORLD = 100f;

I was trying to make a bullet when I realized I never used these and so far (I am doing very little right now, exams for 1 month Sad ) nothing went wrong. For example, here is my bullet constructor (Because I am just testing, I preferred to create both sprite and body in the same place):
1  
2  
3  
4  
5  
6  
7  
8  
9  
public MyBullet(MyWorld world) {
      this.world = world;
      sprite = new Sprite(MyAtlas.get("bullet"));
      sprite.setScale(1 / 2f);
      myBox = new MyBox(world.getWorld());
      body = myBox.dynamicBody(new Vector2(world.getShipBody().getPosition().x, world.getShipBody().getPosition().y), sprite.getWidth() / 4,
            sprite.getHeight() / 4, 1f, 1000f, 0f);
      body.setGravityScale(0);
   }

I set halfWidth as sprite.getWidth() / 4 because I scale the sprite down by 2. And this is my draw method:
1  
2  
3  
sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
      sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
      sprite.draw(world.getBatch());


Nothing is going wrong there and I did not scale a thing. So, why are we supposed to scale sprites and bodies to/from World to/from Box?
And how do we do that? Because if you consider the constants they give as example, scaling is by 100. I have 32x32 images, so it will be 0.32x0.32...

Is it something relative and am I just assuming that 1 pixel = 1 meter?

And there is another question on my mind which is about creating bodies and entities.
Where should I create body and entity? In the same class (maybe a createBody() method)? Or should I have a kind of 'Box Factory'? Or something else? What is your approach to that?

Offline Phibedy

Senior Devvie


Medals: 9



« Reply #1 - Posted 2013-05-30 14:00:48 »

Scale:
There is a speed-limit, so you need to scale down the world to have fast moving bodies/change something in the config (don't know if it's possible in libgdx), also it's not recommended.
1 pixel = 1 m is a bad idea, bodies will move max 120 px/s.
It's better to use a camera that handles the render-size, I wouldn't use pixelToBox2dMeter-values.


Create Box2d-stuff;
Atm I am saving the objects in json-files so I don't need to "create" them manually.
Some time ago, I used "createDefaultBox2dBody()" inside the entity.class,
for creating different Bullets I had
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
class bulletUtiles.class

public static Bullet createBullet(Bullettype type){
switch(type)
case....

}

public enum Bullettype{
//usefull names
}
Offline kutucuk

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #2 - Posted 2013-05-31 12:07:55 »

So it appears that my game assumes 1 pixel = 1 meter... Hence the bullet has the same speed as the tank itself  Grin
What kind of disadvantages do these BOX_TO_WORLD and WORLD_TO_BOX constants have?

And I am sorry but I wasn't able to even think of a camera that handles render size. How would I do that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #3 - Posted 2013-05-31 12:16:51 »

This is the single most difficult concept I had to grasp when I was starting out with LibGDX and Box2D Tongue

At first, things may probably be fine, but I believe that if you choose to stick to 1 pixel = 1 meter... there comes a point where if you create object too big and too heavy just so you could render it the way you want on-screen, the physics simulation becomes inaccurate.

I forgot for what mass/size Box2D starts to have problems...

Offline kutucuk

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #4 - Posted 2013-05-31 12:42:46 »

I hardly find time for learning (final exams etc) so I start small. I mean one thing a day, in 1 hour.

So when I read about that scaling thing and started to write my classes, I was waiting for the point where I will use the scaling constants I talk about. But because I only have one character and one 'enemy'. 2 days ago, I sat down and made a bullet. It does everything I'd expect from it, except for the speed Cheesy Tank and bullet has the same speed. So I figured this is about scaling.

But I can't find out how I can fix this.

I just need to see some classes where the author had to scale the box to world, or scale the sprite to box.

Any pointers, clues or links will be appreciated.
Offline Phibedy

Senior Devvie


Medals: 9



« Reply #5 - Posted 2013-05-31 12:53:38 »

Start with: http://code.google.com/p/libgdx/wiki/OrthographicCamera
and: http://code.google.com/p/libgdx/wiki/SpriteBatch
Afterwards just create box2d-bodies, don't care about box2d-toWorld-Size etc.
If you zoom in, the box2d-bodies seems to get bigger, but that's just because you zoom in. Of course you can also zoom out.
As you will use a spritebatch, the everything will be done for you Smiley
Offline kutucuk

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #6 - Posted 2013-05-31 13:25:06 »

Philbedy,
I read these before. Actually I use both camera and spritebatch like they explain there. But I read them again, with paying more attention. Still I cannot find anything that would help me with designing a camera that handles render-size.
I already use a camera and a spritebatch. I set the spritebatch projection matrix to camera.combined. Maybe that's what I should change?

Actually, my initial issue was not that. My bullet goes too slow. That should be because of scaling. I mean 1 pixel should be 5 or 10 meters so that I can slow down my ship and bullet seems like it's going faster I am thinking...
Offline Phibedy

Senior Devvie


Medals: 9



« Reply #7 - Posted 2013-05-31 15:11:18 »

just use camera.zoom = "some float"
if you set camera.zoom = 0.1f your objects are rendered bigger,
if you set camera.zoom = 2f your objects are rendered smaller.
Offline kutucuk

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #8 - Posted 2013-05-31 15:30:57 »

Ah... We had a big misunderstanding here. Probably my thread title is the cause of that. And instead of telling my real problem, I asked about the method I thought to be the solution to that.

I have a tank and a bullet. Although I apply 1000 N force to tank and 10000000 N force to the bullet, they move at the same speed. Because Box2D has a speed limit. I wanted to know how to overcome this.
Not about zooming or scaling images. Actually, I ask about scaling between box2d and libgdx.

Phibedy I apologize for taking your time.

Offline Phibedy

Senior Devvie


Medals: 9



« Reply #9 - Posted 2013-05-31 15:38:55 »

np  Smiley
just reduce the size of everything and zoom in -> the speed won't increase, but:
steps:
0. looks like always
1. you reduce the size of all box2d-bodies
2. you zoom in (camera)
-> looks like always
3. apply forces
-> your bullet now seems to move faster (indeed it isn't moving faster).
4. scale forces, so the tank will not be as fast as the bullet
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (86 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (81 views)
2014-12-05 12:39:02

SHC (91 views)
2014-12-03 16:27:13

CopyableCougar4 (98 views)
2014-11-29 21:32:03

toopeicgaming1999 (157 views)
2014-11-26 15:22:04

toopeicgaming1999 (154 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!