issue with rectangles on a circle

Hey. I'm new to Box2D and at the moment i'm testing around with JBOX.
So I wanted to make a 'planet', so I disabled gravity and made a Circle at 0,0.
Then i've put a sensor (that is bigger than my planet) around it.
For testing I made some circles and throw them in there. That worked fine. But now with Rectangles, they can't stand still.
When they are on the Planet, they are shaking around. I don't know, how to fix it.
Sorry for my bad english, I will just post my code here, so you can look for the Problem:

Code: Select all

public class SensorTest extends TestbedTest
{
    class BoolWrapper
    {
        boolean tf;
    }
    int e_count = 5;
    Fixture m_sensor;
    Body m_bodies[] = new Body[this.e_count];
    BoolWrapper m_touching[] = new BoolWrapper[this.e_count];

    @Override
    public void initTest(boolean argDeserialized)
    {
        World world = this.getWorld();
        world.setGravity(new Vec2(0.0f, 0.0f));
        for (int i = 0; i < this.m_touching.length; i++)
        {
            this.m_touching[i] = new BoolWrapper();
        }
       
        {
            BodyDef bd = new BodyDef();
            Body ground = getWorld().createBody(bd);
           
            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 100.0f;
                shape.m_p.set(0.0f, 0.0f);
               
                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.isSensor = true;
                this.m_sensor = ground.createFixture(fd);
            }

            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 20.0f;
                shape.m_p.set(0.0f, 0.0f);
               
                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 10f;
                ground.createFixture(fd);
            }
        }
       
        {
//            CircleShape shape = new CircleShape();
//            shape.m_radius = 1.0f;
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(1f, 1f);
           
            for (int i = 0; i < this.e_count; ++i)
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-10.0f + 3.0f * i, 50.0f);
                bd.userData = this.m_touching[i];
               
                this.m_touching[i].tf = false;
                this.m_bodies[i] = getWorld().createBody(bd);
               
                this.m_bodies[i].createFixture(shape, 1.0f);
            }
        }
    }

    @Override
    public void beginContact(Contact contact)
    {
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();
       
        if (fixtureA == this.m_sensor)
        {
            Object userData = fixtureB.getBody().getUserData();
            if (userData != null)
            {
                ((BoolWrapper) userData).tf = true;
            }
        }
       
        if (fixtureB == this.m_sensor)
        {
            Object userData = fixtureA.getBody().getUserData();
            if (userData != null)
            {
                ((BoolWrapper) userData).tf = true;
            }
        }
    }

    @Override
    public void endContact(Contact contact)
    {
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();
       
        if (fixtureA == this.m_sensor)
        {
            Object userData = fixtureB.getBody().getUserData();
            if (userData != null)
            {
                ((BoolWrapper) userData).tf = false;
            }
        }
       
        if (fixtureB == this.m_sensor)
        {
            Object userData = fixtureA.getBody().getUserData();
            if (userData != null)
            {
                ((BoolWrapper) userData).tf = false;
            }
        }
    }

    @Override
    public synchronized void step(TestbedSettings settings)
    {
        super.step(settings);
       
        for (int i = 0; i < this.e_count; ++i)
        {
            if (this.m_touching[i].tf == false)
            {
                continue;
            }
           
            Body body = this.m_bodies[i];
            Body ground = this.m_sensor.getBody();
           
            CircleShape circle = (CircleShape) this.m_sensor.getShape();
            Vec2 center = ground.getWorldPoint(circle.m_p);
           
            Vec2 position = body.getPosition();
           
            Vec2 d = center.sub(position);
            if (d.lengthSquared() < Settings.EPSILON * Settings.EPSILON)
            {
                continue;
            }
           
            d.normalize();
            Vec2 F = d.mulLocal(100.0f / d.lengthSquared());
            body.applyForce(F, position);
        }
    }

    @Override
    public String getTestName()
    {
        return "Planet Test"; //$NON-NLS-1$
    }

}


Is it my fault? 🙂
They are shaking around because they are moving back and forth on the edge of the circle. It's simulating the scenario too perfectly, basically. You'll have to decide how you would want to change the world. Perhaps give them a FrictionJoint to give them some drag? Or make the bodies have multiple shapes so that they can have concave sides?
toucansam wrote:They are shaking around because they are moving back and forth on the edge of the circle. It's simulating the scenario too perfectly, basically. You'll have to decide how you would want to change the world. Perhaps give them a FrictionJoint to give them some drag? Or make the bodies have multiple shapes so that they can have concave sides?


Ah okay, it is because the Circle is perfectly round, isn't it? That sounds pretty logical.. Sorry i am new to game-development. ^^
So i have to make an "unperfect" circle or something like this.

EDIT//
Okay, now im trying to do some sort of Player, that can walk around the planet.
I am thinking about, what would be the best to move him.
Should I move him in the Angle, where he looks at?
And if so, has Box2D a function to do this?
You can play with applying forces to the body. You can do lots of things 🙂 The engine will do exactly what you tell it to do, you just have to figure out how to get it to do exactly what you want it to do. That's the hard part.

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