开发者

Problem Simulating WeldJoint

开发者 https://www.devze.com 2023-03-25 08:23 出处:网络
I\'d have already post this question in the Andengine forum but there are already some questions regarding this topic, some have replies but the ones i want to know don\'t have any replies yet.

I'd have already post this question in the Andengine forum but there are already some questions regarding this topic, some have replies but the ones i want to know don't have any replies yet.

I'm trying to simulate a player jump like in Super Mario Bros. First, I used a simple contact listener to have a boolean value false when contact occurs but the contact occurs with walls grounds, everything. So, I'm now trying to attach another small body to the bottom of player as foot sensor using WeldJoint. But I couldn't achieve that. The WeldJoint wouldn't stick at all. I tried to create the WeldJoint on an update thread, nothing. I tried with the setposition method to update the sensor position with the player's, but it just positions the sensor below ground. Any suggestions would be appreciated. Here is how i tried to create WeldJoint.

Player and sensor

mPlayer = new AnimatedSprite(100, 150, PlayerTextureRegion);
PlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,mPlayer,BodyType.DynamicBody, PLAYER_FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mPlayer, PlayerBody, true开发者_Python百科, true));
mScene.getLastChild().attachChild(mPlayer);

final Shape mSensor= new Rectangle(mPlayer.getX()+4,mPlayer.getY()+mPlayer.getHeight(),10,4);
final Body SensorBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,mSensor,BodyType.DynamicBody, SENSOR_FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mSensor, SensorBody, true, true));
mScene.getLastChild().attachChild(mSensor);

mScene.registerUpdateHandler(new IUpdateHandler() {

        @Override
        public void reset() { }

        @Override
        public void onUpdate(final float pSecondsElapsed) {
             this.createJoint(PlayerBody,SensorBody);
             .......

Joint Method

private void createJoint(Body Anchor, Body Sensor){
    final WeldJointDef join = new WeldJointDef();
    join.initialize(Anchor,Sensor,Anchor.getWorldCenter());
    this.mPhysicsWorld.createJoint(join);
}


Ok, instead of WeldJoint I used RevoluteJoint, without the motor configuration and it works fine now. Just initialize two bodies using revoluteJointDef and they are stuck like weldjoint. For time being I'm going with revoluteJoint to make two bodies as one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号