💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.8 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "constraint.hpp"
  20. #include "entity/components/constraint-stack.hpp"
  21. #include "entity/components/constraints/constraints.hpp"
  22. #include "entity/components/transform.hpp"
  23. namespace entity {
  24. namespace system {
  25. constraint::constraint(entity::registry& registry):
  26. updatable(registry)
  27. {}
  28. void constraint::update(double t, double dt)
  29. {
  30. // For each entity with transform and constraint stack components
  31. registry.view<component::transform, component::constraint_stack>().each(
  32. [&](entity::id transform_eid, auto& transform, auto& stack)
  33. {
  34. // Get entity ID of first constraint
  35. entity::id constraint_eid = stack.head;
  36. // Consecutively apply constraints
  37. while (constraint_eid != entt::null)
  38. {
  39. // Invalid constraint
  40. if (!registry.has<component::constraint_stack_node>(constraint_eid))
  41. break;
  42. // Get constraint stack node of this constraint
  43. const auto& node = registry.get<component::constraint_stack_node>(constraint_eid);
  44. // Apply constraint if enabled
  45. if (node.active)
  46. handle_constraint(transform, constraint_eid, static_cast<float>(dt));
  47. // Get entity ID of next constraint
  48. constraint_eid = node.next;
  49. }
  50. });
  51. }
  52. void constraint::handle_constraint(component::transform& transform, entity::id constraint_eid, float dt)
  53. {
  54. if (registry.has<component::constraint::copy_translation>(constraint_eid))
  55. handle_copy_translation_constraint(transform, constraint_eid);
  56. else if (registry.has<component::constraint::copy_rotation>(constraint_eid))
  57. handle_copy_rotation_constraint(transform, constraint_eid);
  58. else if (registry.has<component::constraint::copy_scale>(constraint_eid))
  59. handle_copy_scale_constraint(transform, constraint_eid);
  60. else if (registry.has<component::constraint::copy_transform>(constraint_eid))
  61. handle_copy_transform_constraint(transform, constraint_eid);
  62. else if (registry.has<component::constraint::track_to>(constraint_eid))
  63. handle_track_to_constraint(transform, constraint_eid);
  64. else if (registry.has<component::constraint::three_dof>(constraint_eid))
  65. handle_three_dof_constraint(transform, constraint_eid);
  66. else if (registry.has<component::constraint::spring_to>(constraint_eid))
  67. handle_spring_to_constraint(transform, constraint_eid, dt);
  68. }
  69. void constraint::handle_copy_translation_constraint(component::transform& transform, entity::id constraint_eid)
  70. {
  71. const auto& params = registry.get<component::constraint::copy_translation>(constraint_eid);
  72. if (this->registry.has<component::transform>(params.target))
  73. {
  74. const auto& target_translation = registry.get<component::transform>(params.target).world.translation;
  75. if (params.offset)
  76. {
  77. if (params.copy_x)
  78. transform.world.translation.x += (params.invert_x) ? -target_translation.x : target_translation.x;
  79. if (params.copy_y)
  80. transform.world.translation.y += (params.invert_y) ? -target_translation.y : target_translation.y;
  81. if (params.copy_z)
  82. transform.world.translation.z += (params.invert_z) ? -target_translation.z : target_translation.z;
  83. }
  84. else
  85. {
  86. if (params.copy_x)
  87. transform.world.translation.x = (params.invert_x) ? -target_translation.x : target_translation.x;
  88. if (params.copy_y)
  89. transform.world.translation.y = (params.invert_y) ? -target_translation.y : target_translation.y;
  90. if (params.copy_z)
  91. transform.world.translation.z = (params.invert_z) ? -target_translation.z : target_translation.z;
  92. }
  93. }
  94. }
  95. void constraint::handle_copy_rotation_constraint(component::transform& transform, entity::id constraint_eid)
  96. {
  97. const auto& params = registry.get<component::constraint::copy_rotation>(constraint_eid);
  98. if (this->registry.has<component::transform>(params.target))
  99. {
  100. const auto& target_rotation = registry.get<component::transform>(params.target).world.rotation;
  101. transform.world.rotation = target_rotation;
  102. }
  103. }
  104. void constraint::handle_copy_scale_constraint(component::transform& transform, entity::id constraint_eid)
  105. {
  106. const auto& params = registry.get<component::constraint::copy_scale>(constraint_eid);
  107. if (this->registry.has<component::transform>(params.target))
  108. {
  109. const auto& target_scale = registry.get<component::transform>(params.target).world.scale;
  110. if (params.copy_x)
  111. transform.world.scale.x = target_scale.x;
  112. if (params.copy_y)
  113. transform.world.scale.y = target_scale.y;
  114. if (params.copy_z)
  115. transform.world.scale.z = target_scale.z;
  116. }
  117. }
  118. void constraint::handle_copy_transform_constraint(component::transform& transform, entity::id constraint_eid)
  119. {
  120. const auto& params = registry.get<component::constraint::copy_transform>(constraint_eid);
  121. if (this->registry.has<component::transform>(params.target))
  122. {
  123. const auto& target_transform = registry.get<component::transform>(params.target).world;
  124. transform.world = target_transform;
  125. }
  126. }
  127. void constraint::handle_track_to_constraint(component::transform& transform, entity::id constraint_eid)
  128. {
  129. const auto& params = registry.get<component::constraint::track_to>(constraint_eid);
  130. if (this->registry.has<component::transform>(params.target))
  131. {
  132. const auto& target_transform = registry.get<component::transform>(params.target).world;
  133. transform.world.rotation = math::look_rotation(math::normalize(math::sub(target_transform.translation, transform.world.translation)), params.up);
  134. }
  135. }
  136. void constraint::handle_three_dof_constraint(component::transform& transform, entity::id constraint_eid)
  137. {
  138. const auto& params = registry.get<component::constraint::three_dof>(constraint_eid);
  139. const math::quaternion<float> yaw = math::angle_axis(params.yaw, {0.0f, 1.0f, 0.0f});
  140. const math::quaternion<float> pitch = math::angle_axis(params.pitch, {-1.0f, 0.0f, 0.0f});
  141. const math::quaternion<float> roll = math::angle_axis(params.roll, {0.0f, 0.0f, -1.0f});
  142. transform.local.rotation = math::normalize(yaw * pitch * roll);
  143. }
  144. void constraint::handle_spring_to_constraint(component::transform& transform, entity::id constraint_eid, float dt)
  145. {
  146. auto& params = registry.get<component::constraint::spring_to>(constraint_eid);
  147. if (this->registry.has<component::transform>(params.target))
  148. {
  149. // Get transform of target entity
  150. const auto& target_transform = registry.get<component::transform>(params.target).world;
  151. // Spring translation
  152. if (params.spring_translation)
  153. {
  154. // Update translation spring target
  155. params.translation.x1 = target_transform.translation;
  156. // Solve translation spring
  157. solve_numeric_spring<float3, float>(params.translation, dt);
  158. // Update transform translation
  159. transform.world.translation = params.translation.x0;
  160. }
  161. // Spring rotation
  162. if (params.spring_rotation)
  163. {
  164. // Update rotation spring target
  165. params.rotation.x1 = {target_transform.rotation.w, target_transform.rotation.x, target_transform.rotation.y, target_transform.rotation.z};
  166. // Solve rotation spring
  167. solve_numeric_spring<float4, float>(params.rotation, dt);
  168. // Update transform rotation
  169. transform.world.rotation = math::normalize(math::quaternion<float>{params.rotation.x0[0], params.rotation.x0[1], params.rotation.x0[2], params.rotation.x0[3]});
  170. }
  171. }
  172. }
  173. } // namespace system
  174. } // namespace entity