- XNA 4 3D Game Development by Example:Beginner's Guide
- Kurt Jaegers
- 432字
- 2021-08-05 18:55:29
Time for action – implementing a look-at point
- Add the following properties to the
Fields
region of theCamera
class:private Vector3 lookAt; private Vector3 baseCameraReference = new Vector3(0, 0, 1); private bool needViewResync = true;
- Add the following region and method to the
Camera
class:#region Helper Methods private void UpdateLookAt() { Matrix rotationMatrix = Matrix.CreateRotationY(rotation); Vector3 lookAtOffset = Vector3.Transform( baseCameraReference, rotationMatrix); lookAt = position + lookAtOffset; needViewResync = true; } #endregion
- Define the
MoveTo()
method that is called in the constructor. This method should be placed inside the Helper Methods region you just created:public void MoveTo(Vector3 position, float rotation) { this.position = position; this.rotation = rotation; UpdateLookAt(); }
- Add two new public properties to the
Properties
region of theCamera
class:public Vector3 Position { get { return position; } set { position = value; UpdateLookAt(); } } public float Rotation { get { return rotation; } set { rotation = value; UpdateLookAt(); } }
What just happened?
Just like the camera's position, the point we are going to look at is stored as a Vector3
. In order to build this point, we need a frame of reference, indicating the direction the camera would be pointing if it were not rotated at all. We define this direction in the baseCameraReference
field, specifying that the non-rotated camera will point along the Z axis (assuming that the camera was located at the origin point (0, 0, 0)).
The last field we added, needViewResync
, will be used to determine when we need to rebuild the next important matrix we will be discussing – the View
matrix. We will return to that topic in a moment.
In order to determine the point in 3D space that the camera will look towards (called the Look At point), we create a rotation
matrix around the Y axis (which points up from the X-Z plane) equal to the current value of the rotation field. We then transform the baseCameraReference
vector with this rotation
matrix, resulting in a Vector3
which points in the direction of the rotation relative to the world origin at (0, 0, 0) in 3D space.
We then build the lookAt
point field by adding this offset vector to the camera's current position, in effect relocating the lookAt
point from the origin to be relative to the camera position. Finally, we mark the needViewResync
flag as true.

The remaining code mentioned previously implements the MoveTo()
method, and exposes public properties for the Position
and Rotation
values. All three of these items simply set their related fields and call the UpdateLookAt()
method we just defined.
- 我們都是數據控:用大數據改變商業、生活和思維方式
- 數據庫技術與應用教程(Access)
- 數據分析實戰:基于EXCEL和SPSS系列工具的實踐
- Access 2007數據庫應用上機指導與練習
- 數據化網站運營深度剖析
- Oracle高性能自動化運維
- Sybase數據庫在UNIX、Windows上的實施和管理
- 大話Oracle Grid:云時代的RAC
- Python金融實戰
- 白話大數據與機器學習
- 大數據架構商業之路:從業務需求到技術方案
- 探索新型智庫發展之路:藍迪國際智庫報告·2015(下冊)
- 新手學會計(2013-2014實戰升級版)
- 數據修復技術與典型實例實戰詳解(第2版)
- SIEMENS數控技術應用工程師:SINUMERIK 840D-810D數控系統功能應用與維修調整教程