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

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