根据玩家位置旋转纹理 [英] Rotate Texture in function of player position

查看:133
本文介绍了根据玩家位置旋转纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行一场二维游戏,其中包括一架与太空船通过导弹作战的飞机,我希望宇宙飞船在玩家位置的作用下旋转,这样他们总是面对着头部的玩家。玩家可以在y和x方向上自由移动。
玩家和宇宙飞船有一个Vector2 pos变量,对应于它们的实际位置。
任何帮助都将不胜感激。

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 always facing the player with the "head". The player can move freely in the y and x directions. THe player and the spaceships have a Vector2 pos variable, that corresponds to their actual position. Any help would be appreciated.

这是我(现在正在工作)的代码:

This is my (now working) code:

    public void render(SpriteBatch sb) {
    Vector2 temp = new Vector2();
    float angle = temp.set(entityManager.getPlayer().pos).sub(pos).angle();
    TextureRegion region = new TextureRegion(texture);
    sb.draw(region, pos.x, pos.y, 0, 0, texture.getWidth(), texture.getHeight(), 1.0f, 1.0f,angle+90);
}

敌人太空飞船旋转,但他们并不总是面对玩家:

The enemy spaceships rotate, but they dont always face the player:

推荐答案

@ dwn的答案在数学上是正确的,但是当你处理LibGDX框架时,最好使用原生工具。

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

如果你有敌人和玩家的位置存储在 Vector2 然后你可以稍微方便地执行计算。
例如,确定特定敌人和玩家之间的角度:

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();

这将为您提供您想要的产品。当然最好将 temp 向量保存在公共空间的某个地方,以便在如此众多的计算中使用它。你最好检查其他方便的 Vector2 方法,它会让你的生活变得非常轻松。

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.

祝你好运。

这篇关于根据玩家位置旋转纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆