[英]3D object rotation in WPF
我在WPF中有一個3D透明對象。 用於創建對象的筆刷的不透明度值已設置為0.25。 我必須使用鼠標移動將3D對象繞其中心點旋轉。 目前,對象逆着鼠標移動的方向旋轉。 有誰知道如何在3D空間中旋轉透明對象?
用於旋轉的代碼是:
public static void DoMouseMoveEvent(object sender, MouseEventArgs e, Transform3DGroup transform3DGroup, System.Windows.Controls.Viewport3D viewport3D, ref Point mLastPos, Point3D centerPoint)
{
var pos = Mouse.GetPosition(viewport3D);
var actualPos = new Point(pos.X - viewport3D.ActualWidth / 2,
viewport3D.ActualHeight / 2 - pos.Y);
double dx = actualPos.X - mLastPos.X;
double dy = actualPos.Y - mLastPos.Y;
double mouseAngle = 0;
if (dx != 0 && dy != 0)
{
mouseAngle = Math.Asin(Math.Abs(dy) /
Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)));
if (dx < 0 && dy > 0) mouseAngle += Math.PI / 2;
else if (dx < 0 && dy < 0) mouseAngle += Math.PI;
else if (dx > 0 && dy < 0) mouseAngle += Math.PI * 1.5;
}
else if (dx == 0 && dy != 0)
{
mouseAngle = Math.Sign(dy) > 0 ? Math.PI / 2 : Math.PI * 1.5;
}
else if (dx != 0 && dy == 0)
{
mouseAngle = Math.Sign(dx) > 0 ? 0 : Math.PI;
}
double axisAngle = mouseAngle + Math.PI / 2;
var axis = new Vector3D(Math.Cos(axisAngle) * 4, Math.Sin(axisAngle) * 4, 0);
//axis.Normalize();
double rotation = 0.02 * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
var r = new QuaternionRotation3D(new Quaternion(axis, rotation * 180 / Math.PI));
var rotateTransform = new RotateTransform3D(r, centerPoint);
transform3DGroup.Children.Add(rotateTransform);
mLastPos = actualPos;
}
行為不正確的原因可能是上述代碼段中未捕獲到用於計算旋轉的z坐標嗎?
謝謝您的幫助....
我認為z坐標有問題。 因為z坐標用於在三維中定位,如果不使用此坐標,則很難在三維中旋轉圖形。 如果您計算z坐標,我認為問題將會解決
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.