1
\$\begingroup\$
import com.badlogic.gdx.graphics.g2d.Sprite
import com.badlogic.gdx.graphics.g2d.SpriteBatch
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,
            returnRadians = false)
        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
    {
        var radians = atan2(
            Gdx.input.y.toDouble() - arg.screenPosition.y + arg.originY * arg.readOnlyScaling.y,
            Gdx.input.x.toDouble() - arg.screenPosition.x + arg.originX * arg.readOnlyScaling.x)
        if(!returnRadians)
            return  -(radians * (180 / Math.PI)).toFloat()
        else
            return radians.toFloat()
    }
    fun degreesToMouse(screenXPoint: Float, screenYPoint: Float, returnRadians: Boolean = false) : Float
    {
        var radians = atan2(
            Gdx.input.y.toDouble() - screenYPoint,
            Gdx.input.x.toDouble() - screenYPoint)
        if(!returnRadians)
            return  -(radians * (180 / Math.PI)).toFloat()
        else
            return radians.toFloat()
    }```


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:

enter image description here

but currently it is like this: enter image description here

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

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Dec 14 '21 at 20:17
  • \$\begingroup\$ the problem is that if i do that, like, if the player goes down, the arrow goes up and vice versa \$\endgroup\$
    – logic
    Dec 14 '21 at 20:24
  • 1
    \$\begingroup\$ Sounds like you're negating the player position too, instead of just the offset you get from the sine function as I described. \$\endgroup\$
    – DMGregory
    Dec 14 '21 at 20:29
  • \$\begingroup\$ i guess i will do cursorDistance - calculated value? \$\endgroup\$
    – logic
    Dec 14 '21 at 20:33
  • \$\begingroup\$ @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 \$\endgroup\$
    – logic
    Dec 15 '21 at 13:56
0
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Dec 15 '21 at 15:30
  • \$\begingroup\$ I did cursor.rotation = degreesToMouse() + 270 and it all works fine, thanks so much \$\endgroup\$
    – logic
    Dec 15 '21 at 15:48
  • \$\begingroup\$ @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 \$\endgroup\$
    – logic
    Dec 15 '21 at 15:50
  • \$\begingroup\$ The unit circle? Bearing? \$\endgroup\$
    – DMGregory
    Dec 15 '21 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .