I have this code. I am not able to track out why the balls are repeling each other?
The code is in mt4j integrated with jbox2d.
[code]public TapBall(MTApplication mtApplication, String name)
{
super(mtApplication, name);
// TODO Auto-generated constructor stub
app = mtApplication;
// this.getCanvas().addChild(bk = new MTBackgroundImage(app, app.loadImage(imagesPath + "background-16.jpg"), true));
worldAABB = new AABB(new Vec2(-worldOffset, -worldOffset), new Vec2((app.width)/scale + worldOffset, (app.height)/scale + worldOffset));
this.world = new World(worldAABB, gravity, sleep);
this.registerPreDrawAction(new UpdatePhysicsAction(world, timeStep, constraintIterations, scale));
container = new MTComponent(app);
this.registerGlobalInputProcessor(new CursorTracer(app,this));
container.scale(scale, scale, 1, Vector3D.ZERO_VECTOR);
this.getCanvas().addChild(container);
this.createScreenBorders(container);
this.getCanvas().unregisterAllInputProcessors();
this.getCanvas().registerInputProcessor(new TapProcessor(app));
this.getCanvas().addGestureListener(TapProcessor.class, this);
this.addContactListener(world);
}
public boolean processGestureEvent(MTGestureEvent ge)
{
TapEvent te = (TapEvent) ge;
if(te.isTapDown())
{
cp = new Vector3D(te.getLocationOnScreen());
c = new PhysicsCircle(app, cp, 50, world, 1f, 0.3f, .4f, scale);
System.out.println("circle drawn!!");
MTColor col = new MTColor(ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255));
c.setFillColor(col);
c.setStrokeColor(col);
this.getCanvas().addChild(c);
PhysicsHelper.addDragJoint(world, c, c.getBody().isDynamic(), scale);
if(!te.isTapped())
{
clickPoint = te.getLocationOnScreen();
IMTComponent3D target = te.getTargetComponent();
// if(target instanceof MTEllipse)
{
System.out.println("Target Matched");
c.scale(1.2f, 1.2f, 1.2f, clickPoint);
}
}
}
return false;
}
public void addContactListener(World world)
{
world.setContactListener(new ContactListener()
{
@Override
public void add(ContactPoint arg0)
{
Shape s1 = arg0.shape1;
Shape s2 = arg0.shape2;
Body body1 = s1.getBody();
Body body2 = s2.getBody();
if(body1.isTouching(body2))
{
System.out.println("circle match");
}
}
@Override
public void persist(ContactPoint arg0) {
// TODO Auto-generated method stub
}
@Override
public void remove(ContactPoint arg0) {
// TODO Auto-generated method stub
}
@Override
public void result(ContactResult arg0) {
// TODO Auto-generated method stub
}
});
}
The code is in mt4j integrated with jbox2d.
Code:
[code]public TapBall(MTApplication mtApplication, String name)
{
super(mtApplication, name);
// TODO Auto-generated constructor stub
app = mtApplication;
// this.getCanvas().addChild(bk = new MTBackgroundImage(app, app.loadImage(imagesPath + "background-16.jpg"), true));
worldAABB = new AABB(new Vec2(-worldOffset, -worldOffset), new Vec2((app.width)/scale + worldOffset, (app.height)/scale + worldOffset));
this.world = new World(worldAABB, gravity, sleep);
this.registerPreDrawAction(new UpdatePhysicsAction(world, timeStep, constraintIterations, scale));
container = new MTComponent(app);
this.registerGlobalInputProcessor(new CursorTracer(app,this));
container.scale(scale, scale, 1, Vector3D.ZERO_VECTOR);
this.getCanvas().addChild(container);
this.createScreenBorders(container);
this.getCanvas().unregisterAllInputProcessors();
this.getCanvas().registerInputProcessor(new TapProcessor(app));
this.getCanvas().addGestureListener(TapProcessor.class, this);
this.addContactListener(world);
}
public boolean processGestureEvent(MTGestureEvent ge)
{
TapEvent te = (TapEvent) ge;
if(te.isTapDown())
{
cp = new Vector3D(te.getLocationOnScreen());
c = new PhysicsCircle(app, cp, 50, world, 1f, 0.3f, .4f, scale);
System.out.println("circle drawn!!");
MTColor col = new MTColor(ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255));
c.setFillColor(col);
c.setStrokeColor(col);
this.getCanvas().addChild(c);
PhysicsHelper.addDragJoint(world, c, c.getBody().isDynamic(), scale);
if(!te.isTapped())
{
clickPoint = te.getLocationOnScreen();
IMTComponent3D target = te.getTargetComponent();
// if(target instanceof MTEllipse)
{
System.out.println("Target Matched");
c.scale(1.2f, 1.2f, 1.2f, clickPoint);
}
}
}
return false;
}
public void addContactListener(World world)
{
world.setContactListener(new ContactListener()
{
@Override
public void add(ContactPoint arg0)
{
Shape s1 = arg0.shape1;
Shape s2 = arg0.shape2;
Body body1 = s1.getBody();
Body body2 = s2.getBody();
if(body1.isTouching(body2))
{
System.out.println("circle match");
}
}
@Override
public void persist(ContactPoint arg0) {
// TODO Auto-generated method stub
}
@Override
public void remove(ContactPoint arg0) {
// TODO Auto-generated method stub
}
@Override
public void result(ContactResult arg0) {
// TODO Auto-generated method stub
}
});
}