Rotate Texture in function of player position

前端 未结 2 1833
北恋
北恋 2021-01-28 10:14

I\'m making a 2d game that consists in a plane fighting against spaceships trough missiles, i want the spaceships to rotate in function of the player position, so that they are

相关标签:
2条回答
  • 2021-01-28 10:37

    I think this should work. Set..

    v = centerPosOfShip - centerPosOfUFO
    

    Normalize...

    v /= sqrt( v.x * v.x + v.y * v.y )
    

    So now we can say v = [ cos(theta) sin(theta) ]; the rotation matrix is...

    [ cos(theta)  -sin(theta) ]
    [ sin(theta)   cos(theta) ]
    

    So we can write in terms of v-components...

    [ v.x  -v.y    centerPosOfUFO.x ][x]
    [ v.y   v.x    centerPosOfUFO.y ][y]
    [ 0     0      1                ][1]
    

    where [x y] is any point on the UFO relative to its center.

    Hope that makes sense

    0 讨论(0)
  • 2021-01-28 10:43

    The @dwn's answer is mathematically correct, but when you deal with LibGDX framework it's better to use native tools.

    If you have positions of enemy and player storing in Vector2s then you may perform computations a bit convenient. For example, to determine angle between specific enemy and player:

    Vector2 enemyPos;
    Vector2 playerPos;
    Vector2 temp;
    
    float angle = temp
                    .set(playerPos)
                    .sub(enemyPos)
                    .angle();
    

    This will give you exactly what you want. Of course it's better to keep temp vector in somewhere in public space to use it in such numerous calculations. You better to check other convenient Vector2methods, it will seriously easy your life.

    Good luck.

    0 讨论(0)
提交回复
热议问题