💿🐜 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.

88 lines
2.7 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 "snapping-system.hpp"
  20. #include "game/components/collision-component.hpp"
  21. #include "game/components/snap-component.hpp"
  22. #include "game/components/transform-component.hpp"
  23. #include "utility/fundamental-types.hpp"
  24. using namespace ecs;
  25. snapping_system::snapping_system(entt::registry& registry):
  26. entity_system(registry)
  27. {}
  28. void snapping_system::update(double t, double dt)
  29. {
  30. registry.view<transform_component, snap_component>().each(
  31. [&](auto entity, auto& snap_transform, auto& snap)
  32. {
  33. bool intersection = false;
  34. float a = std::numeric_limits<float>::infinity();
  35. float3 pick;
  36. ray<float> snap_ray = snap.ray;
  37. if (snap.relative)
  38. {
  39. snap_ray.origin += snap_transform.local.translation;
  40. snap_ray.direction = snap_transform.local.rotation * snap_ray.direction;
  41. }
  42. registry.view<transform_component, collision_component>().each(
  43. [&](auto entity, auto& collision_transform, auto& collision)
  44. {
  45. // Transform ray into local space of collision component
  46. math::transform<float> inverse_transform = math::inverse(collision_transform.local);
  47. float3 origin = inverse_transform * snap_ray.origin;
  48. float3 direction = math::normalize(math::conjugate(collision_transform.local.rotation) * snap_ray.direction);
  49. ray<float> transformed_ray = {origin, direction};
  50. // Broad phase AABB test
  51. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  52. if (!std::get<0>(aabb_result))
  53. {
  54. return;
  55. }
  56. // Narrow phase mesh test
  57. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  58. if (mesh_result)
  59. {
  60. intersection = true;
  61. if (mesh_result->t < a)
  62. {
  63. a = mesh_result->t;
  64. pick = snap_ray.extrapolate(a);
  65. }
  66. }
  67. });
  68. if (intersection)
  69. {
  70. snap_transform.local.translation = pick;
  71. snap_transform.warp = snap.warp;
  72. if (snap.autoremove)
  73. {
  74. registry.remove<snap_component>(entity);
  75. }
  76. }
  77. });
  78. }