WPF3D 如何操纵平行于相机的平面中的对象 [英] WPF3D How to manipulate objects in a plane parallel to the camera

查看:33
本文介绍了WPF3D 如何操纵平行于相机的平面中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 WPF 中创建一个应用程序.我经常使用 3DTools.我有一个四面体,里面有一个立方体.我想在 3D 方向上移动立方体.

I am making a application in WPF. I am using a lot the 3DTools. I have a tetrahedron and, inside it, a cube. I want to move the cube in 3D directions.

我的四面体以 (0,0,0) 为中心使用轨迹球旋转(以编程方式,我的视口上有一个透明边框来捕获事件).

My tetrahedron, centered in the (0,0,0) rotates using a Trackball (programmatically, I have a transparent border over my viewport to capture the events).

应用程序的用户认为在其中移动立方体的更自然的方法是始终在 2D 平面(平行于屏幕)中移动它.因此,如果我想在屏幕中更深地移动它,例如,我只需旋转四面体并在 Y 轴上移动它.我认为这是一个很好的解决方案.

The users for the application think the more naturally way to move the cube inside it is to move it always in a 2D plane (parallel to the screen). So, if I want to move it deeper in the screen, I just rotate the tetrahedron and move it in Y axis, for example. I think this can be a good solution.

我有问题,我想帮助解决:首先,当我移动我的四面体(在轨迹球内)时,轨迹球看起来像移动所有系统坐标.我这样说是因为我为了调试目的而绘制轴,并且这些轴也在旋转.但这不是真的 - 它们移动只是因为轨迹球内的物体(对吧?).编辑 实际上,我认为轨迹球内部的坐标系确实在发生变化.当我在 Y-X 轴上移动立方体时,我的行为正常.但是如果我稍微转动一点,并尝试在 Z 轴上移动,我会看到它变大(靠近相机)和变小(远离相机).=(

I have to problems, and I would like help to solve then: first, when I move my tetrahedron (inside the Trackball) the Trackball looks like moving all the system coordinates. I say this because I draw axis for debug purpose, and those axis are being rotate also. But this is not true - they are moving just because are objects inside the trackball (right?). EDIT Actually, I think the coordinate system inside the trackball are really changing. When I move the cube in Y-X axis, I behaves ok. But if I turn a little bit, and try to move in the Z axis, I see it getting bigger (closer to camera) and smaller (far from camera). =(

所以我的问题是如何定义一个不会被轨迹球旋转的平面?对我来说,答案看起来像好的,只需在轨迹球外定义它",但是,正如我所说的(和 3DTools 文档说)我的视口有一个边框......所以我不知道如何处理这个.

So my questions is how to define a plane which will not be rotate by trackball? For me the answer looks something like "ok, just define it outside the track ball" but, as I said (and 3DTools documentation say) I have a border over my viewport... So I do not know how to handle this.

我的四面体在一组 ScreenSpaceLines3D 中.我将不得不把立方体放在里面.我的第二个问题是如何追踪旋转四面体的顶点位置?当 Position 属性给我原始位置而不是其他任何东西时,我真的很失望.我应该保留矩阵变换,然后在需要时应用到我的点吗?

My tetraedron in a set of ScreenSpaceLines3D. I will have to keep the cube inside it. My second question is how to I trace the positions of the vertex of the tetrahedron of it being rotated? I got really disappointed when the Position property gave me the original positions and nothing else. Should I keep the matrix transformations and apply then to my points when I need then?

对不起,我写的太长了,但我真的陷入了这不是太多的编程问题.熟悉WPF,还有些概念欠缺.

Sorry for long text, but I really stuck in this not so much programming questions. I am knew in WPF, I still have some lacks of concepts.

提前致谢,

佩德罗

推荐答案

然后我要做的是尝试制作自己的操纵器(创建新的类来替换 3DTools 中的 TrackBall Manipulator)来完成这些.例如,过去我通过简单的矩阵/矢量操作制作了缩放/平移、围绕目标旋转、自由旋转、自由移动、旋转对象、平移对象等.

What I'd do is then try to make your own manipulators (create new classes that replace TrackBall Manipulator from 3DTools) to do these. For instance, in the past I have made Zoom/pan, Rotate around a target, Free rotate, Free move, Rotate object, Translate object etc... all by doing simple matrix / vector manipulations.

对于您的示例,您需要做的是计算与 Camera->Target 和世界向上向量之间的平面正交的法线.这将为您提供一个平行"于相机顶部的矢量,如果您正在查看屏幕,则就像本地 X 轴一样.使用这个向量可以限制立方体在这个方向上的运动.

For your example, what you need to do is calculate the normal which is orthogonal to the plane between Camera->Target and the world up vector. this will give you a vector which is "parallel" top the camera, like a local X-Axis if you are looking into the screen. Using this vector you can constrain the motion of the cube in this direction.

为此,计算从相机到目标的向量 V.这是通过从相机位置中减去对象位置来实现的.然后,计算要横向平移的向量,如下所示:

To do this, compute the vector V from camera to target. This is achieved by substracting the object location from the camera location. Then, calculate a vector to translate in sideways as follows:

给定要定位的相机向量,v = xi, yj, zk
对向量进行归一化,v' = xi, yj, zk/sqrt(xi^2 + yj^2 + zk^2)
让 U = 全局世界向上向量 u = 0, 0, 1

Given a vector of your camera to target, v = xi, yj, zk
Normalise the vector, v' = xi, yj, zk / sqrt(xi^2 + yj^2 + zk^2)
Let U = global world up vector u = 0, 0, 1

然后我们可以计算 R = 水平向量,平行于相机的视线方向

Then we can compute R = Horizontal Vector that is parallel to the camera's view direction

R = v' ^ U, where ^ is the cross product, given by   
 a ^ b = (a2b3 - a3b2)i + (a3b1 - a1b3)j + (a1b2 - a2b1)k 

这会给你一个看起来像这样的向量.

This will give you a vector that looks like this.

现在您知道了这个向量 R,当您向左/向右移动鼠标时,您可以使用它来平移对象.例如,在伪代码中:

Now you know this vector R, when you move the mouse left/right you can use this to translate the object. For instance, in pseudocode:

// Pseudocode
onMouseMove()
{
    // Using calculation for R vector above
    Vector r = calulateVector();

    // Assuming passed in the number of points to translate 
    // (or some derivative of mousemove)
    let int pts = mousePointsMovedLeftOrRight;

    // Perform a translate in direction of R
    Vector targetPosition = TargetObject.PositionVector;
    targetPosition.X += r.X * pts;
    targetPosition.Y += r.Y * pts;
    targetPosition.Z += r.Z * pts;
}

我在家里的笔记本电脑上有代码可以做到这一点(它是 OpenGL,但原理相同 - 在 3D 空间中提供对对象和相机的各种操作).如果您想了解更多,请随时与我联系/评论.

I've got code on my laptop at home which can do this (It's OpenGL but same principle - provides various manipulations of objects and camera in 3D space). Please feel free to contact me/comment if you want to know more.

最好的问候,

这篇关于WPF3D 如何操纵平行于相机的平面中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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