admin管理员组文章数量:1024629
I try to rotate a Vector (0,0,-1) around an axis (Y) (0,1,0) with specific degrees (90°) using the System.Numerics Libary from .NET
My Problem is that i get a wrong output and i dont get the Problem. The Output from the Code is: Original vector: <0 0 -1> rotated vector: <-0,99999994 0 -5,9604645E-08>
If i Rotate the Vector (0,0,-1) Around (0,1,0) with 990 degrees the output should be (-1,0,0)
using System.Numerics;
class Program
{
static void Main()
{
Vector3 vector = new Vector3(0,0,-1);
Vector3 rotateAxis = new Vector3(0,1,0);
float angle = 90;
//Create Radians
float radians = (float)(angle *Math.PI/180);
Quaternion rotation = Quaternion.CreateFromAxisAngle(rotateAxis, radians);
// Rotate Vector
Vector3 rotatedVector = Vector3.Transform(vector,rotation);
Console.WriteLine($"Original vector: {vector}");
Console.WriteLine($"rotated vector: {rotatedVector}");
}
}
I try to rotate a Vector (0,0,-1) around an axis (Y) (0,1,0) with specific degrees (90°) using the System.Numerics Libary from .NET
My Problem is that i get a wrong output and i dont get the Problem. The Output from the Code is: Original vector: <0 0 -1> rotated vector: <-0,99999994 0 -5,9604645E-08>
If i Rotate the Vector (0,0,-1) Around (0,1,0) with 990 degrees the output should be (-1,0,0)
using System.Numerics;
class Program
{
static void Main()
{
Vector3 vector = new Vector3(0,0,-1);
Vector3 rotateAxis = new Vector3(0,1,0);
float angle = 90;
//Create Radians
float radians = (float)(angle *Math.PI/180);
Quaternion rotation = Quaternion.CreateFromAxisAngle(rotateAxis, radians);
// Rotate Vector
Vector3 rotatedVector = Vector3.Transform(vector,rotation);
Console.WriteLine($"Original vector: {vector}");
Console.WriteLine($"rotated vector: {rotatedVector}");
}
}
Share
Improve this question
asked Nov 18, 2024 at 11:57
SciroccoXCodingSciroccoXCoding
31 silver badge1 bronze badge
1 Answer
Reset to default 1The result is correct. You didn't get an precise result because the rotation angle itself is not precise. You can round the result to eliminate some bias. For example:
rotatedVector = rotatedVector.Round();
public static class Vector3Extension
{
public static Vector3 Round(this Vector3 v, int decimals = 3)
{
return new Vector3(
(float)Math.Round(v.X, decimals),
(float)Math.Round(v.Y, decimals),
(float)Math.Round(v.Z, decimals));
}
}
I try to rotate a Vector (0,0,-1) around an axis (Y) (0,1,0) with specific degrees (90°) using the System.Numerics Libary from .NET
My Problem is that i get a wrong output and i dont get the Problem. The Output from the Code is: Original vector: <0 0 -1> rotated vector: <-0,99999994 0 -5,9604645E-08>
If i Rotate the Vector (0,0,-1) Around (0,1,0) with 990 degrees the output should be (-1,0,0)
using System.Numerics;
class Program
{
static void Main()
{
Vector3 vector = new Vector3(0,0,-1);
Vector3 rotateAxis = new Vector3(0,1,0);
float angle = 90;
//Create Radians
float radians = (float)(angle *Math.PI/180);
Quaternion rotation = Quaternion.CreateFromAxisAngle(rotateAxis, radians);
// Rotate Vector
Vector3 rotatedVector = Vector3.Transform(vector,rotation);
Console.WriteLine($"Original vector: {vector}");
Console.WriteLine($"rotated vector: {rotatedVector}");
}
}
I try to rotate a Vector (0,0,-1) around an axis (Y) (0,1,0) with specific degrees (90°) using the System.Numerics Libary from .NET
My Problem is that i get a wrong output and i dont get the Problem. The Output from the Code is: Original vector: <0 0 -1> rotated vector: <-0,99999994 0 -5,9604645E-08>
If i Rotate the Vector (0,0,-1) Around (0,1,0) with 990 degrees the output should be (-1,0,0)
using System.Numerics;
class Program
{
static void Main()
{
Vector3 vector = new Vector3(0,0,-1);
Vector3 rotateAxis = new Vector3(0,1,0);
float angle = 90;
//Create Radians
float radians = (float)(angle *Math.PI/180);
Quaternion rotation = Quaternion.CreateFromAxisAngle(rotateAxis, radians);
// Rotate Vector
Vector3 rotatedVector = Vector3.Transform(vector,rotation);
Console.WriteLine($"Original vector: {vector}");
Console.WriteLine($"rotated vector: {rotatedVector}");
}
}
Share
Improve this question
asked Nov 18, 2024 at 11:57
SciroccoXCodingSciroccoXCoding
31 silver badge1 bronze badge
1 Answer
Reset to default 1The result is correct. You didn't get an precise result because the rotation angle itself is not precise. You can round the result to eliminate some bias. For example:
rotatedVector = rotatedVector.Round();
public static class Vector3Extension
{
public static Vector3 Round(this Vector3 v, int decimals = 3)
{
return new Vector3(
(float)Math.Round(v.X, decimals),
(float)Math.Round(v.Y, decimals),
(float)Math.Round(v.Z, decimals));
}
}
本文标签: cRotate a 3D Vector around Axis and degreesStack Overflow
版权声明:本文标题:c# - Rotate a 3D Vector around Axis and degrees - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619560a2159492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论