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

Started by
15 comments, last by MARS_999 10 years, 12 months ago

I don't think I need to scale the size of the objects. ... I need to keep the force constant between screen resolutions as the distance varies and on larger screens objects tske to long to start attracting

But it only varies if you mix in screen resolution in your physics calculations.

Stop doing that, and the problem will go away.

We are trying to tell you that your physics calculations does not need to know about screen resolution, and trying to force it to will only complicate your code.

Separate it.

Physics should live in its own little virtual world where everything is constant.

Then in your rendering code, you can simply chose a view that shows the objects in the world you want to show (maybe there are even more of them then will fit on screen)

Object A and Object B will always be X units apart, in the physics code, no matter if they are shown in opposite corners, or both in the same corner of the screen, and will always behave as expected in relation to each other.

Advertisement

Sigh, I guess I am just to dense to make this happen people... but I have tried and this is what I am using now. LIke I said when I change the screen resolution from 1024x768 to 1920x1080 the objects take to long to move as the magnetism is weakened. I am not using the screen size in this code, I am using box2D GetWorldCenter() and that is it....

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);
        }
    }

Sigh, I guess I am just to dense to make this happen people... but I have tried and this is what I am using now. LIke I said when I change the screen resolution from 1024x768 to 1920x1080 the objects take to long to move as the magnetism is weakened. I am not using the screen size in this code, I am using box2D GetWorldCenter() and that is it....


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);
        }
    }

How do you initialize Box2D ?

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
I will post it tonight....

NX::App* p = NX::App::Get();
    
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(posX, posY);

    KillBody();
    
    body = p->GetWorld()->CreateBody(&bodyDef);

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(sizeX, sizeY);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;

    // Set the box density to be non-zero, so it will be dynamic.
    fixtureDef.density = 1.0f;

    // Override the default friction.
    fixtureDef.friction = 0.3f;

    // Add the shape to the body.
    body->CreateFixture(&fixtureDef);

    body->SetUserData(this);

I think your problem is where you place your objects in the world.

You seem to use screen coordinates to place them.

Decoupling physics from graphics means you have to both convert input (from a mouse etc) to physics world coordinates, and then you need to convert back from physics to screen coordinates in rendering.


b2Vec2 position = body->GetPosition();

I get the positions form Box2d so the coordinates should be done in physics terms...

then I use that variable to draw the object

This topic is closed to new replies.

Advertisement