add PhysX SDK and some tweaks
This commit is contained in:
@ -5,6 +5,7 @@ namespace Example
|
||||
public class BasicController : Entity
|
||||
{
|
||||
public float Speed;
|
||||
public float DistanceFromPlayer = 20.0F;
|
||||
|
||||
private Entity m_PlayerEntity;
|
||||
|
||||
@ -17,8 +18,10 @@ namespace Example
|
||||
{
|
||||
Mat4 transform = GetTransform();
|
||||
|
||||
Vec3 playerTranstation = m_PlayerEntity.GetTransform().Translation;
|
||||
Vec3 translation = transform.Translation;
|
||||
translation.XY = m_PlayerEntity.GetTransform().Translation.XY;
|
||||
translation.XY = playerTranstation.XY;
|
||||
translation.Z = playerTranstation.Z + DistanceFromPlayer;
|
||||
translation.Y = Math.Max(translation.Y, 4.5f);
|
||||
transform.Translation = translation;
|
||||
SetTransform(transform);
|
||||
|
||||
92
ExampleApp/Src/PlayerSphere.cs
Normal file
92
ExampleApp/Src/PlayerSphere.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user