add physX colliders ,add trigger, colliders now can visible, some rotation for cs and native cpp connection

This commit is contained in:
2025-12-24 10:10:24 +08:00
parent 00d3993a77
commit f747db4e27
44 changed files with 2322 additions and 860 deletions

115
ExampleApp/Src/FPSPlayer.cs Normal file
View File

@ -0,0 +1,115 @@
using System;
using Prism;
namespace FPSExample
{
public class FPSPlayer : Entity
{
public float WalkingSpeed = 10.0F;
public float RunSpeed = 20.0F;
public float JumpForce = 50.0F;
public float MouseSensitivity = 10.0F;
public Vec3 Forward = new Vec3(0.0f, 0.0f, 1.0f);
private bool m_Colliding = false;
private float m_CurrentSpeed;
private RigidBodyComponent m_RigidBody;
private TransformComponent m_Transform;
private TransformComponent m_CameraTransform;
private Entity m_CameraEntity;
private Vec2 m_LastMousePosition;
private float m_CameraRotationX = 0.0f;
private float m_RotationY = 0.0f;
void OnCreate()
{
m_Transform = GetComponent<TransformComponent>();
m_RigidBody = GetComponent<RigidBodyComponent>();
m_CurrentSpeed = WalkingSpeed;
AddCollisionBeginCallback((n) => { m_Colliding = true; });
AddCollisionEndCallback((n) => { m_Colliding = false; });
m_CameraEntity = FindEntityByTag("Camera");
m_CameraTransform = m_CameraEntity.GetComponent<TransformComponent>();
m_LastMousePosition = Input.GetMousePosition();
Input.SetCursorMode(Input.CursorMode.Locked);
}
void OnUpdate(float ts)
{
Forward = m_Transform.Forward;
if (Input.IsKeyPressed(KeyCode.Escape) && Input.GetCursorMode() == Input.CursorMode.Locked)
{
Input.SetCursorMode(Input.CursorMode.Normal);
}
m_CurrentSpeed = Input.IsKeyPressed(KeyCode.LeftControl) ? RunSpeed : WalkingSpeed;
UpdateRotation(ts);
UpdateMovement();
UpdateCameraTransform();
}
private void UpdateRotation(float ts)
{
Vec2 currentMousePosition = Input.GetMousePosition();
Vec2 delta = m_LastMousePosition - currentMousePosition;
if (delta.X != 0)
{
m_RotationY += delta.X * MouseSensitivity * ts;
}
m_RigidBody.Rotate(new Vec3(0.0F, delta.X * MouseSensitivity, 0.0f) * ts);
if (delta.Y != 0.0F)
{
m_CameraRotationX += delta.Y * MouseSensitivity * ts;
m_CameraRotationX = Mathf.Clamp(m_CameraRotationX, -80.0f, 80.0f);
}
m_LastMousePosition = currentMousePosition;
}
private void UpdateMovement()
{
if (Input.IsKeyPressed(KeyCode.W))
m_RigidBody.AddForce(m_Transform.Forward * m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.S))
m_RigidBody.AddForce(m_Transform.Forward * -m_CurrentSpeed);
if (Input.IsKeyPressed(KeyCode.A))
m_RigidBody.AddForce(m_Transform.Right * -m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.D))
m_RigidBody.AddForce(m_Transform.Right * m_CurrentSpeed);
if (Input.IsKeyPressed(KeyCode.Space) && m_Colliding)
m_RigidBody.AddForce(Vec3.Up * JumpForce);
}
private void UpdateCameraTransform(){
// TODO: This workflow needs to be improved. (Will be fixed by object parenting)
Mat4 cameraTransform = m_CameraTransform.Transform;
Vec3 cameraTranslation = cameraTransform.Translation;
Vec3 translation = m_Transform.Transform.Translation;
cameraTranslation.XZ = translation.XZ;
cameraTranslation.Y = translation.Y + 1.5F;
cameraTransform.Translation = cameraTranslation;
m_CameraTransform.Transform = cameraTransform;
m_CameraTransform.Rotation = new Vec3(m_CameraRotationX, m_RotationY, 0.0f);
}
}
}

View File

@ -22,9 +22,11 @@ namespace Example
private bool Colliding => m_CollisionCounter > 0;
private TransformComponent m_Transform;
void OnCreate()
{
m_PhysicsBody = GetComponent<RigidBodyComponent>();
m_Transform = GetComponent<TransformComponent>();
MeshComponent meshComponent = GetComponent<MeshComponent>();
m_MeshMaterial = meshComponent.Mesh.GetMaterial(0);
@ -32,6 +34,8 @@ namespace Example
AddCollisionBeginCallback(OnPlayerCollisionBegin);
AddCollisionEndCallback(OnPlayerCollisionEnd);
AddTriggerBeginCallback(OnPlayerTriggerBegin);
AddTriggerEndCallback(OnPlayerTriggerEnd);
}
void OnPlayerCollisionBegin(float value)
@ -44,6 +48,16 @@ namespace Example
m_CollisionCounter--;
}
void OnPlayerTriggerBegin(float value)
{
Console.WriteLine("Player trigger begin");
}
void OnPlayerTriggerEnd(float value)
{
Console.WriteLine("Player trigger end");
}
void OnUpdate(float ts)
{
float movementForce = HorizontalForce;
@ -53,22 +67,18 @@ namespace Example
movementForce *= 0.4f;
}
Vec3 forward = GetForwardDirection();
Vec3 right = GetRightDirection();
Vec3 up = GetUpDirection();
if (Input.IsKeyPressed(KeyCode.W))
m_PhysicsBody.AddForce(forward * movementForce);
m_PhysicsBody.AddForce(m_Transform.Forward * movementForce);
else if (Input.IsKeyPressed(KeyCode.S))
m_PhysicsBody.AddForce(forward * -movementForce);
m_PhysicsBody.AddForce(m_Transform.Forward * -movementForce);
if (Input.IsKeyPressed(KeyCode.D))
m_PhysicsBody.AddForce(right * movementForce);
m_PhysicsBody.AddForce(m_Transform.Right * movementForce);
else if (Input.IsKeyPressed(KeyCode.A))
m_PhysicsBody.AddForce(right * -movementForce);
m_PhysicsBody.AddForce(m_Transform.Right * -movementForce);
if (Colliding && Input.IsKeyPressed(KeyCode.Space))
m_PhysicsBody.AddForce(up * JumpForce);
m_PhysicsBody.AddForce(m_Transform.Up * JumpForce);
if (Colliding)
m_MeshMaterial.Set("u_AlbedoColor", new Vec3(1.0f, 0.0f, 0.0f));