feat(scripting): expose PhysX rigidbody and raycast APIs
This commit is contained in:
10
managed/XCEngine.ScriptCore/ForceMode.cs
Normal file
10
managed/XCEngine.ScriptCore/ForceMode.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace XCEngine
|
||||
{
|
||||
public enum ForceMode
|
||||
{
|
||||
Force = 0,
|
||||
Acceleration = 1,
|
||||
Impulse = 2,
|
||||
VelocityChange = 3
|
||||
}
|
||||
}
|
||||
@@ -311,6 +311,71 @@ namespace XCEngine
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void MeshRenderer_SetRenderLayer(ulong gameObjectUUID, int value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern int Rigidbody_GetBodyType(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetBodyType(ulong gameObjectUUID, int value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern float Rigidbody_GetMass(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetMass(ulong gameObjectUUID, float value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern float Rigidbody_GetLinearDamping(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetLinearDamping(ulong gameObjectUUID, float value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern float Rigidbody_GetAngularDamping(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetAngularDamping(ulong gameObjectUUID, float value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_GetLinearVelocity(ulong gameObjectUUID, out Vector3 velocity);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetLinearVelocity(ulong gameObjectUUID, ref Vector3 velocity);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_GetAngularVelocity(ulong gameObjectUUID, out Vector3 velocity);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetAngularVelocity(ulong gameObjectUUID, ref Vector3 velocity);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern bool Rigidbody_GetUseGravity(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetUseGravity(ulong gameObjectUUID, bool value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern bool Rigidbody_GetEnableCCD(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_SetEnableCCD(ulong gameObjectUUID, bool value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_AddForce(ulong gameObjectUUID, ref Vector3 force, int forceMode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rigidbody_ClearForces(ulong gameObjectUUID);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern bool Physics_Raycast(
|
||||
ref Vector3 origin,
|
||||
ref Vector3 direction,
|
||||
float maxDistance,
|
||||
out ulong hitGameObjectUUID,
|
||||
out Vector3 hitPoint,
|
||||
out Vector3 hitNormal,
|
||||
out float hitDistance,
|
||||
out int hitIsTrigger);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Rendering_SetRenderPipelineAssetType(Type assetType);
|
||||
|
||||
|
||||
60
managed/XCEngine.ScriptCore/Physics.cs
Normal file
60
managed/XCEngine.ScriptCore/Physics.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
namespace XCEngine
|
||||
{
|
||||
public static class Physics
|
||||
{
|
||||
public static bool Raycast(Vector3 origin, Vector3 direction)
|
||||
{
|
||||
RaycastHit hit;
|
||||
return Raycast(origin, direction, out hit, float.MaxValue);
|
||||
}
|
||||
|
||||
public static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance)
|
||||
{
|
||||
RaycastHit hit;
|
||||
return Raycast(origin, direction, out hit, maxDistance);
|
||||
}
|
||||
|
||||
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hit)
|
||||
{
|
||||
return Raycast(origin, direction, out hit, float.MaxValue);
|
||||
}
|
||||
|
||||
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hit, float maxDistance)
|
||||
{
|
||||
hit = default(RaycastHit);
|
||||
|
||||
if (float.IsNaN(maxDistance) || maxDistance <= 0.0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (float.IsPositiveInfinity(maxDistance))
|
||||
{
|
||||
maxDistance = float.MaxValue;
|
||||
}
|
||||
|
||||
bool hasHit = InternalCalls.Physics_Raycast(
|
||||
ref origin,
|
||||
ref direction,
|
||||
maxDistance,
|
||||
out ulong hitGameObjectUUID,
|
||||
out Vector3 hitPoint,
|
||||
out Vector3 hitNormal,
|
||||
out float hitDistance,
|
||||
out int hitIsTrigger);
|
||||
|
||||
if (!hasHit)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
hit = new RaycastHit(
|
||||
hitGameObjectUUID,
|
||||
hitPoint,
|
||||
hitNormal,
|
||||
hitDistance,
|
||||
hitIsTrigger != 0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
managed/XCEngine.ScriptCore/PhysicsBodyType.cs
Normal file
9
managed/XCEngine.ScriptCore/PhysicsBodyType.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace XCEngine
|
||||
{
|
||||
public enum PhysicsBodyType
|
||||
{
|
||||
Static = 0,
|
||||
Dynamic = 1,
|
||||
Kinematic = 2
|
||||
}
|
||||
}
|
||||
51
managed/XCEngine.ScriptCore/RaycastHit.cs
Normal file
51
managed/XCEngine.ScriptCore/RaycastHit.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
namespace XCEngine
|
||||
{
|
||||
public struct RaycastHit
|
||||
{
|
||||
private ulong m_gameObjectUUID;
|
||||
private Vector3 m_point;
|
||||
private Vector3 m_normal;
|
||||
private float m_distance;
|
||||
private bool m_isTrigger;
|
||||
|
||||
internal RaycastHit(
|
||||
ulong gameObjectUUID,
|
||||
Vector3 point,
|
||||
Vector3 normal,
|
||||
float distance,
|
||||
bool isTrigger)
|
||||
{
|
||||
m_gameObjectUUID = gameObjectUUID;
|
||||
m_point = point;
|
||||
m_normal = normal;
|
||||
m_distance = distance;
|
||||
m_isTrigger = isTrigger;
|
||||
}
|
||||
|
||||
public GameObject GameObject => m_gameObjectUUID != 0 ? new GameObject(m_gameObjectUUID) : null;
|
||||
public GameObject gameObject => GameObject;
|
||||
|
||||
public Transform Transform
|
||||
{
|
||||
get
|
||||
{
|
||||
GameObject hitObject = GameObject;
|
||||
return hitObject != null ? hitObject.Transform : null;
|
||||
}
|
||||
}
|
||||
|
||||
public Transform transform => Transform;
|
||||
|
||||
public Vector3 Point => m_point;
|
||||
public Vector3 point => Point;
|
||||
|
||||
public Vector3 Normal => m_normal;
|
||||
public Vector3 normal => Normal;
|
||||
|
||||
public float Distance => m_distance;
|
||||
public float distance => Distance;
|
||||
|
||||
public bool IsTrigger => m_isTrigger;
|
||||
public bool isTrigger => IsTrigger;
|
||||
}
|
||||
}
|
||||
129
managed/XCEngine.ScriptCore/Rigidbody.cs
Normal file
129
managed/XCEngine.ScriptCore/Rigidbody.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
namespace XCEngine
|
||||
{
|
||||
public sealed class Rigidbody : Component
|
||||
{
|
||||
internal Rigidbody(ulong gameObjectUUID)
|
||||
: base(gameObjectUUID)
|
||||
{
|
||||
}
|
||||
|
||||
public PhysicsBodyType BodyType
|
||||
{
|
||||
get => (PhysicsBodyType)InternalCalls.Rigidbody_GetBodyType(GameObjectUUID);
|
||||
set => InternalCalls.Rigidbody_SetBodyType(GameObjectUUID, (int)value);
|
||||
}
|
||||
|
||||
public PhysicsBodyType bodyType
|
||||
{
|
||||
get => BodyType;
|
||||
set => BodyType = value;
|
||||
}
|
||||
|
||||
public float Mass
|
||||
{
|
||||
get => InternalCalls.Rigidbody_GetMass(GameObjectUUID);
|
||||
set => InternalCalls.Rigidbody_SetMass(GameObjectUUID, value);
|
||||
}
|
||||
|
||||
public float mass
|
||||
{
|
||||
get => Mass;
|
||||
set => Mass = value;
|
||||
}
|
||||
|
||||
public float LinearDamping
|
||||
{
|
||||
get => InternalCalls.Rigidbody_GetLinearDamping(GameObjectUUID);
|
||||
set => InternalCalls.Rigidbody_SetLinearDamping(GameObjectUUID, value);
|
||||
}
|
||||
|
||||
public float linearDamping
|
||||
{
|
||||
get => LinearDamping;
|
||||
set => LinearDamping = value;
|
||||
}
|
||||
|
||||
public float AngularDamping
|
||||
{
|
||||
get => InternalCalls.Rigidbody_GetAngularDamping(GameObjectUUID);
|
||||
set => InternalCalls.Rigidbody_SetAngularDamping(GameObjectUUID, value);
|
||||
}
|
||||
|
||||
public float angularDamping
|
||||
{
|
||||
get => AngularDamping;
|
||||
set => AngularDamping = value;
|
||||
}
|
||||
|
||||
public Vector3 LinearVelocity
|
||||
{
|
||||
get
|
||||
{
|
||||
InternalCalls.Rigidbody_GetLinearVelocity(GameObjectUUID, out Vector3 velocity);
|
||||
return velocity;
|
||||
}
|
||||
set => InternalCalls.Rigidbody_SetLinearVelocity(GameObjectUUID, ref value);
|
||||
}
|
||||
|
||||
public Vector3 linearVelocity
|
||||
{
|
||||
get => LinearVelocity;
|
||||
set => LinearVelocity = value;
|
||||
}
|
||||
|
||||
public Vector3 AngularVelocity
|
||||
{
|
||||
get
|
||||
{
|
||||
InternalCalls.Rigidbody_GetAngularVelocity(GameObjectUUID, out Vector3 velocity);
|
||||
return velocity;
|
||||
}
|
||||
set => InternalCalls.Rigidbody_SetAngularVelocity(GameObjectUUID, ref value);
|
||||
}
|
||||
|
||||
public Vector3 angularVelocity
|
||||
{
|
||||
get => AngularVelocity;
|
||||
set => AngularVelocity = value;
|
||||
}
|
||||
|
||||
public bool UseGravity
|
||||
{
|
||||
get => InternalCalls.Rigidbody_GetUseGravity(GameObjectUUID);
|
||||
set => InternalCalls.Rigidbody_SetUseGravity(GameObjectUUID, value);
|
||||
}
|
||||
|
||||
public bool useGravity
|
||||
{
|
||||
get => UseGravity;
|
||||
set => UseGravity = value;
|
||||
}
|
||||
|
||||
public bool EnableCCD
|
||||
{
|
||||
get => InternalCalls.Rigidbody_GetEnableCCD(GameObjectUUID);
|
||||
set => InternalCalls.Rigidbody_SetEnableCCD(GameObjectUUID, value);
|
||||
}
|
||||
|
||||
public bool enableCCD
|
||||
{
|
||||
get => EnableCCD;
|
||||
set => EnableCCD = value;
|
||||
}
|
||||
|
||||
public void AddForce(Vector3 force)
|
||||
{
|
||||
AddForce(force, ForceMode.Force);
|
||||
}
|
||||
|
||||
public void AddForce(Vector3 force, ForceMode mode)
|
||||
{
|
||||
InternalCalls.Rigidbody_AddForce(GameObjectUUID, ref force, (int)mode);
|
||||
}
|
||||
|
||||
public void ClearForces()
|
||||
{
|
||||
InternalCalls.Rigidbody_ClearForces(GameObjectUUID);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user