Math Overview

来源:互联网 发布:iphone7在线软件 编辑:程序博客网 时间:2024/05/16 09:03
Math Overview

The XNA Framework Math Libraries can be found in the Microsoft.Xna.Framework namespace, alongside a number of additional types that deal with the XNA Framework Application model.

  • Coordinate system
  • Mathematical Constants and Scalar Manipulation
  • Basic Geometric Types
  • Precision and Performance

Coordinate system

The XNA Framework uses a right-handed coordinate system, with the positive z-axis pointing toward the observer when the positive x-axis is pointing to the right, and the positive y-axis is pointing up.

XNA采用右手坐标系。

Mathematical Constants and Scalar Manipulation

The XNA Framework provides the MathHelper Members class for manipulating scalar values and retrieving some common mathematical constants. This includes methods such as the ToDegrees and ToRadians utility methods for converting between degrees and radians.

XNA提供对工具类来访问数学常量和进行数值操作。

Basic Geometric Types

The XNA Framework Math library has multiple basic geometric types that can be used to manipulate objects in 2D or 3D space. The primitive objects in this library represent the data required to represent a geometric object or an operation on that object. Each geometric type has a number of mathematical operations that are supported for the type.
根据不同的几何类型,有不同的操作。

Vectors

The XNA Framework provides the Vector2, Vector3 and Vector4 classes for representing and manipulating vectors. A vector is typically used to represent a direction and magnitude. However, in the XNA framework it might also be used to store a coordinate or some other data type with the same storage requirements.

向量的定义。

 

Each vector class has methods for performing standard vector operations such as:

  • Dot product 点积
  • Cross product 叉积
  • Normalization 标准化
  • Transformation 转换
  • Linear, Cubic, Catmull-Rom, or Hermite spline interpolation.

常见的向量操作。

Matrices

The XNA Framework provides the Matrix class for transformation of geometry. The Matrix class uses a row major order to address matrices, which means that the row is specified before the column when describing an element of a two-dimensional matrix. The Matrix class provides methods for performing standard matrix operations such as calculating the determinate or inverse of a matrix, in addition to helper methods for creating scale, translation, and rotation matrices.

矩阵和矩阵操作。

Quaternions

The XNA Framework provides the Quaternion structure to represent and calculate the efficient rotation about a vector around a specified angle.

通过Quaternions来表示和计算一个向量绕一个特定角度的旋转。

Curves

The Curve class represents a hermite curve that allows you to interpolate varying positions at different times without having to explicitly define each position. The curve is defined by a collection of CurveKey points representing each varying position at different times.

This class can be used not only for spatial motion, but also to represent any response that changes over time.

曲线类。

Bounding Volumes

The XNA Framework provides the BoundingBox, BoundingFrustum, BoundingSphere, Plane, and Ray classes for representing simplified versions of geometry for the purpose of efficient collision and hit-testing. These classes have methods for checking for intersection and containment with each other.

这几个类用来做碰撞检测。

Precision and Performance

The XNA Framework Math libraries are single-precision. This means that the primitives and operations contained in this library use 32-bits to represent floating-point numbers in order to achieve a balance between precision and efficiency when performing large numbers of calculations.

使用32位float浮点数。

 

The 32-bit floating-point numbers can represent a range of values from negative 3.402823e38 to positive 3.402823e38. The 32-bits of a floating-point number are used to store the sign, mantissa, and exponent of the number. The result is seven digits of floating-point precision. Some numbers, such as π, 1/3, or the square root of two, can only be approximated with a limited amount of memory. Because the number of bits used to represent a decimal number limit the range of numbers it can represent, floating-point numbers only approximate decimal numbers that require more than seven digits of precision. Consequently, rounding errors can occur under different conditions through the use of any binary-representation approximating a floating point number.

因为精度的问题,会发生一些取整数的错误。

 

You can find more information about single-precision numbers in the documentation for the Single data type.

Bb203910.bp(en-US,XNAGameStudio.30).gifBest Practice

Depending on the hardware platform your game is targeting, you may have multiple processors available to perform floating-point calculations. For example, on the Xbox 360 platform and on many Windows-based computers, you have a CPU, on which your game code is running, but you also have GPU, on which your shader code is running. Therefore, you can also perform floating point calculations on the GPU using shaders and HLSL intrinsic functions. GPUs are very fast at performing large numbers of floating-point operations. This can be very useful, for example, if you need to perform a large number of floating point operations to animate a particle system.

However, you need to remember that the Zune platform does not have a GPU and does not support shaders. For Zune, you can use a conditional compilation directive #if ZUNE to write downlevel-code that behaves gracefully on this platform while still taking advantage of the performance gains on other types of hardware.

我们可以使用GPU来进行大量浮点数的计算。