2016-08-24 43 views
0

我想要做的是接受输入的用户触摸,将触摸的相机坐标转换为世界坐标,然后创建一个矢量以便使用对我的身体施加压力。 这是我必须为:基于触摸输入在Libgdx中使用力移动物体

Vector3 worldPos = new Vector3(newX, newY, 0); 
    GameScreen.gameCam.unproject(worldPos); 

    newX = worldPos.x; 
    newY = worldPos.y; 

    Vector2 direction = new Vector2(); 

    direction.set((newX - xPos), (newY - yPos)); 
    if (newX <= xPos){ 
     direction.set((xPos - newX), (yPos - newY)); 
    } 

    b2Body.applyForceToCenter(direction, true); 

我知道,这确实是不正确的,在所有尽可能创建矢量去,但坐标被正确地转换。

所以我的问题是,我如何获得一个精确的矢量,即使在负方向,当我有身体的世界坐标和触摸?

我已经尝试做一些研究,以便更好地了解所有这些,但我有一些麻烦。任何帮助将不胜感激,谢谢你的时间。

回答

1

如果您对下一页末,newY屏幕触摸坐标和未来改变其对世界COORDS您可以通过减法touchWorldPos得到方向currentObjectPos:

Vector3 worldPos = new Vector3(newX, newY, 0); 
GameScreen.gameCam.unproject(worldPos); 

newX = worldPos.x; 
newY = worldPos.y; 

Vector2 direction = new Vector2(); 
direction.set((newX - xPos), (newY - yPos)); 


b2Body.applyForceToCenter(direction, true); 

没有这个if语句:

if (newX <= xPos){ 
    direction.set((xPos - newX), (yPos - newY)); 
} 

在box2d这个方向可能有小的值,你必须通过一些像100这样的值(mayby更多)