# Needs help on actually making the trigonometric function work as intended

import com.badlogic.gdx.graphics.g2d.Sprite
import kotlin.math.*

class Player : Entity // Entity : Sprite
{
var cursor : Sprite
var cursorDistance : Float = 1f

constructor(animationInfo:  HashMap<String, CustomAnimation>,
initialX : Float =0f,
initialY: Float = 0f) : super(animationInfo, initialX, initialY)
{
cursor = Sprite(Textures.idleArrow)
cursor.setPosition(this.x, this.y)
//        cursor.setOriginCenter()
}
override fun updateLogic(delta: Float)
{
var degree = degreesToMouse(returnRadians = true)
var curX = cursorDistance * cos(degree) + (x + getWorldOriginX)
var curY = cursorDistance * sin(degree) + (y + getWorldOriginY)
cursor.setPosition(curX, curY)
cursor.setOriginCenter()
cursor.rotation = degreesToMouse(cursor.x * TILE_WIDTH + cursor.originX,
screenYPoint = cursor.y * TILE_HEIGHT + cursor.originY,
println("cursor rotation: ${cursor.rotation}, degree:$degree")
}

override fun gameDraw(batch: SpriteBatch) {
super.gameDraw(batch)
batch.draw(
cursor, cursor.x, cursor.y, cursor.originX / TILE_WIDTH, cursor.originY / TILE_WIDTH,
(cursor.regionWidth / TILE_WIDTH.toFloat()), (cursor.regionHeight / TILE_HEIGHT.toFloat()),
cursor.scaleX, cursor.scaleY, cursor.rotation)

}
}


    fun degreesToMouse(arg: Entity = this, returnRadians: Boolean = false) : Float
{
Gdx.input.y.toDouble() - arg.screenPosition.y + arg.originY * arg.readOnlyScaling.y,
Gdx.input.x.toDouble() - arg.screenPosition.x + arg.originX * arg.readOnlyScaling.x)
return  -(radians * (180 / Math.PI)).toFloat()
else
}
fun degreesToMouse(screenXPoint: Float, screenYPoint: Float, returnRadians: Boolean = false) : Float
{
Gdx.input.y.toDouble() - screenYPoint,
Gdx.input.x.toDouble() - screenYPoint)
return  -(radians * (180 / Math.PI)).toFloat()
else
}



this is all the related code, the only important place is the updateLogic function, thanks to help of some people, i got that cos(degree) sin(degree) etc. however currently it doesn't work at all like i intended, i intended for it to work like this:

but currently it is like this:

as you can see, nor does the arrow look at the mouse cursor, nor would the line and the point at the ring interact if a line was drawn between center and mouse, I need a bit of help on that part. There are a lot of conversions and divisions like TILE_WIDTH etc. those are there because I need to use a FitViewport and a Tile Renderer, Tilewidth and TileHeight are 32, and those need conversion

• It looks like you just need to negate the y value you're getting from the sine function, because your vertical axis is upside down relative to what your code expects. Dec 14 '21 at 20:17
• the problem is that if i do that, like, if the player goes down, the arrow goes up and vice versa Dec 14 '21 at 20:24
• Sounds like you're negating the player position too, instead of just the offset you get from the sine function as I described. Dec 14 '21 at 20:29
• i guess i will do cursorDistance - calculated value? Dec 14 '21 at 20:33
• @DMGregory i did the following in the answers according to your comment and it works, thanks. Though there is one problem left and its that the cursor doesnt "look at" the mouse, if you could help that too, i would greatly appreciate it Dec 15 '21 at 13:56

thanks to the help of @DMGregory and the people who helped me initially make this code, found the answer to make that cursor thing orbit around the player according to the position to the mouse:

        var degree = degreesToMouse(returnRadians = true)
var curX = (cursorDistance * cos(degree)) + (x + getWorldOriginX)
var curY = (cursorDistance - (cursorDistance * sin(degree))) + (y + getWorldOriginY) - 1
cursor.setPosition(curX, curY)


cursorDistance is the obvious variable, x is in the viewport units so it doesnt get conversion, getWorldOriginX and getWorldOriginY returns originX-Y / TILE_WIDTH-HEIGHT as those arent initially in world units but pixel units as it is based of the texture, and to make that arrow point towards the cursor i did:

cursor.rotation = degreesToMouse() + 270

• I think you want a = degree (or some variant on it like negating it or shifting it by 90 or 180, depending on your coordinate system). (curX, curY) is an arrow pointing to the cursor from the scene origin, not from the center of the player sprite, so it does not correspond to the direction you want the cursor to point. Dec 15 '21 at 15:30
• I did cursor.rotation = degreesToMouse() + 270` and it all works fine, thanks so much Dec 15 '21 at 15:48
• @DMGregory also what would you suggest naming the title as? I didn't even know what to search for while making this, some people helped me, but I still don't know what this would be called Dec 15 '21 at 15:50
• Dec 15 '21 at 17:48