Big G the same at various screen sizes....

am using Box2D for my physics, and I am calculating magnetism on objects. What I have noticed now is when I change the screen size the objects take longer to attract to each other vs. a small screen size, which makes logical sense as the distance is less.


So how can I take this formula and make it use the same BIG G constant and be screen size independent?


Thanks!!

Code:

const float BIG_G = .2f;

for(b2Body* b1 = world->GetBodyList(); b1; b1 = b1->GetNext())   
    {
        void* data1 = b1->GetUserData();
        b2Vec2 pi = b1->GetWorldCenter();
        float mi = b1->GetMass();
        for(b2Body* b2 =world->GetBodyList(); b2; b2 = b2->GetNext())
        {
            void* data2 = b2->GetUserData();
            if(b1 == b2 || !data1 || !data2 )
                continue;               
            b2Vec2 pk = b2->GetWorldCenter();
            float mk = b2->GetMass();
            b2Vec2 delta = pk - pi;
            float r = delta.Length();
            float force = BIG_G * mi * mk / (r*r);
            delta.Normalize();
            b1->ApplyForce( force * delta, pi);
            b2->ApplyForce(-force * delta, pk);
        }
    }
Presumably you have some way to know what the screen size is, and how many pixels per physics unit you are displaying. Have you tried just scaling the gravity as the pixels per meter changes?
irresistible force wrote:
Presumably you have some way to know what the screen size is, and how many pixels per physics unit you are displaying. Have you tried just scaling the gravity as the pixels per meter changes?


thought about it... but what would be the correct way to do this or how is everyone else doing this... I can't believe I am the only one who has issues with screen size changes and the objects not moving correctly when the screen sizes change.

I would love to see some code to learn from if possible as I am not sure how to go about this...

I use this

Code:
b2Vec2 position = body->GetPosition();


and send that to my rendering calls to position the objects...
Basically I think you need to pick a dimension (horizontal, vertical) and:
- figure out how many pixels are on the screen in that dimension
- figure out how many physics units are on the screen in that dimension
- make your BIG_G value related to the ratio between the pixels and physics units

How you do the first two things depends entirely on whatever platform and device you are working with, and these are probably not the right forums to help with that.
irresistible force wrote:
Basically I think you need to pick a dimension (horizontal, vertical) and:
- figure out how many pixels are on the screen in that dimension
- figure out how many physics units are on the screen in that dimension
- make your BIG_G value related to the ratio between the pixels and physics units

How you do the first two things depends entirely on whatever platform and device you are working with, and these are probably not the right forums to help with that.



I can grab the screen size with Irrlicht

Code:
device->getVideoDriver()->getScreenSize().Width
//and
device->getVideoDriver()->getScreenSize().Height


now the last two you stated I have no idea on that. By the way BIG_G isn't for gravity it's magnetism force, I am applying to the objects to attract them to each other.

I was told to not use screen dimensions but I have no idea on how that is supposed to work with Box2d using the screen dimensions to position the objects on setup and calculates their position and updates that value. I am assuming what you stated that a scaling of the force would be easiest. But I have no idea on how to scale the force value.

e.g.
say max size is 1920x1080 and lowest is 800x600 the user can select from.


at 800x600 the game is correct in how the objects move, now at 1920x1080 the objects are going to move very SLOW as the distance just got a lot longer.

so 1920-800 = 1120 difference in distance am I supposed to do something with that value? to increase the BIG_G value or force value itself?

I am about to just make the dam game only one size as this seems like a joke that I can't get this working on all sizes....

THanks!
Anyone else have anything? I was told not to tie the screen size to the physics calculations but from what I can tell Box2D is using them... when you setup the body description.

Thanks!

Original thread: https://box2d.org/forum/viewtopic.php?t=9266