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

78 lines
2.5 KiB

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