add PhysX SDK and some tweaks

This commit is contained in:
2025-12-17 12:39:20 +08:00
parent 57ca6c30f5
commit 00d3993a77
26 changed files with 1594 additions and 132 deletions

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism;
namespace Example
{
class PlayerSphere : Entity
{
public float HorizontalForce = 10.0f;
public float JumpForce = 10.0f;
private RigidBodyComponent m_PhysicsBody;
private MaterialInstance m_MeshMaterial;
public int m_CollisionCounter = 0;
public Vec3 MaxSpeed = new Vec3();
private bool Colliding => m_CollisionCounter > 0;
void OnCreate()
{
m_PhysicsBody = GetComponent<RigidBodyComponent>();
MeshComponent meshComponent = GetComponent<MeshComponent>();
m_MeshMaterial = meshComponent.Mesh.GetMaterial(0);
m_MeshMaterial.Set("u_Metalness", 0.0f);
AddCollisionBeginCallback(OnPlayerCollisionBegin);
AddCollisionEndCallback(OnPlayerCollisionEnd);
}
void OnPlayerCollisionBegin(float value)
{
m_CollisionCounter++;
}
void OnPlayerCollisionEnd(float value)
{
m_CollisionCounter--;
}
void OnUpdate(float ts)
{
float movementForce = HorizontalForce;
if (!Colliding)
{
movementForce *= 0.4f;
}
Vec3 forward = GetForwardDirection();
Vec3 right = GetRightDirection();
Vec3 up = GetUpDirection();
if (Input.IsKeyPressed(KeyCode.W))
m_PhysicsBody.AddForce(forward * movementForce);
else if (Input.IsKeyPressed(KeyCode.S))
m_PhysicsBody.AddForce(forward * -movementForce);
if (Input.IsKeyPressed(KeyCode.D))
m_PhysicsBody.AddForce(right * movementForce);
else if (Input.IsKeyPressed(KeyCode.A))
m_PhysicsBody.AddForce(right * -movementForce);
if (Colliding && Input.IsKeyPressed(KeyCode.Space))
m_PhysicsBody.AddForce(up * JumpForce);
if (Colliding)
m_MeshMaterial.Set("u_AlbedoColor", new Vec3(1.0f, 0.0f, 0.0f));
else
m_MeshMaterial.Set("u_AlbedoColor", new Vec3(0.8f, 0.8f, 0.8f));
Vec3 linearVelocity = m_PhysicsBody.GetLinearVelocity();
linearVelocity.Clamp(new Vec3(-MaxSpeed.X, -1000, -MaxSpeed.Z), MaxSpeed);
m_PhysicsBody.SetLinearVelocity(linearVelocity);
if (Input.IsKeyPressed(KeyCode.R))
{
Mat4 transform = GetTransform();
transform.Translation = new Vec3(0.0f);
SetTransform(transform);
}
}
}
}