feat: added raycasting support for physics engine 2D

This commit is contained in:
2025-06-09 18:11:20 +03:00
parent adfa6c6ba0
commit 6a41407005
6 changed files with 226 additions and 2 deletions

View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
@@ -35,6 +37,26 @@ public interface IPhysicsEngine2D
/// <param name="deltaTime">The time step.</param>
void StepIndividual(IRigidBody2D rigidBody, float deltaTime);
/// <summary>
/// Casts a <see cref="Ray2D"/> into the scene and returns the closest <see cref="RaycastResult"/> it hits, if any.
/// </summary>
/// <param name="ray">The <see cref="Ray2D"/> to cast.</param>
/// <param name="length">The maximum distance to check for intersections. Defaults to <see cref="float.MaxValue"/>.</param>
/// <returns>
/// A <see cref="RaycastResult"/> containing information about the hit, or <see cref="null"/> if no hit was detected.
/// </returns>
RaycastResult? Raycast(Ray2D ray, float length = float.MaxValue);
/// <summary>
/// Casts a <see cref="Ray2D"/> into the scene and stores all hit results in the provided list.
/// </summary>
/// <param name="ray">The <see cref="Ray2D"/> to cast.</param>
/// <param name="results">
/// A list to which all <see cref="RaycastResult"/>s will be added. The list will be populated with zero or more <see cref="RaycastResult"/> objects.
/// </param>
/// <param name="length">The maximum distance to check for intersections. Defaults to <see cref="float.MaxValue"/>.</param>
void Raycast(Ray2D ray, IList<RaycastResult> results, float length = float.MaxValue);
readonly record struct PhysicsIterationArguments(IPhysicsEngine2D Sender, float IterationDeltaTime);
readonly record struct PhysicsStepArguments(IPhysicsEngine2D Sender, float StepDeltaTime);
}

View File

@@ -0,0 +1,23 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
/// <summary>
/// Represents a 2D raycast resolver.
/// </summary>
public interface IRaycastResolver2D
{
/// <summary>
/// Casts a <see cref="Ray2D"/> against a specific <see cref="ICollider2D"/> shape and returns the first hit, if any.
/// </summary>
/// <typeparam name="T">The type of the <see cref="ICollider2D"/>, which must implement <see cref="ICollider2D"/>.</typeparam>
/// <param name="shape">The <see cref="ICollider2D"/> shape to test against.</param>
/// <param name="ray">The <see cref="Ray2D"/> to cast.</param>
/// <param name="length">
/// The maximum distance to check along the <see cref="Ray2D"/>. Defaults to <see cref="float.MaxValue"/>.
/// </param>
/// <returns>
/// A <see cref="RaycastResult"/> containing information about the intersection, or <see cref="null"/> if there was no hit.
/// </returns>
RaycastResult? RaycastAgainst<T>(T shape, Ray2D ray, float length = float.MaxValue) where T : ICollider2D;
}

View File

@@ -0,0 +1,14 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
public readonly struct RaycastResult(Ray2D ray, ICollider2D collider2D, Vector2D position, Vector2D normal)
{
public readonly Ray2D Ray = ray;
public readonly Vector2D Position = position;
public readonly Vector2D Normal = normal;
public readonly ICollider2D Collider2D = collider2D;
public readonly RigidBody2D? RigidBody2D = collider2D.BehaviourController.GetBehaviourInParent<RigidBody2D>();
}