UPDATE:
I solved my problem guys trick was to use
powerIndicator.setOrigin(powerIndicator.width/2,0f)
THANKS EVERYONE
I am trying to draw and rotate an arrow sprite based on a point,around a static circle body without messing its drawing starting point. Arrow should be directed to the same direction with user touch point,static body center vector.
Let me explain it with an example.
This is how i calculate arrow drawing point:
fun powerIndicatorDownPointCalculator(playerPosition: Vector2,touchPosition: Vector2,distanceBetweenTouchPointAndPlayer:Float):Vector2{
// player position is static body center,touch position is touch point.
val playerPositionHolder = playerPosition.cpy()
val abVector = playerPosition.sub(touchPosition);
val normalizedAbVector = Vector2(abVector.x/distanceBetweenFingerAndPlayer,abVector.y/distanceBetweenFingerAndPlayer)
Gdx.app.log("normalized ab vector",normalizedAbVector.toString())
var pointToAddX = 90*normalizedAbVector.x
var pointToAddY = 90*normalizedAbVector.y
var newPointX = playerPositionHolder.x + pointToAddX
var newPointY = playerPositionHolder.y + pointToAddY
Gdx.app.log("NEW POINT", Vector2(newPointX,newPointY).toString())
return Vector2(newPointX,newPointY)
}
Here is a simple video showing it:
This is how i calculate C point to use later on rotation(?)
fun calculateAimingPoint(playerPosition: Vector2,touchPosition: Vector2):Vector2{
//playerPosition is staticbody centerval abVector = playerPosition.sub(touchPosition);
val secondPos = blackBody.position.cpy()
val cPointPosition = secondPos.add(abVector)
return cPointPosition
}
This is what i wrote for rotation but for sure it is wrong since not working as i wanted:
var angle = atan2(aimingPoint.y - blackBody.position.y, aimingPoint.x - blackBody.position.x ) *180/ PI ;
//aiming point is C point, blackBody is static body
if (angle > 90)
{
angle = 450 - angle;
}
else
{
angle = 90 - angle;
}
// powerIndicator.setOrigin(????) // setOrigin is for rotation but i dont now what to write here.
angle = angle*-1
powerIndicator.rotation = angle.toFloat()
What i actually wanna achieve is this:
As zakwayda suggested first i am trying to solve rotation problem. I fixed arrows position to central bodies center and trying to rotate to face with aim point.
I have decided to work on rotation first.I fixed position of arrow to center of circular body and i am trying to face arrow towards aiming point.
(shooter is central body)
var xDownPoint = shooter.body.position.x-powerIndicator.width/2
var yDownPoint = shooter.body.position.y
powerIndicator.setPosition(xDownPoint,yDownPoint)
var degrees = (Math.atan2((aimingPoint.x - xDownPoint).toDouble(), -(aimingPoint.y - yDownPoint).toDouble()) * 180.0 / Math.PI - 180.0f).toFloat() powerIndicator.setOrigin(xDownPoint,yDownPoint)
powerIndicator.rotation = degrees
This is the result:
https://im4.ezgif.com/tmp/ezgif-4-21ee1394147e.gif
As you see starting position of arrow is messed up but i guess its rotation degree is correct