yangruihan / blog

个人博客|Personal Blog
17 stars 2 forks source link

[Unity] CharacterController 源码学习 #22

Open yangruihan opened 3 years ago

yangruihan commented 3 years ago

Move 分析

核心逻辑

touching = m_Controller->move((const PxVec3&)movement, m_MinMoveDistance, elapsedTime, filters);

可以找到m_Controller的类型是physx::PxCapsuleController*,真正执行移动的逻辑是 PhysX 提供的

于是取得 PhysX 源码:https://github.com/NVIDIAGameWorks/PhysX-3.4

主要移动逻辑位于 PhysX_3.4\Source\PhysXCharacterKinematic\src\CctCharacterController.cpp

调用堆栈:CapsuleController::move->Controller::move->SweepTest::moveCharacter

查看moveCharacter源码可以发现大体分为一下几个阶段:

  1. 移动向量分解(Up、Side、Down) Ps::decomposeVector(normal_compo, tangent_compo, direction, upDirection); (PhysX_3.4\Source\PhysXCharacterKinematic\src\CctCharacterController.cpp 1706)
  2. 计算碰撞检测盒 volume.computeTemporalBox(*this, temporalBox, volume.mCenter, direction); (PhysX_3.4\Source\PhysXCharacterKinematic\src\CctCharacterController.cpp 1742)
  3. 收集接触到的几何体 updateTouchedGeoms(userData, userObstacles, temporalBox, filters, SideVector); (PhysX_3.4\Source\PhysXCharacterKinematic\src\CctCharacterController.cpp 1745)
  4. 三个向量三个Pass(Up、Side、Down),计算移动最终位置 (PhysX_3.4\Source\PhysXCharacterKinematic\src\CctCharacterController.cpp 1748 ~ 1956)