限制拖动到对象的局部轴 [英] Constrain dragging to local axis of object

查看:80
本文介绍了限制拖动到对象的局部轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图沿局部轴拖动对象时,我正在尝试实现与3D建模程序非常相似的功能.主要区别在于,您无需在屏幕上移动光标,而是可以自由移动相机,并且光标始终位于屏幕中间.有点像第一人称游戏.

I'm trying to implement something very similar to what 3D modeling programs do, when dragging an object along a local axis. The main difference is, that you don't move the cursor around the screen, instead you freely move the camera and the cursor is always in the middle of the screen. Kind of like a first person game.

想象一下,将相机对准下面标有蓝色十字(光标)的立方体的表面.您按住鼠标以抓取"多维数据集.现在应将多维数据集的拖动限制在以大红色箭头表示的轴上.

Imagine pointing the camera at the face of the cube below, marked with a blue cross (the cursor). You press and hold the mouse to "grab" the cube. The dragging of the cube should now be constrained to the axis, represented with the big red arrow.

例如,您可以将相机移至左侧,然后将其向右旋转,如第二幅图像所示.在这种情况下,应将立方体移动到线框立方体的位置.该多维数据集的单个实体面与您最初捕获的相同.在这里,向上/向下移动相机(沿蓝线移动光标)应该不会影响多维数据集的新位置.

For instance you could have moved the camera to the left and turned it to the right, as seen in the second image. In this case, the cube should be moved to the position of the wireframed cube. The single solid face of that cube is the same that you grabbed originally. Here, moving the turning the camera up/down (moving the cursor along the blue line) should have no effect on the cube's new position.

我很难弄清楚其背后的数学原理.我知道立方体的开始位置和旋转.我一直都知道相机的位置和旋转.我可以确定被抓的脸和它的正常状态.

I'm having a hard time figuring out the math behind this. I know the cube's start position and rotation. I know the camera's position and rotation at all times. I can determine the face that was grabbed and its normal.

我正在考虑寻找与所抓取的面平行的平面,以及与红色箭头和光标相交的平面,但这似乎不正确.

I was thinking about finding the plane that is parallel to the grabbed face and its intersection with the red arrow and the cursor are closest to each other, but this doesn't seem to be correct.

我还试图以某种方式将光标投射到红色箭头上,基本上消除了沿蓝线的任何移动,但是我不确定如何.

I was also trying to somehow project the cursor onto the red arrow, basically eliminating any movement along the blue line, but I'm not sure how.

推荐答案

您可以使用2条线(准确地说是射线)将问题简化为2D问题.红色箭头投射到xy平面,并且与相机发出的光线相同(这意味着您都忽略了z轴),然后将其映射回3D.

You can reduce the problem to a 2D problem, using 2 line (rays to be precise). The red arrow projected to the xy plane and the same with the ray from the camera (meaning you ignore the z axis for both), then map it back to 3D.

首先获取人脸中心(faceStartPos)和相机中心.

First get the center of the face (faceStartPos) and center of the camera.

根据Blender的方向(从屏幕上向下移z,向右移y,从屏幕向外移出x),使用照相机的旋转来确定从照相机投射的线(我想您可以算出,这类似于您从多维数据集获得范数的方式,我认为),法线(以其起始位置为准)是从脸部中心开始的线.然后,您将拥有一个由2个线性方程组和2个未知数组成的系统,还有一个小代数,您可以确定它们的值(对于xy).至于求解一个由2个方程组成的系统,我认为您应该采用矩阵方法(至少我发现最容易在代码中实现,特别是如果您知道这是一个由2个变量组成的系统.

Going by Blender's orientation (z up, y right, x out from screen) of the you use the rotation of the camera to determine the line projected from the camera (I think you can figure that out, would be similar to how you got the norm from the cube, I assume), and the normal (with its starting position) as the line from the center of the face. Then you have a system of 2 linear equation with 2 unknowns, a little algebra and you can determine their values (for x and y). As for solving a system of 2 equation, I think you shoudl take the matrix approach (at least i find it easiest to implement in code, especially if you know it is is a system for 2 variables.

之后,您可以确定xy的值,则具有等式faceStartPos + t * normVec,其中faceStartPos.x + t * normVec.x求解t.

After that you can determine the values for x and y, you have the equation faceStartPos + t * normVec where faceStartPos.x + t * normVec.x, solve for t.

t是移动多维数据集所需的魔术变量,您要做的只是newCubeCenter = oldCubeCenter + t * normVec

t is the magic variable that you really needed to move the cube, all you do is newCubeCenter = oldCubeCenter + t * normVec

注意事项:

  • (臭名昭著)浮点错误
  • 相机与法线几乎(或完全)对齐以使其平行的点

这篇关于限制拖动到对象的局部轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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