add physX colliders ,add trigger, colliders now can visible, some rotation for cs and native cpp connection
This commit is contained in:
@ -11,10 +11,12 @@ namespace Prism
|
||||
{
|
||||
}
|
||||
|
||||
private List<Action<float>> m_Collision2DBeginCallbacks = new List<Action<float>>();
|
||||
private List<Action<float>> m_Collision2DEndCallbacks = new List<Action<float>>();
|
||||
private Action<float> m_CollisionBeginCallbacks;
|
||||
private Action<float> m_CollisionEndCallbacks;
|
||||
private Action<float>? m_CollisionBeginCallbacks;
|
||||
private Action<float>? m_CollisionEndCallbacks;
|
||||
private Action<float>? m_Collision2DBeginCallbacks;
|
||||
private Action<float>? m_Collision2DEndCallbacks;
|
||||
private Action<float>? m_TriggerBeginCallbacks;
|
||||
private Action<float>? m_TriggerEndCallbacks;
|
||||
|
||||
protected Entity() { ID = 0; }
|
||||
|
||||
@ -65,32 +67,14 @@ namespace Prism
|
||||
SetTransform_Native(ID, ref transform);
|
||||
}
|
||||
|
||||
public Vec3 GetForwardDirection()
|
||||
{
|
||||
GetForwardDirection_Native(ID, out Vec3 forward);
|
||||
return forward;
|
||||
}
|
||||
|
||||
public Vec3 GetRightDirection()
|
||||
{
|
||||
GetRightDirection_Native(ID, out Vec3 right);
|
||||
return right;
|
||||
}
|
||||
|
||||
public Vec3 GetUpDirection()
|
||||
{
|
||||
GetUpDirection_Native(ID, out Vec3 up);
|
||||
return up;
|
||||
}
|
||||
|
||||
public void AddCollision2DBeginCallback(Action<float> callback)
|
||||
{
|
||||
m_Collision2DBeginCallbacks.Add(callback);
|
||||
m_Collision2DBeginCallbacks += callback;
|
||||
}
|
||||
|
||||
public void AddCollision2DEndCallback(Action<float> callback)
|
||||
{
|
||||
m_Collision2DEndCallbacks.Add(callback);
|
||||
m_Collision2DEndCallbacks += callback;
|
||||
}
|
||||
|
||||
public void AddCollisionBeginCallback(Action<float> callback)
|
||||
@ -103,6 +87,16 @@ namespace Prism
|
||||
m_CollisionEndCallbacks += callback;
|
||||
}
|
||||
|
||||
public void AddTriggerBeginCallback(Action<float> callback)
|
||||
{
|
||||
m_TriggerBeginCallbacks += callback;
|
||||
}
|
||||
|
||||
public void AddTriggerEndCallback(Action<float> callback)
|
||||
{
|
||||
m_TriggerEndCallbacks += callback;
|
||||
}
|
||||
|
||||
private void OnCollisionBegin(float data)
|
||||
{
|
||||
if (m_CollisionBeginCallbacks != null)
|
||||
@ -117,16 +111,29 @@ namespace Prism
|
||||
|
||||
private void OnCollision2DBegin(float data)
|
||||
{
|
||||
foreach (var callback in m_Collision2DBeginCallbacks)
|
||||
callback.Invoke(data);
|
||||
if(m_Collision2DBeginCallbacks != null)
|
||||
m_Collision2DBeginCallbacks.Invoke(data);
|
||||
}
|
||||
|
||||
private void OnCollision2DEnd(float data)
|
||||
{
|
||||
foreach (var callback in m_Collision2DEndCallbacks)
|
||||
callback.Invoke(data);
|
||||
if(m_Collision2DEndCallbacks != null)
|
||||
m_Collision2DEndCallbacks.Invoke(data);
|
||||
}
|
||||
|
||||
private void OnTriggerBegin(float data)
|
||||
{
|
||||
if (m_TriggerBeginCallbacks != null)
|
||||
m_TriggerBeginCallbacks.Invoke(data);
|
||||
}
|
||||
|
||||
private void OnTriggerEnd(float data)
|
||||
{
|
||||
if (m_TriggerEndCallbacks != null)
|
||||
m_TriggerEndCallbacks.Invoke(data);
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern void CreateComponent_Native(ulong entityID, Type type);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
@ -137,12 +144,5 @@ namespace Prism
|
||||
private static extern void SetTransform_Native(ulong entityID, ref Mat4 matrix);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern ulong FindEntityByTag_Native(string tag);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern void GetForwardDirection_Native(ulong entityID, out Vec3 forward);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern void GetRightDirection_Native(ulong entityID, out Vec3 right);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern void GetUpDirection_Native(ulong entityID, out Vec3 up);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,14 +4,39 @@ namespace Prism
|
||||
{
|
||||
public class Input
|
||||
{
|
||||
public enum CursorMode
|
||||
{
|
||||
Normal = 0,
|
||||
Hidden = 1,
|
||||
Locked = 2,
|
||||
}
|
||||
|
||||
public static bool IsKeyPressed(KeyCode keycode)
|
||||
{
|
||||
return IsKeyPressed_Native(keycode);
|
||||
}
|
||||
|
||||
public static Vec2 GetMousePosition()
|
||||
{
|
||||
GetMousePosition_Native(out Vec2 position);
|
||||
return position;
|
||||
}
|
||||
|
||||
public static void SetCursorMode(CursorMode mode) => SetCursorMode_Native(mode);
|
||||
public static CursorMode GetCursorMode() => GetCursorMode_Native();
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern bool IsKeyPressed_Native(KeyCode keycode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern void GetMousePosition_Native(out Vec2 position);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern void SetCursorMode_Native(CursorMode mode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
private static extern CursorMode GetCursorMode_Native();
|
||||
}
|
||||
}
|
||||
56
Prism-ScriptCore/Src/Prism/Math/Queternion.cs
Normal file
56
Prism-ScriptCore/Src/Prism/Math/Queternion.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Prism
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Quaternion
|
||||
{
|
||||
public float X;
|
||||
public float Y;
|
||||
public float Z;
|
||||
public float W;
|
||||
|
||||
public Quaternion(float x, float y, float z, float w)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
W = w;
|
||||
}
|
||||
|
||||
public Quaternion(float w, Vec3 xyz)
|
||||
{
|
||||
X = xyz.X;
|
||||
Y = xyz.Y;
|
||||
Z = xyz.Z;
|
||||
W = w;
|
||||
}
|
||||
|
||||
public Quaternion(Vec3 eulerAngles)
|
||||
{
|
||||
Vec3 c = Vec3.Cos(eulerAngles * 0.5F);
|
||||
Vec3 s = Vec3.Sin(eulerAngles * 0.5F);
|
||||
|
||||
W = c.X * c.Y * c.Z + s.X * s.Y * s.Z;
|
||||
X = s.X * c.Y * c.Z - c.X * s.Y * s.Z;
|
||||
Y = c.X * s.Y * c.Z + s.X * c.Y * s.Z;
|
||||
Z = c.X * c.Y * s.Z - s.X * s.Y * c.Z;
|
||||
}
|
||||
|
||||
public static Quaternion AngleAxis(float angle, Vec3 axis)
|
||||
{
|
||||
float s = (float)Math.Sin(angle * 0.5F);
|
||||
return new Quaternion(s, axis * s);
|
||||
}
|
||||
|
||||
public static Quaternion operator*(Quaternion a, Quaternion b)
|
||||
{
|
||||
float w = a.W * b.W - a.X * b.X - a.Y * b.Y - a.Z * b.Z;
|
||||
float x = a.W * b.X + a.X * b.W + a.Y * b.Z - a.Z * b.Y;
|
||||
float y = a.W * b.Y + a.Y * b.W + a.Z * b.X - a.X * b.Z;
|
||||
float z = a.W * b.Z + a.Z * b.W + a.X * b.Y - a.Y * b.X;
|
||||
return new Quaternion(x, y, z, w);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,20 +25,23 @@ namespace Prism
|
||||
}
|
||||
|
||||
public void Clamp(Vec2 min, Vec2 max) {
|
||||
if (X < min.X)
|
||||
X = min.X;
|
||||
if (X > max.X)
|
||||
X = max.X;
|
||||
|
||||
if (Y < min.Y)
|
||||
Y = min.Y;
|
||||
if (Y > max.Y)
|
||||
Y = max.Y;
|
||||
X = Mathf.Clamp(X, min.X, max.X);
|
||||
Y = Mathf.Clamp(Y, min.Y, max.Y);
|
||||
}
|
||||
|
||||
public static Vec2 operator -(Vec2 l, Vec2 r)
|
||||
{
|
||||
return new Vec2(l.X - r.X, l.Y - r.Y);
|
||||
}
|
||||
|
||||
public static Vec2 operator -(Vec2 vector)
|
||||
{
|
||||
return new Vec2(-vector.X, -vector.Y);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"({X}, {Y})";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,12 @@ namespace Prism
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vec3
|
||||
{
|
||||
public static Vec3 Zero = new Vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
public static Vec3 Forward = new Vec3(0.0f, 0.0f, -1.0f);
|
||||
public static Vec3 Right = new Vec3(1.0f, 0.0f, 0.0f);
|
||||
public static Vec3 Up = new Vec3(0.0f, 1.0f, 0.0f);
|
||||
|
||||
public float X;
|
||||
public float Y;
|
||||
public float Z;
|
||||
@ -40,15 +46,6 @@ namespace Prism
|
||||
Z = Mathf.Clamp(Z, min.Z, max.Z);
|
||||
}
|
||||
|
||||
public static Vec3 operator *(Vec3 left, float scalar)
|
||||
{
|
||||
return new Vec3(left.X * scalar, left.Y * scalar, left.Z * scalar);
|
||||
}
|
||||
|
||||
public static Vec3 operator *(float scalar, Vec3 right)
|
||||
{
|
||||
return new Vec3(scalar * right.X, scalar * right.Y, scalar * right.Z);
|
||||
}
|
||||
|
||||
public Vec2 XY {
|
||||
get { return new Vec2(X, Y); }
|
||||
@ -66,5 +63,56 @@ namespace Prism
|
||||
set { Y = value.X; Z = value.Y; }
|
||||
}
|
||||
|
||||
|
||||
public static Vec3 operator *(Vec3 left, float scalar)
|
||||
{
|
||||
return new Vec3(left.X * scalar, left.Y * scalar, left.Z * scalar);
|
||||
}
|
||||
|
||||
public static Vec3 operator *(float scalar, Vec3 right)
|
||||
{
|
||||
return new Vec3(scalar * right.X, scalar * right.Y, scalar * right.Z);
|
||||
}
|
||||
|
||||
public static Vec3 operator +(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
|
||||
}
|
||||
|
||||
public static Vec3 operator -(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
|
||||
}
|
||||
|
||||
public static Vec3 operator /(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
|
||||
}
|
||||
|
||||
public static Vec3 operator /(Vec3 left, float scalar)
|
||||
{
|
||||
return new Vec3(left.X / scalar, left.Y / scalar, left.Z / scalar);
|
||||
}
|
||||
|
||||
public static Vec3 operator-(Vec3 vector)
|
||||
{
|
||||
return new Vec3(-vector.X, -vector.Y, -vector.Z);
|
||||
}
|
||||
|
||||
public static Vec3 Cos(Vec3 vector)
|
||||
{
|
||||
return new Vec3((float)Math.Cos(vector.X), (float)Math.Cos(vector.Y), (float)Math.Cos(vector.Z));
|
||||
}
|
||||
|
||||
public static Vec3 Sin(Vec3 vector)
|
||||
{
|
||||
return new Vec3((float)Math.Sin(vector.X), (float)Math.Sin(vector.Y), (float)Math.Sin(vector.Z));
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"({X}, {Y}, {Z})";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -2,13 +2,13 @@ using System.Runtime.InteropServices;
|
||||
|
||||
namespace Prism
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vec4
|
||||
{
|
||||
[FieldOffset(0)] public float X;
|
||||
[FieldOffset(4)] public float Y;
|
||||
[FieldOffset(8)] public float Z;
|
||||
[FieldOffset(12)] public float W;
|
||||
public float X;
|
||||
public float Y;
|
||||
public float Z;
|
||||
public float W;
|
||||
|
||||
public Vec4(float scalar)
|
||||
{
|
||||
|
||||
77
Prism-ScriptCore/Src/Prism/Physics/Colliders.cs
Normal file
77
Prism-ScriptCore/Src/Prism/Physics/Colliders.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System;
|
||||
|
||||
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 bool IsTrigger { get; protected set; }
|
||||
|
||||
}
|
||||
|
||||
public class BoxCollider : Collider
|
||||
{
|
||||
public Vec3 Size { get; private set; }
|
||||
public Vec3 Offset { get; private set; }
|
||||
|
||||
internal BoxCollider(ulong entityID, Vec3 size, Vec3 offset, bool isTrigger)
|
||||
{
|
||||
ColliderEntity = new Entity(entityID);
|
||||
Type = ColliderType.Box;
|
||||
Size = size;
|
||||
Offset = offset;
|
||||
IsTrigger = isTrigger;
|
||||
}
|
||||
}
|
||||
|
||||
public class SphereCollider : Collider
|
||||
{
|
||||
public float Radius { get; private set; }
|
||||
|
||||
internal SphereCollider(ulong entityID, float radius, bool isTrigger)
|
||||
{
|
||||
ColliderEntity = new Entity(entityID);
|
||||
Type = ColliderType.Box;
|
||||
Radius = radius;
|
||||
IsTrigger = isTrigger;
|
||||
}
|
||||
}
|
||||
|
||||
public class CapsuleCollider : Collider
|
||||
{
|
||||
public float Radius { get; private set; }
|
||||
public float Height { get; private set; }
|
||||
|
||||
internal CapsuleCollider(ulong entityID, float radius, float height, bool isTrigger)
|
||||
{
|
||||
ColliderEntity = new Entity(entityID);
|
||||
Type = ColliderType.Box;
|
||||
Radius = radius;
|
||||
Height = height;
|
||||
IsTrigger = isTrigger;
|
||||
}
|
||||
}
|
||||
|
||||
public class MeshCollider : Collider
|
||||
{
|
||||
public Mesh Mesh { get; private set; }
|
||||
|
||||
internal MeshCollider(ulong entityID, Mesh mesh, bool isTrigger)
|
||||
{
|
||||
ColliderEntity = new Entity(entityID);
|
||||
Type = ColliderType.Box;
|
||||
Mesh = mesh;
|
||||
IsTrigger = isTrigger;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,11 +46,62 @@ namespace Prism
|
||||
}
|
||||
}
|
||||
|
||||
public Vec3 Rotation
|
||||
{
|
||||
get
|
||||
{
|
||||
GetRotation_Native(Entity.ID, out Vec3 rotation);
|
||||
return rotation;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetRotation_Native(Entity.ID, ref value);
|
||||
}
|
||||
}
|
||||
|
||||
public Vec3 Forward
|
||||
{
|
||||
get
|
||||
{
|
||||
GetRelativeDirection_Native(Entity.ID, out Vec3 result, ref Vec3.Forward);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public Vec3 Right
|
||||
{
|
||||
get
|
||||
{
|
||||
GetRelativeDirection_Native(Entity.ID, out Vec3 result, ref Vec3.Right);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public Vec3 Up
|
||||
{
|
||||
get
|
||||
{
|
||||
GetRelativeDirection_Native(Entity.ID, out Vec3 result, ref Vec3.Up);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
public static extern void GetTransform_Native(ulong entityID, out Mat4 result);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
public static extern void SetTransform_Native(ulong entityID, ref Mat4 result);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
public static extern void GetRelativeDirection_Native(ulong entityID, out Vec3 result, ref Vec3 direction);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
public static extern void GetRotation_Native(ulong entityID, out Vec3 rotation);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
public static extern void SetRotation_Native(ulong entityID, ref Vec3 rotation);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -163,6 +214,12 @@ namespace Prism
|
||||
SetLinearVelocity_Native(Entity.ID, ref velocity);
|
||||
}
|
||||
|
||||
public void Rotate(Vec3 rotation)
|
||||
{
|
||||
Rotate_Native(Entity.ID, ref rotation);
|
||||
}
|
||||
|
||||
|
||||
// TODO: Add SetMaxLinearVelocity() as well
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
@ -174,5 +231,8 @@ namespace Prism
|
||||
internal static extern void GetLinearVelocity_Native(ulong entityID, out Vec3 velocity);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void SetLinearVelocity_Native(ulong entityID, ref Vec3 velocity);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rotate_Native(ulong entityID, ref Vec3 rotation);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user