Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move an Sprite in Fling/Swipe direction(Diagonally) using AndEngine

I am working on a game in AndEngine. In that I am able to move an Object in Right to Left, Top to Bottom and vice-versa. But, my issue is that how can I move the Sprite object in the Direction of Fling? It means if users Fling's in any direction the Sprite Object should move on the co-ordinates of the fling and should move on.

If anyone can suggest about, how to get the exact X and Y co-ordinates would also do, I can manage to move the Sprite Object myself on the co-ordinates.

You can also see the VIDEO - Pirates Subs

In the video the Launcher coming on the FLING is what I am looking for, from any direction.

Thanks in Advance. Suri Sahani.

like image 867
Lalit Poptani Avatar asked Oct 11 '22 02:10

Lalit Poptani


1 Answers

Well, you can try this code.....

float slope = (y2 - y1) / (x2 - x1);
float angle = (float) Math.atan(slope);
float angleInDegree = (float) Math.toDegrees(angle);

c = y1 - (slope * x1);

onFling() method look's like this, for all direction.

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {

float c;
// Checks if LifeLine is there or not and enable or disable Fling
// Operation.
float sx = 0, sy = 0;
float x1 = e1.getX();
float y1 = e1.getY();

float x2 = e2.getX();
float y2 = e2.getY();

float slope = (y2 - y1) / (x2 - x1);
float angle = (float) Math.atan(slope);
float angleInDegree = (float) Math.toDegrees(angle);

c = y1 - (slope * x1);

/**
* bottom right to left top
*/
if (x1 > x2 && y1 > y2) {
sx = CAMERA_WIDTH;
sy = (slope * sx) + c;

missile = new Missile(sx, sy,
this.mFaceTextureRegionMissileLeftToRight);
missile.setVelocity(-(float) (600 * (Math.cos(angle))),
-(float) (600 * (Math.sin(angle))));

scene.getTopLayer().addEntity(missile);

missile.setRotation(angleInDegree + 180);

}
/**
* left top corner to right
*/
else if (x2 > x1 && y2 > y1) {
sx = -100;
sy = (slope * sx) + c;
missile = new Missile(sx, sy,
this.mFaceTextureRegionMissileLeftToRight);
missile.setVelocity((float) (300 * (Math.cos(angle))),
(float) (300 * (Math.sin(angle))));
scene.getTopLayer().addEntity(missile);
missile.setRotation(angleInDegree);
}
/**
* left bottom corner to right up
*/
else if (x2 > x1 && y1 > y2) {
sx = -100;
sy = (slope * sx) + c;
missile = new Missile(sx, sy,
this.mFaceTextureRegionMissileLeftToRight);
missile.setVelocity((float) (300 * (Math.cos(angle))),
(float) (300 * (Math.sin(angle))));
scene.getTopLayer().addEntity(missile);
missile.setRotation(angleInDegree);
}

/**
* Right corner to left bottom down
*/
else if (x1 > x2 && y2 > y1) {
sx = CAMERA_WIDTH;
sy = (slope * sx) + c;
missile = new Missile(sx, sy,
this.mFaceTextureRegionMissileLeftToRight);
missile.setVelocity((float) (-600 * (Math.cos(angle))),
-(float) (600 * (Math.sin(angle))));
scene.getTopLayer().addEntity(missile);
missile.setRotation(angleInDegree + 180);
}
return false;
}
like image 116
Siten Avatar answered Oct 16 '22 20:10

Siten