PuppetPhysics.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include <cstdint>
  3. #include <string>
  4. #include <glm/glm.hpp>
  5. namespace WallpaperEngine::Render::Objects {
  6. /**
  7. * Jiggle physics of one puppet bone, read from the JSON after its MDLS record the way wallpaper64.exe 2.8.42
  8. * does (sub_140265C30). Angles are radians, stiffness/friction per second, rest target in bone space.
  9. */
  10. struct PuppetBonePhysics {
  11. enum Flags : uint32_t {
  12. Simulate = 0x1,
  13. SimulateRigid = 0x2,
  14. Gravity = 0x4,
  15. AngleLimits = 0x8,
  16. TotalAngleLimit = 0x10,
  17. Rotation = 0x1000,
  18. Translation = 0x2000,
  19. InverseKinematics = 0x4000,
  20. LockRotationX = 0x100000,
  21. LockRotationY = 0x200000,
  22. LockRotationZ = 0x400000,
  23. LockTranslationX = 0x800000,
  24. LockTranslationY = 0x1000000,
  25. LockTranslationZ = 0x2000000,
  26. };
  27. uint32_t flags = 0;
  28. float rotationStiffness = 0.0f;
  29. float translationStiffness = 0.0f;
  30. float rotationFriction = 0.0f;
  31. float translationFriction = 0.0f;
  32. // stored as 1 - inertia / 100 like WE
  33. float rotationInertia = 1.0f;
  34. float translationInertia = 1.0f;
  35. glm::vec3 gravityDirection { 0.0f };
  36. float mass = 1000.0f;
  37. glm::vec3 target { 0.0f };
  38. float maxTranslation = 0.0f;
  39. glm::vec3 angleMin { 0.0f };
  40. glm::vec3 angleMax { 0.0f };
  41. float maxAngle = 0.0f;
  42. [[nodiscard]] bool simulated () const { return (flags & (Rotation | Translation)) != 0; }
  43. static PuppetBonePhysics parse (const std::string& json);
  44. };
  45. struct PuppetBonePhysicsState {
  46. // w, x, y, z
  47. glm::vec4 angularVelocity { 1.0f, 0.0f, 0.0f, 0.0f };
  48. glm::vec3 angles { 0.0f };
  49. glm::vec3 offset { 0.0f };
  50. glm::vec3 velocity { 0.0f };
  51. };
  52. /**
  53. * One frame of a bone's physics (sub_1401FDF90). `world` is the bone's scene transform this frame before its own
  54. * physics, `previousWorld` last frame's after it, `objectScale` the average axis scale of the object. Returns the
  55. * matrix to apply in front of the bone's model-space transform (model * result).
  56. */
  57. glm::mat4 stepPuppetBonePhysics (
  58. const PuppetBonePhysics& physics, PuppetBonePhysicsState& state, const glm::mat4& world,
  59. const glm::mat4& previousWorld, float dt, float objectScale
  60. );
  61. /** Quaternion (w, x, y, z) helpers shared with the IK solver: sub_140216070, sub_1402167C0, sub_140216280 */
  62. glm::vec4 puppetSlerp (const glm::vec4& a, const glm::vec4& b, float t);
  63. glm::vec4 puppetRotationArc (const glm::vec3& from, const glm::vec3& to);
  64. glm::mat3 puppetQuatRows (const glm::vec4& q);
  65. /**
  66. * Script `applyBonePhysicsImpulse` (sub_140210990): the directional impulse is added to the translation velocity,
  67. * the angular one (degrees) is turned into a quaternion and multiplied onto the angular velocity.
  68. */
  69. void applyPuppetBoneImpulse (
  70. PuppetBonePhysicsState& state, const glm::vec3& directional, const glm::vec3& angularDegrees
  71. );
  72. } // namespace WallpaperEngine::Render::Objects