制作2D物理引擎:空间与物体(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/making-a-d-physics-engine-spaces-and-bodies-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2414 个词 阅读量 0制作2D物理引擎:空间与物体(译文)
原文地址:https://www.codeproject.com/Articles/1067334/Making-a-D-Physics-Engine-Spaces-and-Bodies
原文作者:Arav Singhal
译文由本站 robot-v1.0 翻译
前言
The basics of spaces, transformations and bodies used in a 2D physics engine.
2D物理引擎中使用的空间,变换和物体的基础知识.
制作2D物理引擎:系列(Making a 2D Physics Engine: The Series)
这是第二篇文章(This is the second article in the)制作2D物理引擎(Making a 2D Physics Engine)系列.如果您尚未阅读本系列之前的所有文章,我强烈建议您绕道而过.(Series. If you haven’t already read all the articles in the series before this one, I strongly recommend that you take a detour and skim through them.)
- 制作2D物理引擎:数学(Making a 2D Physics Engine: The Math)
- 制作2D物理引擎:空间与物体(Making a 2D Physics Engine: Spaces and Bodies)
- 制作2D物理引擎:形状,世界和整合(Making a 2D Physics Engine: Shapes, Worlds and Integration)
- 制作2D物理引擎:质量,惯性和力(Making a 2D Physics Engine: Mass, Inertia and Forces)
先决条件(Prerequisites)
线性代数的基本知识,包括2D向量和2x2矩阵,如本系列第一篇文章所述.(Basic knowledge of linear algebra including 2D vectors and 2x2 matrices as covered in the first article in the series.)
介绍(Introduction)
本文旨在介绍局部空间和世界空间的概念,并处理从局部空间到世界空间的转换,反之亦然.它还概述了物理引擎中实体或物理实体的表示.(This article intends to introduce the concepts of Local space and World space and deal with transformations from Local to World space and vice versa. It also outlines the representation of a body or physical entity in the physics engine.)
空间(Spaces)
空间是相对于其定义对象属性(即旋转和位置)的无限范围.(Spaces are limitless extents relative to which an object’s properties, i.e. rotation and position are defined.)
世界空间(World Space)
一个世界上的所有物体在空间中都有确定的位置和旋转,这是相对于世界原点测量的.世界空间无处不在;我们世界上的所有实体都将相对于它放置.(All objects in a world have a definite position and rotation in space, which are measured relative to the world origin. World space is omnipresent; all entities in our world will be placed relative to it.)
当地空间(Local Space)
局部空间是相对于世界上单个实体的.在实体的局部空间中测量时,其他实体的属性是相对于该实体进行测量的.(Local space is relative to a single entity in the world. When measured in local space of an entity, the properties of other entities are measured relative to that entity.)
转型(Transformation)
让给定点为(Let the given point be)(X)((X))在世界空间和(in world space and)(X')((X'))在相对于实体的局部空间中.如果该实体的位置(本地空间的原点)是(in local space relative to an entity. If the position of that entity (origin of the local space) is)(P )((P))实体的旋转矩阵(局部空间的旋转矩阵)为(and the rotation matrix of the entity (rotation matrix of the local space) is)(U =\ begin {bmatrix} \ cos \ theta和-\ sin \ theta \ \ sin \ theta和\ cos \ theta \ end {bmatrix} )((U = \begin{bmatrix}\cos \theta & -\sin \theta \ \sin \theta & \cos \theta \end{bmatrix}))(哪里((where)(\ theta )((\theta))是实体的世界旋转),则可以进行以下转换.(is the world rotation of the entity), then the following transformations can take place.)
本地空间到世界空间(转换)(Local Space to World Space (Transform))
(X =P + UX')((X = P + UX')) 首先旋转局部空间点以将其带到世界空间旋转,然后平移到世界空间位置.(The local space point is first rotated to bring it to world space rotation, and then translated to the world space position.) 重要的是要注意,局部空间原点或旋转的变化会(It is important to note that a change in the local space origin or rotation will)**不(not)**影响局部空间位置.但是,世界空间的位置可能会改变.(affect a local space position. The world space position, however, may change.)
世界空间到局部空间(逆变换)(World Space to Local Space (Inverse Transform))
求解上述(X')方程,将为我们提供所需的世界空间到局部空间的转换:(Solving the above equation for (X') will give us the required world space to local space transformation:) (X'=U ^ {-1}(X-P))((X' = U^{-1}(X - P))) 这给我们带来了一个问题-如何找到逆(This gives us a problem - how to find the inverse)(U ^ {-1} )((U^{-1}))矩阵?原来我们的旋转矩阵是正交的,所以(of the matrix? Turns out that our rotation matrix is orthongonal, so)(U ^ {-1} =U ^ T )((U^{-1} = U^T)),在哪里(, where)(U ^ T )((U^T))是我们已经知道如何计算的矩阵转置.现在可以将等式重写为(is the transpose of matrix which we already know how to compute. The equation can now be rewritten as) (X'=U ^ T(X-P))((X' = U^T(X - P))) 空间变换将在整个引擎中使用,以简化数学例程.(Space transformations will be used throughout the engine to simplify mathematical routines.)
身体(Bodies)
实体或实体仅仅是世界中的对象.在我们的案例中,身体是可以与环境进行物理交互(或不取决于其配置)的对象.每个物体都有一些定义的特征或特性,例如位置,速度,扭矩,其形状和质量.(Bodies or entities are are simply objects in a world. In our case, a body is an object that can physically interact with the environment (or not, depending on its configuration). Each body has a few defining characteristics or properties, like position, velocity, torque, its shape, and mass.) 物理引擎中的物体本身并不能做什么.通常,它所负责的只是整合和更新其力量,速度和位置.它是物理引擎(或其他外部代码)来管理诸如碰撞之类的交互.通常会通过在物体上施加力来操纵它.但是,在极少数情况下(如脉冲冲突解决方案,我们将在后面介绍),速度将由物理引擎直接修改.(A body in a physics engine does not do much by itself. Generally, all it is responsible for is integrating and updating its forces, velocities, and positions. It is the physics engine - or other external code - that manages interactions like collisions. A body will typically be manipulated by applying forces on it. However, in rare cases (like impulse collision resolution, which we will cover later), the velocity will be modified directly by the physics engine.)
身体物理引擎的相互作用(Body-Physics Engine Interactions)
物理引擎与世界上物体的典型交互包括:(Typical interactions of a physics engine with the bodies in a world include:)
- 施加力(Application of forces)
- 碰撞检测与解决(Collision detection and resolution)
- 空间查询(射线投射和shapecast)(Spatial queries (raycasts and shapecasts))
- 关节和约束(Joints and constraints)
代码中的主体(A Body in Code)
一种(A) Body
Rust中的结构(取自我与本系列文章一起在Rust中构建的引擎中)如下所示:(structure in Rust (taken from the engine I am building in Rust alongside this article series) looks like the following:)
pub struct Body {
pub position: Vec2,
pub rotation: f32,
pub velocity: Vec2,
pub angular_vel: f32,
force: Vec2,
torque: f32,
pub mass: f32,
pub inertia: f32,
pub coeff_friction: f32,
pub coeff_restitution: f32,
pub shape: Shape,
}
这个(This) Body
但是,定义不完整.随着物理引擎的增长,将向其中添加更多属性.(definition, however, is not complete. As the physics engine grows, more properties will be added to it.)
历史(History)
2017年11月7日:初始发布(7 Nov 2017: Initial post)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
physics 2D mathematics 新闻 翻译