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:
Is it my fault? 🙂
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? 🙂