Unproject鼠标让3D世界坐标Libgdx [英] Unproject mouse to get 3D world coordinates Libgdx
问题描述
我的提问:我如何得到一个三维模型,以鼠标光标移动保持模型的y位置为0(使用Libgdx)
My Question: How do i get a 3D model to move with the mouse cursor keeping the y location of the model at 0. (Using Libgdx)
什么我已经尝试过: 我试图让我的三维模型跟随我的光标。
What i have tried: I'm trying to get my 3D models to follow my cursor.
目前,我只是做了模型运动与x和我的鼠标与添加的因素,如乘数和摄像头位置y坐标。这不太好,因为它不遵循光标,如果我旋转摄像头,则该模型没有在正确的方向前进。
At the moment i have simply made the models move with the x and y coordinates of my mouse with adding factors like a multiplier and camera location. This is not very good because it doesn't follow the cursor and if i rotate the camera then the model doesn't move in the correct direction.
这是用于移动模型codeI:
This is the code i used for moving the model :
instances.get(modelIndex).transform.setToTranslation(((Gdx.input.getX() - 650)/ 10)+ cam.position.x,0,((Gdx.input。 getY()以 - 400)/ 10)+ cam.position.z);
下面有一个链接到这个问题的图像(绿色圆圈就是鼠标)。
Here's a link to an image of the problem (The green circle is where the mouse is).
我知道,我要对此错误的,我应该使用unproject方法。我曾尝试使用unproject方法确实有一点成功,该模型只是坚持相机的近窗格中。我想留在0我的对象在y轴上,所以我用这个
I know that I'm going about this wrong and that I should be using the unproject method. I have tried to use the unproject method and did have little success, the model was just stuck to the near pane of the camera. I want my object to stay at 0 on the y axes so i used this
cam.unproject(mousePos);
instances.get(modelIndex).transform.setToTranslation(mousePos.x, 0, mousePos.z);
这则导致模型中的移动很慢(不使用鼠标),在正确的方向。此外,当我旋转或移动摄像头的模式将在距离或力挺摄像头。
This then resulted in the model moving quite slowly(not with the mouse), in the right direction. Also when I rotated or moved the camera the model would be set somewhere in the distance or even behind the camera.
推荐答案
您不能简单地使用 camera.unproject
,因为一个二维的坐标可以重新present多坐标在三维空间。相反,你可以使用 camera.getPickRay
获得雷
,这是由原点(相机的位置定义近平面)以及一个方向到三维空间。所以,你会得到的线在三维空间的重新presents二维坐标:
You cannot simply use camera.unproject
, because a single 2D coordinate can represent multiple coordinates in 3D space. Instead you can use camera.getPickRay
to get the Ray
, which is defined by the origin (the location on the camera's near plane) along with a direction into 3D space. So, you'll get the line in 3D space that represents the 2D coordinate:
Ray ray = cam.getPickRay(screenX, screenY);
现在你想在这一行(线),其中Y = 0点(距离产地):
Now you want the point (distance from origin) on that line (ray) where y=0:
ray.origin.y + ray.direction.y * distance = 0 <=>
ray.direction.y * distance = -ray.origin.y <=>
distance = -ray.origin.y / ray.direction.y
致使:
float distance = -ray.origin.y / ray.direction.y;
现在让在这个距离上的光线的位置:
Now to get the position on the ray at that distance:
Vector3 tmpVector = new Vector3();
tmpVector.set(ray.direction).scl(distance).add(ray.origin);
要设置模型实例转换到该值可以使用:
To set the model instance translation to that value you can use:
instance.transform.setTranslation(tmpVector);
下面实例
指的是你的 instances.get(modelIndex)
。
我会鼓励你使用InputProcessor(见<一href="http://$c$c.google.com/p/libgdx/wiki/InputEvent">http://$c$c.google.com/p/libgdx/wiki/InputEvent),所以把它放在一起:
I would encourage you to use InputProcessor (see http://code.google.com/p/libgdx/wiki/InputEvent), so to put it all together:
Vector3 tmpVector = new Vector3();
@Override
public boolean touchDragged (int screenX, int screenY, int pointer) {
Ray ray = cam.getPickRay(screenX, screenY);
final float distance = -ray.origin.y / ray.direction.y;
tmpVector.set(ray.direction).scl(distance).add(ray.origin);
instance.transform.setTranslation(tmpVector);
return true;
}
请注意,您可能需要重写接地
并返回真
为 touchDragged
被调用。
Note that you might need to override touchdown
and return true
for touchDragged
to be called.
这篇关于Unproject鼠标让3D世界坐标Libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!