/
EugenyCh7
/
Puppet2D.Net
Обзор
Документация
Войти
/
EugenyCh7
/
Puppet2D.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Puppet2D.Engine/Delegates.cs
185 строк
7 KB
EugenyCh7
+ World.CollideCapsule
25 авг 2025, 22:58
25 авг 2025, 22:58
63f951a
Код
Авторство
О чём код?
using Puppet2D.Engine.Common; using Puppet2D.Engine.Components.RigidBodies; using Puppet2D.Engine.Worlds; using Puppet2D.System; using System; using System.Runtime.InteropServices; namespace Puppet2D.Engine { #region Safe Delegates /// <summary> /// Optional friction mixing callback. /// This intentionally provides no context objects because this is called from a worker thread. /// Warning: /// This function should not attempt to modify Box2D state or user application state. /// </summary> public delegate float FrictionCallback(float frictionA, int userMaterialIdA, float frictionB, int userMaterialIdB); /// <summary> /// Optional restitution mixing callback. /// This intentionally provides no context objects because this is called from a worker thread. /// Warning: /// This function should not attempt to modify Box2D state or user application state. /// </summary> public delegate float RestitutionCallback(float restitutionA, int userMaterialIdA, float restitutionB, int userMaterialIdB); /// <summary> /// Prototype for a contact filter callback. /// This is called when a contact pair is considered for collision. /// This allows you to perform custom logic to prevent collision between shapes. /// This is only called if one of the two shapes has custom filtering enabled. /// </summary> public delegate bool CustomFilterFcn(RigidBody bodyA, RigidBody bodyB); /// <summary> /// Prototype for a pre-solve callback. /// This is called after a contact is updated. /// This allows you to inspect a contact before it goes to the solver.If you are careful, /// you can modify the contact manifold(e.g.modify the normal). /// Warning: /// Do not attempt to modify the world inside this callback. /// </summary> public delegate bool PreSolveFcn(RigidBody bodyA, RigidBody bodyB, Manifold manifold); /// <summary> /// Prototype callback for overlap queries. /// Called for each shape found in the query. /// </summary> public delegate bool OverlapResultFcn(RigidBody body); /// <summary> /// Prototype callback for ray casts. /// Called for each shape found in the query. /// You control how the ray cast proceeds by returning a float: /// return -1: ignore this shape and continue /// return 0: terminate the ray cast return fraction: clip the ray to this point /// return 1: don't clip the ray and continue /// </summary> /// <param name="body">The body hit by the ray</param> /// <param name="point">The point of initial intersection</param> /// <param name="normal">The normal vector at the point of intersection</param> /// <param name="fraction">The fraction along the ray at the point of intersection</param> /// <returns>-1 to filter, 0 to terminate, fraction to clip the ray for closest hit, 1 to continue</returns> public delegate float CastResultFcn(RigidBody body, Vector2 point, Vector2 normal, float fraction); /// <summary> /// Used to collect collision planes for character movers. /// Return true to continue gathering planes. /// </summary> public delegate bool PlaneResultFcn(RigidBody body, PlaneResult plane); #endregion Safe Delegates #region Unsafe Delegates internal unsafe delegate float UnsafeFrictionCallback(float frictionA, int userMaterialIdA, float frictionB, int userMaterialIdB); internal unsafe delegate float UnsafeRestitutionCallback(float restitutionA, int userMaterialIdA, float restitutionB, int userMaterialIdB); [return: MarshalAs(UnmanagedType.U1)] internal unsafe delegate bool UnsafeCustomFilterFcn(B2.ShapeId shapeIdA, B2.ShapeId shapeIdB, void* context); [return: MarshalAs(UnmanagedType.U1)] internal unsafe delegate bool UnsafePreSolveFcn(B2.ShapeId shapeIdA, B2.ShapeId shapeIdB, B2.Manifold* manifold, void* context); [return: MarshalAs(UnmanagedType.U1)] internal unsafe delegate bool UnsafeOverlapResultFcn(B2.ShapeId shapeId, void* context); internal unsafe delegate float UnsafeCastResultFcn(B2.ShapeId shapeId, Vector2 point, Vector2 normal, float fraction, void* context); [return: MarshalAs(UnmanagedType.U1)] internal unsafe delegate bool UnsafePlaneResultFcn(B2.ShapeId shapeId, B2.PlaneResult* plane, void* context); #endregion Unsafe Delegates internal static unsafe class Delegates { internal static nint GetPointer<TDelegate>(TDelegate d) where TDelegate : Delegate { return d != null ? Marshal.GetFunctionPointerForDelegate(d) : nint.Zero; } internal static TDelegate FromPointer<TDelegate>(nint ptr) where TDelegate : Delegate { return ptr != nint.Zero ? Marshal.GetDelegateForFunctionPointer<TDelegate>(ptr) : (TDelegate)(object)null; } internal static UnsafeFrictionCallback AsUnsafeFrictionCallback(FrictionCallback frictionCallback) { if (frictionCallback is null) return null; return frictionCallback.Invoke; } internal static UnsafeRestitutionCallback AsUnsafeRestitutionCallback(RestitutionCallback restitutionCallback) { if (restitutionCallback is null) return null; return restitutionCallback.Invoke; } internal static UnsafeCustomFilterFcn AsUnsafeCustomFilterFcn(CustomFilterFcn customFilterFcn, World world) { if (customFilterFcn is null) return null; return (shapeIdA, shapeIdB, _) => { return customFilterFcn.Invoke(world.GetRigidBody(shapeIdA), world.GetRigidBody(shapeIdB)); }; } internal static UnsafePreSolveFcn AsUnsafePreSolveFcn(PreSolveFcn preSolveFcn, World world) { if (preSolveFcn is null) return null; return (shapeIdA, shapeIdB, manifold, _) => { return preSolveFcn.Invoke( world.GetRigidBody(shapeIdA), world.GetRigidBody(shapeIdB), new Manifold(*manifold)); }; } internal static UnsafeOverlapResultFcn AsUnsafeOverlapResultFcn(OverlapResultFcn overlapResultFcn, World world) { if (overlapResultFcn is null) return null; return (shapeId, _) => { return overlapResultFcn.Invoke(world.GetRigidBody(shapeId)); }; } internal static UnsafeCastResultFcn AsUnsafeCastResultFcn(CastResultFcn castResultFcn, World world) { if (castResultFcn is null) return null; return (shapeId, point, normal, fraction, _) => { return castResultFcn.Invoke( world.GetRigidBody(shapeId), point, normal, fraction); }; } internal static UnsafePlaneResultFcn AnUnsafePlaneResultFcn(PlaneResultFcn planeResultFcn, World world) { if (planeResultFcn is null) return null; return (shapeId, plane, _) => { PlaneResult planeResult = new PlaneResult(*plane); return planeResultFcn.Invoke( world.GetRigidBody(shapeId), planeResult); }; } } }