Fundamentals of 3D mathematics | coordinate system

Posted by o2cathy on Sat, 18 Dec 2021 12:33:45 +0100

Fundamentals of 3D Mathematics: graphics and game development

preface

Baiyu wubing decided to open a new pit. Reread fundamentals of 3D Mathematics: graphics and game development (First Edition).

Combine Cocos Creator engine and other related books, sort out and record some notes.

start

There are many kinds of coordinate systems. This paper mainly focuses on Cartesian Coordinate System.

From game engine architecture

The first criterion of computer graphics: approximation principle. If it looks right, it's right.

Left hand coordinate system and right hand coordinate system

There are two coordinate systems in 3D coordinate system: left-hand coordinate system and right-hand coordinate system.

Stretch out your hands and make your thumb (x), index finger (y) and middle finger (z) perpendicular to each other to form the corresponding coordinate system.

Left hand coordinate system and right hand coordinate system

🎈 The coordinate system in OpenGL is generally the right-hand coordinate system, while in Direct3D, it is generally the left-hand coordinate system.

🥥 The world coordinate system of Cocos Creator 3.0 adopts the Cartesian right-hand coordinate system. By default, x is right, y is up, z is outward, and the - z axis is straight ahead.

Cocos Creator and right hand coordinate system

The rotation also follows the law of corresponding left and right hands, stretch out your hands and give a praise 👍 Gesture, thumb pointing towards, four fingers holding the direction is positive.

Rotation and left and right hands

🥥 Rotation in Cocos Creator 3.0 follows the right-hand rule. Looking from the steering axis to the origin, when the attribute value is positive, the node rotates counterclockwise. When the attribute value is negative, the node rotates clockwise.

Cocos Creator rotation

⚠️ Note: the rotation attribute on the node is a quaternion that represents the angle of rotation about any axis. The attribute corresponding to rotation in the attribute inspector is the Euler angle attribute Euler angles. These two attributes can be used separately according to requirements. Please pay attention to the corresponding differences between them and the attribute names of the editor panel when using the API.

The left and right hand coordinate systems can be converted to each other, just flip the symbol of one axis.

Flip one axis

World coordinate system

World Coordinate is a special coordinate system, which establishes the largest space we are concerned about.

The world coordinate system establishes the reference framework needed to describe other coordinate systems. In other words, the world coordinate system can be used to describe the position of other coordinate systems, but not a larger and external coordinate system.

World coordinate system

The world coordinate system is also widely known as the global coordinate system or the cosmic coordinate system.

Typical questions about the world coordinate system are about the initial position and environment:

  • Position and direction of each object
  • Camera position and orientation
  • What is the terrain of every point in the world
  • Where do objects come from and go (NPC motion strategy)

🥥 The world coordinate system, also known as the absolute coordinate system, represents the unified coordinate system in the scene space in Cocos Creator 3.0 game development, and "world" is used to represent our game scene.

Cocos Creator world coordinate system

Object coordinate system

Local Coordinate is a coordinate system related to a specific object. Each object has its own coordinate system. When the object moves or changes direction, the coordinate system related to the object will also change.

In some cases, the object coordinate system is also called the model coordinate system. Because the coordinates of model vertices are described in model coordinates.

💡 Object coordinate system | local coordinate system | model coordinate system | local coordinate system ❕ It can be regarded as one thing and can be called in many ways.

Possible problems in object coordinate system:

  • Are there interacting objects around? Shall I attack it
  • Which way? In front of me, behind me? Left? right? I should shoot at it 🔫,, Or turn around and run 🏃?)

Object coordinate system

🎈 The space defined by the model is called local space or model space. The term used in OpenGL documents is object space.

🥥 There can be a hierarchical structure of parent-child relationship between the nodes of Creator 3.0. The Node Position we set by modifying the Position attribute of the Node is the local coordinate system of the Node relative to the parent Node, not the world coordinate system.

Creator local coordinate system

🎈 3D artists are doing similar things when building 3D objects. Instead of building the geometry of the object in the global scene coordinate system (i.e. world space, also translated as world space), they create the object relative to the local coordinate system (local space, also translated as local space).

Camera coordinate system

The camera coordinate system can be regarded as a special object coordinate system, which is defined in the visible area of the camera screen.

Camera coordinate system

Some typical problems about camera coordinate system:

  • Is a point in 3D space in front of the camera?
  • Is a point in 3D space on the screen or beyond the boundary of the camera plane cone? (crop coordinate system)
  • Is an object on the screen? Is it partial or not at all?
  • Two objects, who is in front? (visibility detection)

Camera coordinate system

🎈 We assign a local coordinate system to the camera (this is called view space, also translated as observation coordinate system, view space, eye space or camera space)

🥥 The visual range of the camera is composed of a Frustum composed of six planes. Near Plane and Far Plane are used to control the visual distance and range near and far. At the same time, they also constitute the size of the viewport.

video camera

Coordinate system conversion

Coordinate transformation: know the coordinates of a point and how to describe the point in another coordinate system. The point does not really move, but describes its position in different coordinate systems.

OpenGL and DirectX coordinate system

Usually, the changes between coordinate systems will be represented by a matrix (dig a pit first and talk about it in detail later).

coordinate transformation

🎈 The process of converting the coordinates in the local coordinate system to the global scene coordinate system is called world transform, and the transformation matrix used is called world matrix

Then from global coordinates to local coordinates, use the inverse of the world matrix!

coordinate transformation

🎈 The coordinate transformation from world space to observation space is called view transformation (also translated into observation transformation, view transformation, etc.), and the matrix used in this transformation is called view matrix (also translated into view matrix).

The camera is also a kind of local coordinates, that is, the observation matrix is equivalent to the inverse of the camera's world matrix!

SRT MVP

🌰 For example, there are two nodes in the scene, each of which has a child node. Find the coordinates of one child node in the local coordinate system of the other child node.

for instance

Solve the above example in three steps:

  1. Find the world coordinates of B0
  2. Find the inverse of the world matrix of A0
  3. Multiply the results of the above two steps
aW1wb3J0IHsgX2RlY29yYXRvciwgQ29tcG9uZW50LCBOb2RlLCBWZWMzLCB2MywgbWF0NCwgTWF0NCB9IGZyb20gJ2NjJzsKaW1wb3J0IHsgRURJVE9SIH0gZnJvbSAnY2MvZW52JzsKY29uc3QgeyBjY2NsYXNzLCBwcm9wZXJ0eSwgZXhlY3V0ZUluRWRpdE1vZGUgfSA9IF9kZWNvcmF0b3I7Cgpjb25zdCB0ZW1wX21hdDQgPSBtYXQ0KCk7CkBjY2NsYXNzKCdOb2RlRXh0cmFJbmZvJykKQGV4ZWN1dGVJbkVkaXRNb2RlCmV4cG9ydCBjbGFzcyBOb2RlRXh0cmFJbmZvIGV4dGVuZHMgQ29tcG9uZW50IHsKCiAgICBAcHJvcGVydHkKICAgIHByaXZhdGUgX3JlZnJlc2ggPSBmYWxzZTsKICAgIEBwcm9wZXJ0eSh7ZGlzcGxheU5hbWU6ICLliLfmlrAifSkKICAgIGdldCByZWZyZXNoKCkgewogICAgICAgIHJldHVybiB0aGlzLl9yZWZyZXNoOwogICAgfQogICAgc2V0IHJlZnJlc2godmFsdWUpIHsKICAgICAgICB0aGlzLm9uVHJhbnNmb3JtQ2hhbmdlKCkKICAgIH0KCiAgICBAcHJvcGVydHkoeyByZWFkb25seTogdHJ1ZS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cml4SW52ZXJ0ID0gTWF0NC5pbnZlcnQodGVtcF9tYXQ0LCB0aGlzLm90aGVyTm9kZS53b3JsZE1hdHJpeCk7CiAgICAgICAgICAgIFZlYzMudHJhbnNmb3JtTWF0NCh0aGlzLm90aGVyTm9kZVBvc2l0aW9uLCB0aGlzLndvcmxkUG9zaXRpb24sIG90aGVyTm9kZVdvcmxkTWF0cml4SW52ZXJ0KTsKICAgICAgICB9CiAgICB9Cn0=

Complex plane

Just talking about the coordinate system, let's talk about something else.

In a complex plane, there is a real axis and an imaginary axis. The geometric meaning of the product of complex numbers is just rotation and scaling.

Complex plane

end

Baiyu wubing expects to write notes on vectors in the next issue 📒.

reference material

Fundamentals of 3D Mathematics: graphics and game development

Game engine architecture

  • DirectX 12 3D game development practice
  • Math girl