Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML and Box2D meters per pixel efficiency  (Read 4421 times)

0 Members and 1 Guest are viewing this topic.

Orobu

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML and Box2D meters per pixel efficiency
« on: June 09, 2012, 06:01:52 pm »
Hello :)

I am working on a game in SFML and Box2D and was wondering what you think is the best way to combat the "meters per pixel" (or pixels per meter) issue with Box2D. I have searched around the forum and seen that many people seem to use a (for example) "#define PIXELS_PER_METER 64.0f" and then pass that into Box2D's constructor when creating a box shape.

Correct me if I'm wrong but won't that create VERY large objects? which in it's own turn will cause strange physics simulation and/or bad performance?

I made a game previously with SDL/OpenGL and Box2D and there we scaled the entire world with glScale and adapted accordingly. needles to say, it was horrible...

Is there any way to do this efficiently?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
Re: SFML and Box2D meters per pixel efficiency
« Reply #1 on: June 09, 2012, 07:33:12 pm »
I don't quite get what you're talking about...

As stated by the docs of Box2D it's best to choose a scale that would be equally in real life (thus 1(real)meter = 1(box2d)meter).
Then you can either convert the sizes from Box2D to Pixels as used by SFML or you could draw everything as is (if the sacle is big enough) and then just zoome out with a sf::View, although this might generate some graphical artefacts.

Now what exactly is your question about efficency?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Orobu

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML and Box2D meters per pixel efficiency
« Reply #2 on: June 09, 2012, 08:27:22 pm »
Sorry if my post was unclear.

I was merely wondering what would be the best way to mix SFML and Box2D so they work together. for example if I have a box at 128x128 pixels, and I want to create a fixture for a body in Box2D to match with it. What would be the best way to do this?

EDIT: I currently have all Box2D boxes scaled to pixels but this is causing some performance issues and the physics isn't bheaving as it should. how would I go about to scale it with view?

« Last Edit: June 09, 2012, 11:51:30 pm by Orobu »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
Re: SFML and Box2D meters per pixel efficiency
« Reply #3 on: June 10, 2012, 12:43:28 am »
I guess you haven't read the documentation of Box2D very well, because it states to NOT scale Box2D to pixel... ::)

Also the FAQ has a good example under the title "How do I convert pixels to meters?".

Quote
Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498). Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1 - 10 meters, with 1 meter being the sweet spot.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Orobu

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML and Box2D meters per pixel efficiency
« Reply #4 on: June 10, 2012, 02:06:24 am »
So I keep box2D as it is and scale all my graphics so it fits my chosen scaling factor?

I suppose that means I will have to zoom the sf::View as well, or how will I handle the sprite scaling with SFML?

capz

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: SFML and Box2D meters per pixel efficiency
« Reply #5 on: June 10, 2012, 02:14:45 am »
why don't you just take an object, such as a door, or a character, estimate what it's size would be in real life, and use that in relation with the pixel size of the sprite(assuming you use those) to figure out a good ratio? Should give you some decent physics, and I believe that's what they (box2d tutorials) recommend you do as well

 

anything