0

I am developing a game in andengine and I want to apply a Force to my Sprite. The thing is I rotate the sprite with a controller and I want a force to be applied on the direction the sprite is facing (Sprite is a rocket).

Thanks in advance hope I explained it well.

Kanga
  • 125
  • 12
  • Create PhysicsHandler object and then apply velocity and then connect the sprite to this handler.It will work. – Rama Oct 24 '13 at 13:09
  • can you provide me with some code or something I don't quite understand you I tried something but it doesn't work – Kanga Oct 24 '13 at 21:17
  • Similar question: http://stackoverflow.com/questions/9164146/simple-gun-in-cocos2dbox2d-game – iforce2d Oct 26 '13 at 06:34

2 Answers2

0
  Sprite  pSprite = new Sprite(23, 43, pTextureRegion, pVertexBufferObject);
    PhysicsHandler handler = new PhysicsHandler(pSprite);
    handler.setVelocityX(pVelocityX);
   handler.setVelocityY(pVelocityY);

In this way you can implement. If it is not adjusting then you can creating body for that particular sprite and apply force to that body

Rama
  • 1,156
  • 1
  • 7
  • 13
0

I found the facing direction using this

currentVelocity = new Vector2((float)Math.cos(body.getAngle()) * magnitude, (float) Math.sin(body.getAngle()) * magnitude);
    body.applyForce(currentVelocity, body.getPosition());

magnitude is a float variable.

Kanga
  • 125
  • 12
  • depending on the looks of the sprite you maybe need to play with the sin and cos maybe change them (cos to sin and sin to cos) and maybe add negative values to x or y od the vector2. That's just a tip for viewers on this post I had problems and I solved it that way :) – Kanga Oct 29 '13 at 18:25