2015-10-23 35 views
0

我想根据玩家手臂的旋转来拍摄一个物体(咒语)。这个咒语应该是从手中出来并射向鼠标所在的地方(手臂旋转并指向鼠标的位置)。这是手臂在游戏中旋转的方式。在libgdx中基于手臂旋转的拍摄对象java

public boolean mouseMoved(int screenX, int screenY) { 
    tmp.x = screenX; 
    tmp.y = screenY; 
    tmp.z = 0; 
    cam.unproject(tmp); 
    rot = MathUtils.radiansToDegrees * MathUtils.atan2((float)tmp.y - (float)player.getArmSprite().getY() - player.getArmSprite().getHeight(), 
      tmp.x -player.getArmSprite().getX() - player.getArmSprite().getWidth()); 
    if (rot < 0) rot += 360; 

    //Last right or left means if hes looking left or right 
    if(player.lastRight) 
     player.setObjectRotation(rot + 80); 
    if(player.lastLeft) 
     player.setObjectRotation(-rot - 80); 

这是法术应该如何拍基于脱旋转

//destination is a vector of where on screen the mouse was clicked 
    if(position.y < destination.y){ 
     position.y += vel.y * Gdx.graphics.getDeltaTime(); 
    } 
    if(position.x < destination.x){ 
     position.x += vel.x * Gdx.graphics.getDeltaTime(); 
    } 

然而,这是非常靠不住的,从来没有真正反应也应该有一些例外的方式。它从手中发射,然后如果y轴相等,它完全平均并且一直到它到达x位置,我希望它从手中发射到从点a到点b完全直线的位置,这很清楚一个旋转问题,我似乎无法弄清楚如何解决。

这里是发生了什么事

Example image

的图像

的红色显示我点击,你可以看到它首先到达X位置,现在正行驶到Y,当它应该已经达到我首先点击的x和y位置

对此问题的任何帮助都非常感谢!

+0

目前还不清楚你有什么问题。 “won and,从不真实地反应它应该怎样”是什么意思?请更具体一些,比如“它只是中途移动”或“手臂绕着一圈旋转并将屏幕弹出到用户的鼻子中” –

+0

@RyanBemrose我编辑它 – Temo

回答

1

我在弧度和切线方面很不好,但幸运的是我们有矢量。

由于您拥有的手臂角度为rot。我建议使用矢量现在用于任何矢量相关数学。

//A vector pointing up 
Vector2 direction = new Vector2(0, 1); 

//Let's rotate that by the rotation of the arm 
direction.rotate(rot); 

现在方向是手臂指向的方向。如果您的rotation计算在up = 0。所以你可能需要将它旋转180度,90度或-90度。或者你在某种程度上做了一些愚蠢的事情。

你的法术也应该有一个向量的位置。把它放在手上或你想从哪里开始。现在你所需要做的就是扩大direction,因为它现在的长度为1.如果你想移动5个单位,每帧你可以做direction.scl(5)现在它的长度为5.但从技术上讲,现在没有方向了,现在大家都称之为所以我们来做吧。

//when you need to fire 
float speed = 5; 
Vector2 velocity = direction.cpy().scl(speed); 

//update 
position.add(velocity); 
draw(fireballImage, position.x, position.y); 

我先复制了方向,否则它也会被缩放。然后,我只是将速度添加到该位置并使用该Vector进行绘制。

并显示向量是真棒,你应该看到这个真棒badlogic与我创建的鼠标程序。 https://github.com/madmenyo/FollowMouse只有我自己的代码的视图线。它只需要一点点的矢量知识,而且非常易读。