1
votes

I have a sprite that I want to move on touchevent. there should by kind of arrows pointing the direction of planned movement. The movement should star on Action_Up 9it works). The problem is with rotation center and anchor center. It should work like this: When you touch sprite, arrows and blue circle appear (later the number of arrows will depend on how far the finger is from the sprite showing the strenght of the movement). First arrow is attached to sprite, another arrow to first and so on. So when you move the finger the center of bottom of first arrow should rotate around the center of my sprite. (I hope its clear, its the similar concept to Angy Birds, where you move your finger to aim and set force). The problem is that I cannot set coordinates. For example setting coordinates (50f, 50f) for rotation center or anchorcenter makes my arrow move around point far from center of the sprite. I thought that dividing coordinates by 32 will help but no. Even coordinates like (0.5f, 0.5f) move my arrows further than 0.5 of pixel. I am using andEngine GLES2-AnchorCenter

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


    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){

        arrow = new Sprite(0, 0, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        arrow2 = new Sprite(55, 70, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        arrow3 = new Sprite(55, 70, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        arrow4 = new Sprite(55, 70, 110, 70, ResourceManager.getInstance().mArrowregion, getVertexBufferObjectManager());
        circle = new Sprite(50, 50, 300, 300, ResourceManager.getInstance().mBlueCircleRegion, getVertexBufferObjectManager());

        arrow.setAnchorCenter(25.0f/32, -10.0f/32);
        arrow.setRotationCenter(0, 0);

        arrow.setAlpha(0.4f);
        arrow2.setAlpha(0.6f);
        arrow3.setAlpha(0.8f);
        arrow4.setAlpha(1.0f);
        circle.setAlpha(0.3f);

        this.attachChild(circle);       
        this.attachChild(arrow);
        arrow.attachChild(arrow2);
        arrow2.attachChild(arrow3);
        arrow3.attachChild(arrow4);



    }

    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){
        arrow.setRotation(0 + pSceneTouchEvent.getX());


    }

    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
        arrow.detachSelf();
        circle.detachSelf();
        arrow.dispose();
        circle.dispose();

        body.setLinearVelocity(-((pSceneTouchEvent.getX()/32 - body.getPosition().x) * 10), -((pSceneTouchEvent.getY()/32 - body.getPosition().y) * 10));



    }


    return true;
}

EDIT:

What works is when I use:

arrow = new Sprite(50, 100, ...)
arrow.setRotationCenter(0.5f, -0.25f);

and no anchor center set up.

Why I need values for X and Y lower than 1f for rotation center?

2

2 Answers

1
votes

setRotationCenter should normally be in the range 0.0 to 1.0, because they are a proportion of the width/height of the object, for example: setRotationCenter(0.5f, 0.5f) would make the object rotate around its center.

The setRotationCenter in GLES2-AC is different from the setRotationCenter in GLES2

for more info I advice you to follow this link :

Entity class

In this link, you have these functions:

protected void updateLocalRotationCenter() {
        this.updateLocalRotationCenterX();
        this.updateLocalRotationCenterY();
    }

    protected void updateLocalRotationCenterX() {
        this.mLocalRotationCenterX = this.mRotationCenterX * this.mWidth;
    }

    protected void updateLocalRotationCenterY() {
        this.mLocalRotationCenterY = this.mRotationCenterY * this.mHeight;
    }

as you can see, the rotationCenter values (X,Y) are multiplied with the Width and Height of the entity, this is why they should be between 0.0-1.0

1
votes

The AnchorCenter and RotationCenter (x,y) values are between 0.0f and 1.0f:

  • 0f 0f for entity's bottom left vertice
  • 0,5f 0,5f for entity's center (anchor center)
  • 1f 1f for entity's top right vertice
  • etc...

So if you want to achieve something like angry birds I would set my bird anchor center to 1f 0.5f and then apply some rotation logic to the body. I think it would work.