开发者

Xna forward vector?

How can I easily do this? Is there 开发者_StackOverflowany function in the framework which I can use, as I created my own but I feel as if it's probably slow (new to programming so I have no idea).

public Vector3 anglestoforward(Vector3 angles)
{
    return new Vector3((float)(Math.Cos(angles.Y) * Math.Cos(angles.X)),(float)(Math.Sin(angles.Y) * Math.Cos(angles.X)),(float)(Math.Sin(angles.X * -1)));
}


But that's always the same, give me an example of how I would use that...

Although your code will work and it's performance not an issue if done a handful of times per frame, when you want to do anything more than rock bottom basics with 3D programming, you will learn to lean much more heavily on linear algebra (vector math) rather than angles and trigonometry. This will also involve storing your orientations as matrices or quaternions rather than angles. You will find yourself pretty much never need to resolve a set of angles to a vector like you are asking.

Basically, angles are easy to understand and are intuitive but the math to work with them becomes very complex compared to a linear algebra approach. The linear algebra approach may take a little more learning but it really opens up the flood gates of what is possible with creating 3D algorithms.

The built in methods of the various classes in Xna are geared more towards helping with a linear algebra approach to 3D rather than an angular approach.

If you want, here is an article I wrote that describes the basics of working with vectors and matrices instead of angles: Vector Math approach


Vector3 has a Forward property. If you're using a Matrix, you can use the Matrix.Forward property. Both will return a Vector3 containing the components of the Forward vector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜