Vote count:
0
I am using libgdx and box2d to create a moving platform that goes left and right.
my plan to create moving platform: create platform, add 1 sensor left of platform and add 1 sensor right of platform. than check if sensor are touching the ground. if it is switch dir.
here is what i have so far:
public createPlatform(){
bdef.type = BodyType.KinematicBody;
pshape.setAsBox(28 / PPM, 8 / PPM);
... create platform
body.createFixture(fdef).setUserData("platform");
/*** 2nd fixture - LEFT ***/
pshape.setAsBox(2 / PPM, 8 / PPM, new Vector2(-30 / PPM, 0 / PPM),
0);
fdef.shape = pshape;
fdef.isSensor = true;
fdef.filter.categoryBits = Constants.BIT_MOVE_PLATFORM; // id
fdef.filter.maskBits = Constants.BIT_GROUND; // collied with
body.createFixture(fdef).setUserData("Pfoot1");// myContactListener
MovePlatform mp = new MovePlatform(body);
body.setUserData(mp);
movePlatformStore.add(mp);
}
this is where I am trying to do collision note: for some reason it userdata never comes in if statments below. so it never prints(here -----).
public class MyContactListener implements ContactListener {
private boolean platformRight = false; // switch dir of platform
public MyContactListener() {
super();
}
public void beginContact(Contact contact) {
Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();
if (fa == null || fb == null)
return;
// platform
if (fa.getUserData() != null && fa.getUserData().equals("Pfoot1")) {
System.out.println("here-------------");
platformRight = true;
}
if (fb.getUserData() != null && fb.getUserData().equals("Pfoot1")) {
System.out.println("here------------");
platformRight = true;
}
}
so in update method - switch dir of platform movePlatformStore is array list where i am store all my moving platforms.
public void update(int dt){
// update - platforms
if (movePlatformStore != null) {
for (int i = 0; i < movePlatformStore.size; i++) {
movePlatformStore.get(i).update(dt);
if (myContactListenerObj.getPlatformRight()) {
movePlatformStore.get(i).getBody().setLinearVelocity(1f, 0);
} else {
movePlatformStore.get(i).getBody()
.setLinearVelocity(-1f, 0);
}
}
}
}
again to recap. I am not sure if this is the logic to create moving platforms. if some one can create small tutorial on this or give me link to tutorial would be real helpful to me.
problem: for some reason it never prints ("here-------"). even if it does still logic is off on this one. I am going to have 10 moving platform and they will move different direction(left,right,up,down), different speed... etc.
Aucun commentaire:
Enregistrer un commentaire