I’m more interested in developing AI NPC that can detect players, follow along with them, and prevent them from collision if they hit the wall. I heard that either cross product or dot product was used in this case but I got no idea how I can implement it. Besides that what other kind of cases where I need to use linear algebra in games?
给你一些关于他们详细介绍:
叉乘
碰撞检测:当检测两个三维物体的碰撞时,可以通过计算它们的包围盒的轴对齐包围盒(AABB)的面的法线向量,然后进行叉乘来确定是否有交集。叉乘的结果向量会指向两个AABB之间的空间,如果这个向量的长度小于或等于两个AABB的一半尺寸之和,那么可以认为两个物体相交。
力的计算:在物理模拟中,例如,计算两个互相作用的物体之间的力,可以利用叉乘来得到一个垂直于两个物体当前位置向量所在平面的力向量,这个力向量的大小等于两个物体之间的距离,方向则由右手定则确定。
旋转:在计算物体绕一个轴旋转时,可以使用叉乘来得到旋转轴的向量。例如,在Unity中,可以通过计算鼠标移动产生的向量与摄像机朝向向量的叉乘,来确定旋转的方向和速度。
点乘
方向判断:点乘常用于计算两个向量的夹角余弦值,这在确定两个物体之间的相对方向或者一个物体相对于另一个物体的朝向时非常有用。
投影计算:在3D渲染中,可以通过点乘来计算光线与表面的投影,这对于计算光照效果非常重要。
速度和加速度:在游戏物理中,点乘可以用来计算速度向量与加速度向量的关系,从而确定物体的加速方向和大小。
线性代数中矩阵的应用比较多
矩阵
变换:矩阵用于在3D空间中进行平移、旋转和缩放等变换。通过矩阵乘法,可以一次性地应用这些变换。
视图和投影矩阵:在游戏引擎中,摄像机的视图矩阵用于定义摄像机的位置和朝向,投影矩阵则用于定义渲染到屏幕上的视锥体。
动画:在角色动画中,通过变换矩阵可以计算骨骼和关节的当前位置,从而驱动角色的动作。
粒子系统:在粒子系统中,矩阵用于变换粒子的位置和速度,实现粒子运动、旋转等效果。
这些数据处理在蓝图中是有特定节点的,你可以查询文档
About AI. Well. You need constantly do traces line of sphere traces, then i suppose break hit result and check player is or wall and create path finding system (many of them pick what you like). But it’s need good optimization. You can look into the AI Perception Component file and see how detection is done in Unreal.
About using algebra. Many places in gameplay. Down and up camera when crouch - vectors. Movement - vectors. Line Traces while shoot - vectors. Example parkour - vectors. If you use animations and not root motion - you need to move you character through vectors. Find places where you need to spawn item if you drop it from inventory - vectors. It’s not THAT hard if you don’t make the wind that blows on the car according to the formulas from Gran Turismo game.
You need only little of lineal algebra. Not all algebra. Unreal already have many math functions, including dot product and cross product. Probles is - not many people on the Internet tell how linear algebra is used in real examples, and there are very few tutorials on a similar topic. So I can’t recommend anything other than book “3D Math Primer for Graphics and Game Development” by Dunn Fletcher (it’s free), and your own experiments, sorry. (I only know a few channels on youtube). I learned myself just by doing mechanics.