Search code examples
androidlogicspriteandenginemove

How to rotate sprite and move


How do I move sprite of sprite's front direction. I've andengine gles 2. I tried add sprite and I gave speed but sprite just left, top, bottom and right I wanna sprite can cross direction. i.e. when I rotate sprite's front, move that direction. How can move that just where the front my sprite ? So, if sprite's front look at the bottom sprite gotta move bottom direction, and if sprite front direction of the upper left sprite gotta move upper left. Like this image..

https://i.sstatic.net/muoSg.png

My code...

@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
        float pTouchAreaLocalX, float pTouchAreaLocalY) {

    if (pSceneTouchEvent.isActionDown()) {

        //Rotate left button
        if(id==0){ 

       shipSprite.setRotation(-10);}           

//Speed button

        if(id==1){
  shipSprite.mPhysicsHandler.setVelocityY(-400);

}

    return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
}

not going to cross(i.e. sprite's front)... just going Y direction..


Solution

  • I dont know andengine. And did not find anything on "andengine move sprite forward". But I can offer mathematical meaning:

    enter image description here

    a - your rotation. Do not forget to normalize the vector. Good luck!

    shipSprite.mPhysicsHandler.setVelocityX(f.x);
    shipSprite.mPhysicsHandler.setVelocityY(f.y);