Libgdx and Box2d Detect Collision of two specific bodies -
i have 2 bodies, both dynamic, 1 of them have turned off gravity , want turn on when hit other body. know can set own contact listener want specify action if 2 specific bodies touch.
any 1 have guidance?
u can set userdata of body , use that
public class gamecoliision implements contactlistener { public gamecoliision() { } @override public void begincontact(contact contact) { // todo auto-generated method stub body a=contact.getfixturea().getbody(); body b=contact.getfixtureb().getbody(); a.getuserdata(); if(a.getuserdata() instanceof obstacle&&b.getuserdata() instanceof car) { } } @override public void endcontact(contact contact) { // todo auto-generated method stub } @override public void presolve(contact contact, manifold oldmanifold) { // todo auto-generated method stub } @override public void postsolve(contact contact, contactimpulse impulse) { // todo auto-generated method stub } } public class obstacle { public body rectanglebody; public body trianglebody; world world; public static final float width=10/40f; public static final float height=10/40f; public obstacle(world world,float x,float y) { this.world=world; initrectangle(x,y); } private void initrectangle(float x, float y) { bodydef bodydef=new bodydef(); fixturedef fixturedef=new fixturedef(); bodydef.type=bodytype.dynamicbody; rectanglebody=world.createbody(bodydef); polygonshape polygonshape=new polygonshape(); polygonshape.setasbox(width,height); fixturedef.shape=polygonshape; fixturedef.friction=1f; fixturedef.density = 1f; rectanglebody.createfixture(fixturedef); rectanglebody.settransform(x, y, 0) ; rectanglebody.setangulardamping(0); rectanglebody.setuserdata(this); }
hope solves problem
Comments
Post a Comment