I am new to andengine and even android game development. i have created sprite as a box. this box is now draggable by using this coding. it works fine.
but i want multitouch on this which i want to rotate a sprite with 2 finger in that box and even it should be draggable. …. plz help someone…
i am trying this many days but no idea.
final float centerX = (CAMERA_WIDTH - this.mBox.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mBox.getHeight()) / 2;
Box= new Sprite(centerX, centerY, this.mBox,
this.getVertexBufferObjectManager()) {
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/ 2,
pSceneTouchEvent.getY() - this.getHeight() / 2);
float pValueX = pSceneTouchEvent.getX();
float pValueY = CAMERA_HEIGHT-pSceneTouchEvent.getY();
float dx = pValueX - gun.getX();
float dy = pValueY - gun.getY();
double Radius = Math.atan2(dy,dx);
double Angle = Radius * 360 ;
Box.setRotation((float)Math.toDegrees(Angle));
return true;
}
Make sure you enabled multi touch in your game. You can use the same code used in the MultiTouchExample in the
onLoadEnginemethod.The algorithm is quite simple, similar to what you’ve posted here.
onAreaTouchedmethod. (You can get the pointer ID by callingpSceneTouchEvent.getPointerID()).pTouchAreaLocalXandpTouchAreaLocalY).ACTION_DOWNfor both), save the initial angle. (Math.tan2(pointer1Y - pointer2Y, pointer1X - pointer2X)).ACTION_UPis not called for the pointers, update the new angle in everyACTION_MOVEevent of the pointers, and get the angle delta (delta = currentAngle - initialAngle). Then callsetRotation(Math.toDegrees(delta)).To make the sprite dragable with 2 pointers, you need to move your sprite the lesser of the distance each pointer has moved. For example, if:
the sprite should move +40 units in the X axis, and -10 units in the Y axis.