add overlapBox/Sphere/Capsule function, fixed OnWake/OnSleep not work

This commit is contained in:
2025-12-28 22:40:48 +08:00
parent d0eed3a33d
commit 9a44dd8d79
34 changed files with 1439 additions and 280 deletions

View File

@ -55,6 +55,12 @@ namespace Prism
return new Entity(entityID);
}
public Entity FindEntityByID(ulong entityID)
{
// TODO: to verify it
return new Entity(entityID);
}
public Mat4 GetTransform()
{
Mat4 mat4Instance;

View File

@ -10,12 +10,31 @@ namespace Prism
Hidden = 1,
Locked = 2,
}
public enum MouseButton
{
Button0 = 0,
Button1 = 1,
Button2 = 2,
Button3 = 3,
Button4 = 4,
Button5 = 5,
Left = Button0,
Right = Button1,
Middle = Button2
}
public static bool IsKeyPressed(KeyCode keycode)
{
return IsKeyPressed_Native(keycode);
}
public static bool IsMouseButtonPressed(MouseButton button)
{
return IsMouseButtonPressed_Native(button);
}
public static Vec2 GetMousePosition()
{
GetMousePosition_Native(out Vec2 position);
@ -30,6 +49,9 @@ namespace Prism
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsKeyPressed_Native(KeyCode keycode);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsMouseButtonPressed_Native(MouseButton button);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void GetMousePosition_Native(out Vec2 position);

View File

@ -46,6 +46,25 @@ namespace Prism
Z = Mathf.Clamp(Z, min.Z, max.Z);
}
public float Length()
{
return (float)Math.Sqrt(X * X + Y * Y + Z * Z);
}
public Vec3 Normalized()
{
float length = Length();
return new Vec3(X / length, Y / length, Z / length);
}
public void Normalize()
{
float length = Length();
X = X / length;
Y = Y / length;
Z = Z / length;
}
public Vec2 XY {
get { return new Vec2(X, Y); }
@ -79,6 +98,11 @@ namespace Prism
return new Vec3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}
public static Vec3 operator +(Vec3 left, float right)
{
return new Vec3(left.X + right, left.Y + right, left.Z + right);
}
public static Vec3 operator -(Vec3 left, Vec3 right)
{
return new Vec3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);

View File

@ -4,29 +4,55 @@ namespace Prism
{
public class Collider
{
public enum ColliderType
{
Box,
Sphere,
Capsule,
Mesh
}
public Entity ColliderEntity { get; protected set; }
public ColliderType Type { get; protected set; }
public ulong EntityID { get; protected set; }
public bool IsTrigger { get; protected set; }
private Entity entity;
private RigidBodyComponent _rigidBodyComponent;
public Entity Entity
{
get
{
if (entity == null)
entity = new Entity(EntityID);
return entity;
}
}
public RigidBodyComponent RigidBody
{
get
{
if (_rigidBodyComponent == null)
_rigidBodyComponent = Entity.GetComponent<RigidBodyComponent>();
return _rigidBodyComponent;
}
}
public override string ToString()
{
string type = "Collider";
if (this is BoxCollider) type = "BoxCollider";
else if (this is SphereCollider) type = "SphereCollider";
else if (this is CapsuleCollider) type = "CapsuleCollider";
else if (this is MeshCollider) type = "MeshCollider";
return "Collider(" + type + ", " + EntityID + ", " + IsTrigger + ")";
}
}
public class BoxCollider : Collider
{
public Vec3 Size { get; private set; }
public Vec3 Offset { get; private set; }
public Vec3 Size { get; protected set; }
public Vec3 Offset { get; protected set; }
internal BoxCollider(ulong entityID, Vec3 size, Vec3 offset, bool isTrigger)
private BoxCollider(ulong entityID, bool isTrigger, Vec3 size, Vec3 offset)
{
ColliderEntity = new Entity(entityID);
Type = ColliderType.Box;
EntityID = entityID;
Size = size;
Offset = offset;
IsTrigger = isTrigger;
@ -35,12 +61,11 @@ namespace Prism
public class SphereCollider : Collider
{
public float Radius { get; private set; }
public float Radius { get; protected set; }
internal SphereCollider(ulong entityID, float radius, bool isTrigger)
private SphereCollider(ulong entityID, bool isTrigger, float radius)
{
ColliderEntity = new Entity(entityID);
Type = ColliderType.Box;
EntityID = entityID;
Radius = radius;
IsTrigger = isTrigger;
}
@ -48,13 +73,12 @@ namespace Prism
public class CapsuleCollider : Collider
{
public float Radius { get; private set; }
public float Height { get; private set; }
public float Radius { get; protected set; }
public float Height { get; protected set; }
internal CapsuleCollider(ulong entityID, float radius, float height, bool isTrigger)
private CapsuleCollider(ulong entityID, bool isTrigger, float radius, float height)
{
ColliderEntity = new Entity(entityID);
Type = ColliderType.Box;
EntityID = entityID;
Radius = radius;
Height = height;
IsTrigger = isTrigger;
@ -63,13 +87,12 @@ namespace Prism
public class MeshCollider : Collider
{
public Mesh Mesh { get; private set; }
public Mesh Mesh { get; protected set; }
internal MeshCollider(ulong entityID, Mesh mesh, bool isTrigger)
private MeshCollider(ulong entityID, bool isTrigger, IntPtr filepath)
{
ColliderEntity = new Entity(entityID);
Type = ColliderType.Box;
Mesh = mesh;
EntityID = entityID;
Mesh = new Mesh(filepath);
IsTrigger = isTrigger;
}
}

View File

@ -0,0 +1,74 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Prism
{
[StructLayout(LayoutKind.Sequential)]
public struct RaycastHit
{
public ulong EntityID { get; private set; }
public Vec3 Position { get; private set; }
public Vec3 Normal { get; private set; }
public float Distance { get; private set; }
}
public static class Physics
{
public static bool Raycast(Vec3 origin, Vec3 direction, float maxDistance, out RaycastHit hit)
{
return Raycast_Native(ref origin, ref direction, maxDistance, out hit);
}
public static Collider[] OverlapBox(Vec3 origin, Vec3 halfSize)
{
return OverlapBox_Native(ref origin, ref halfSize);
}
public static Collider[] OverlapCapsule(Vec3 origin, float radius, float halfHeight)
{
return OverlapCapsule_Native(ref origin, radius, halfHeight);
}
public static Collider[] OverlapSphere(Vec3 origin, float radius)
{
return OverlapSphere_Native(ref origin, radius);
}
public static int OverlapBoxNonAlloc(Vec3 origin, Vec3 halfSize, Collider[] colliders)
{
return OverlapBoxNonAlloc_Native(ref origin, ref halfSize, colliders);
}
public static int OverlapCapsuleNonAlloc(Vec3 origin, float radius, float halfHeight, Collider[] colliders)
{
return OverlapCapsuleNonAlloc_Native(ref origin, radius, halfHeight, colliders);
}
public static int OverlapSphereNonAlloc(Vec3 origin, float radius, Collider[] colliders)
{
return OverlapSphereNonAlloc_Native(ref origin, radius, colliders);
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Raycast_Native(ref Vec3 origin, ref Vec3 direction, float maxDistance, out RaycastHit hit);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Collider[] OverlapBox_Native(ref Vec3 origin, ref Vec3 halfSize);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Collider[] OverlapCapsule_Native(ref Vec3 origin, float radius, float halfHeight);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Collider[] OverlapSphere_Native(ref Vec3 origin, float radius);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int OverlapBoxNonAlloc_Native(ref Vec3 origin, ref Vec3 halfSize, Collider[] colliders);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int OverlapCapsuleNonAlloc_Native(ref Vec3 origin, float radius, float halfHeight, Collider[] colliders);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int OverlapSphereNonAlloc_Native(ref Vec3 origin, float radius, Collider[] colliders);
}
}

View File

@ -192,6 +192,18 @@ namespace Prism
VelocityChange,
Acceleration
}
public float Mass
{
get { return GetMass_Native(Entity.ID); }
set { SetMass_Native(Entity.ID, value); }
}
public uint Layer
{
get { return GetLayer_Native(Entity.ID); }
}
public void AddForce(Vec3 force, ForceMode forceMode = ForceMode.Force)
{
@ -234,5 +246,14 @@ namespace Prism
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rotate_Native(ulong entityID, ref Vec3 rotation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern uint GetLayer_Native(ulong entityID);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern float GetMass_Native(ulong entityID);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern float SetMass_Native(ulong entityID, float mass);
}
}