feat: added raycasting support for physics engine 2D
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
23
Engine.Physics2D/Abstract/IRaycastResolver2D.cs
Normal file
23
Engine.Physics2D/Abstract/IRaycastResolver2D.cs
Normal 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;
|
||||
}
|
14
Engine.Physics2D/Abstract/RaycastResult.cs
Normal file
14
Engine.Physics2D/Abstract/RaycastResult.cs
Normal 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>();
|
||||
}
|
Reference in New Issue
Block a user