0
\$\begingroup\$

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.

enter image description here

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 gif showing it:

https://i.ibb.co/TvHBMh9/MNML-November19-041320-AM.gif

This is how i calculate C point to use later on rotation(?)

 fun calculateAimingPoint(playerPosition: Vector2,touchPosition: Vector2):Vector2{
//playerPosition is staticbody center
        val 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: https://i.ibb.co/gDM9H3L/MNML-November19-042729-AM-2.gif

\$\endgroup\$
  • \$\begingroup\$ I am unsure if i understand your question. You want to draw an arrow that points in the direction the stone is going to fly, based on some touch input? if so you can subtract the stones center from your touch point and normalize that to get the arrows direction. scale that by the radius of the stone and add it to it's center to get the base point of the arrow. add the direction from before to that and you have the tip of your arrow. atan2 the direction from before to get an angle in radians. \$\endgroup\$ – Jonas Beck Nov 19 at 16:24
  • \$\begingroup\$ actually this is exactly what i am trying to do with above code \$\endgroup\$ – lastpeony4 Nov 19 at 18:09
0
\$\begingroup\$

I solved my problem guys. Trick was to set origin correctly

  var degrees = (Math.atan2((aimingPoint.x - xDownPoint).toDouble(), -(aimingPoint.y - yDownPoint).toDouble()) * 180.0 / Math.PI - 180.0f).toFloat()


            powerIndicator.setOrigin(powerIndicator.width/2,0f)

           powerIndicator.rotation = degrees
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.