2

I made a racing game in andengine.The car is moving by MoveModifier().The car is dynamicBody at the same time.But the dynamicBody characteristic of car is losing when it is moving.

(enemy1=car)

Sprite enemy1=new Sprite(0,0,this.enemyRegion1);
         enemy1.registerEntityModifier(
                    (IEntityModifier) new SequenceEntityModifier (
                            new MoveModifier(10, enemy1.getX(),  enemy1.getX()+400, 
                                    enemy1.getY(), enemy1.getY())));
        final Vector2 velocity = Vector2Pool.obtain(5, 5);

        final FixtureDef enemyFixtureDef1 = PhysicsFactory.createFixtureDef(0, 0, 0);
        this.enemyBody1 = PhysicsFactory.createBoxBody(this.mPhysicsWorld, enemy1, BodyType.DynamicBody, enemyFixtureDef1);
        this.enemyBody1.setLinearVelocity(velocity);
        Vector2Pool.recycle(velocity);
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(enemy1, this.enemyBody1, true, false));






        this.scene.attachChild(enemy1);
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
user1391058
  • 265
  • 4
  • 19

1 Answers1

2

You cannot move physics bodies using MoveModifier, it only works with Sprites without a PhysicsConnector. The connection goes only one way, from the Box2D engine to AndEngine that works on top of it. You will have to manage the car movement in terms of physics and the sprite will follow the body. Not the other way round.

JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • so how can the body follow a known path? Which function in box2d? – user1391058 May 27 '12 at 16:51
  • Look at this question: http://stackoverflow.com/questions/8629524/making-a-box2d-object-follow-a-predetermined-path The accepted answer should work. – JohnEye May 27 '12 at 17:14
  • I know the coordinates.but which function will I use to move it to coordinates I know? For example: body.move(10,10); – user1391058 May 27 '12 at 18:08
  • 1
    If you want to move it instantly, use body.setTransform(parameters). If you choose to use MouseJoint, then it will be mousejoint.setTarget(coords). – JohnEye May 27 '12 at 18:36