Andengine rotating image

Suppose the screen receives errors from the outside. For this we need to have 4 coordinates (StartX, StartY, TargetX, TargetY). How to turn errors in the right direction? I spent hours doing this, and could not understand this part. Thus, the error appears on the screen, but it is not facing in the right direction. (Four coordinates are random numbers).

I consider the gradient of the line, as I learned at school:

int deltaY = TargetY - StartY; int deltaX = TargetX - StartX; float gradient = (float)deltaY / (float)deltaX; 

Then the angle of rotation of the error:

 float angle = (float) (Math.atan(gradient) * 180 / PI); 

Then here is the rotation and displacement modifier:

 registerEntityModifier(new RotationModifier(0.3f, 0, angle) { @Override protected void onModifierFinished(IEntity pItem) { super.onModifierFinished(pItem); registerEntityModifier(new MoveModifier(0.8f, StartX, TargetX, StartY, TargetY) { @Override protected void onModifierFinished(IEntity pItem) { super.onModifierFinished(pItem); } }); } }); 

The solution is more complicated, since on a line of a certain gradient the error can come from two directions, so that it can collide with two directions. I know that the 1/2 gradient is 26.56 degrees, and in the AndEngine coordinate system - 26.56 or -206.56 degrees, depending on which side the error is coming from.

+4
source share
2 answers

Try something like this:

 float bugAngle = 180 + (float) Math.toDegrees(Math.atan2((targetY - bugSprite.getRotationCenterY() - startY), (targetX - bugSprite.getRotationCenterX() - startX))); bugSprite.setRotation(bugAngle); 

I apologize for the long line of death.

+3
source

try with a simpler:

 float angle = getAngle(StartX, TargetX , StartY, TargetY ); YourSprite.setRotation((int) angle); 

Broca: D

0
source

Source: https://habr.com/ru/post/1437023/


All Articles