add transform class, some code tweaks

This commit is contained in:
2025-12-29 23:11:14 +08:00
parent ce41e348f8
commit 960eeaf94b
32 changed files with 368 additions and 314 deletions

View File

@ -16,6 +16,7 @@ namespace Example
public void OnUpdate(float ts)
{
/*
Mat4 transform = GetTransform();
Vec3 playerTranstation = m_PlayerEntity.GetTransform().Translation;
@ -25,6 +26,7 @@ namespace Example
translation.Y = Math.Max(translation.Y, 4.5f);
transform.Translation = translation;
SetTransform(transform);
*/
}

View File

@ -20,8 +20,6 @@ namespace FPSExample
private Entity m_CameraEntity;
private Vec2 m_LastMousePosition;
private float m_CameraRotationX = 0.0f;
private float m_RotationY = 0.0f;
void OnCreate()
{
@ -50,6 +48,7 @@ namespace FPSExample
Input.SetCursorMode(Input.CursorMode.Locked);
m_CurrentSpeed = Input.IsKeyPressed(KeyCode.LeftControl) ? RunSpeed : WalkingSpeed;
UpdateRotation(ts);
UpdateMovement();
@ -58,60 +57,59 @@ namespace FPSExample
private void UpdateRotation(float ts)
{
Vec2 currentMousePosition = Input.GetMousePosition();
Vec2 delta = m_LastMousePosition - currentMousePosition;
if (delta.X != 0)
Vec2 currentMousePosition = Input.GetMousePosition();
Vec2 delta = m_LastMousePosition - currentMousePosition;
float yRotation = delta.X * MouseSensitivity * ts;
float xRotation = delta.Y * MouseSensitivity * ts;
m_RigidBody.Rotate(new Vec3(0.0f, yRotation, 0.0f));
if (delta.X != 0 || delta.Y != 0)
{
m_RotationY += delta.X * MouseSensitivity * ts;
m_CameraTransform.Rotation += new Vec3(xRotation, yRotation, 0.0f);
}
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_CameraTransform.Rotation = new Vec3(Mathf.Clamp(m_CameraTransform.Rotation.X, -89.0f, 89.0f), m_CameraTransform.Rotation.YZ);
m_LastMousePosition = currentMousePosition;
}
Collider[] colliders = new Collider[10];
Collider[] colliders = new Collider[10];
private void UpdateMovement()
{
RaycastHit hitInfo;
if (Input.IsKeyPressed(KeyCode.H) &&
Physics.Raycast(m_CameraTransform.Transform.Translation + (m_CameraTransform.Forward * 5.0f),
m_CameraTransform.Forward, 20.0f, out hitInfo))
Physics.Raycast(m_CameraTransform.Position + (m_CameraTransform.Transform.Forward * 5.0f),
m_CameraTransform.Transform.Forward, 20.0f, out hitInfo))
{
FindEntityByID(hitInfo.EntityID).GetComponent<MeshComponent>().Mesh.GetMaterial(0).Set("u_AlbedoColor", new Vec3(1.0f ,0.0f, 0.0f));
}
if (Input.IsKeyPressed(KeyCode.I))
{
// NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array
// whenever you call it. The normal versions allocates a brand new array every time.
// NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array
// whenever you call it. The normal versions allocates a brand new array every time.
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Transform.Translation, new Vec3(1.0F), colliders);
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Position, new Vec3(1.0F), colliders);
Console.WriteLine("Colliders: {0}", numColliders);
// When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist
for (int i = 0; i < numColliders; i++)
{
Console.WriteLine(colliders[i]);
}
// When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist
for (int i = 0; i < numColliders; i++)
{
Console.WriteLine(colliders[i]);
}
}
if (Input.IsKeyPressed(KeyCode.W))
m_RigidBody.AddForce(m_Transform.Forward * m_CurrentSpeed);
m_RigidBody.AddForce(m_Transform.Transform.Forward * m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.S))
m_RigidBody.AddForce(m_Transform.Forward * -m_CurrentSpeed);
m_RigidBody.AddForce(m_Transform.Transform.Forward * -m_CurrentSpeed);
if (Input.IsKeyPressed(KeyCode.A))
m_RigidBody.AddForce(m_Transform.Right * -m_CurrentSpeed);
m_RigidBody.AddForce(m_Transform.Transform.Right * -m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.D))
m_RigidBody.AddForce(m_Transform.Right * m_CurrentSpeed);
m_RigidBody.AddForce(m_Transform.Transform.Right * m_CurrentSpeed);
if (Input.IsKeyPressed(KeyCode.Space) && m_Colliding)
m_RigidBody.AddForce(Vec3.Up * JumpForce);
@ -119,19 +117,9 @@ namespace FPSExample
}
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.Forward * -Mathf.Clamp(Distance, 0, 10);
m_CameraTransform.Transform = cameraTransform;
m_CameraTransform.Rotation = new Vec3(m_CameraRotationX, m_RotationY, 0.0f);
Vec3 position = m_Transform.Position;
position.Y += 1.5f;
m_CameraTransform.Position = position;
}
}
}

View File

@ -78,6 +78,7 @@ namespace Example
void OnUpdate(float ts)
{
/*
Mat4 transform = GetTransform();
Vec3 translation = transform.Translation;
translation.Y += ts * speed;
@ -91,6 +92,7 @@ namespace Example
transform.Translation = translation;
SetTransform(transform);
*/
}

View File

@ -72,9 +72,11 @@ namespace Example
if (Input.IsKeyPressed(KeyCode.R))
{
/*
Mat4 transform = GetTransform();
transform.Translation = new Vec3(0.0f);
SetTransform(transform);
*/
}
}

View File

@ -68,17 +68,17 @@ namespace Example
}
if (Input.IsKeyPressed(KeyCode.W))
m_PhysicsBody.AddForce(m_Transform.Forward * movementForce);
m_PhysicsBody.AddForce(m_Transform.Transform.Forward * movementForce);
else if (Input.IsKeyPressed(KeyCode.S))
m_PhysicsBody.AddForce(m_Transform.Forward * -movementForce);
m_PhysicsBody.AddForce(m_Transform.Transform.Forward * -movementForce);
if (Input.IsKeyPressed(KeyCode.D))
m_PhysicsBody.AddForce(m_Transform.Right * movementForce);
m_PhysicsBody.AddForce(m_Transform.Transform.Right * movementForce);
else if (Input.IsKeyPressed(KeyCode.A))
m_PhysicsBody.AddForce(m_Transform.Right * -movementForce);
m_PhysicsBody.AddForce(m_Transform.Transform.Right * -movementForce);
if (Colliding && Input.IsKeyPressed(KeyCode.Space))
m_PhysicsBody.AddForce(m_Transform.Up * JumpForce);
m_PhysicsBody.AddForce(m_Transform.Transform.Up * JumpForce);
if (Colliding)
m_MeshMaterial.Set("u_AlbedoColor", new Vec3(1.0f, 0.0f, 0.0f));
@ -91,9 +91,11 @@ namespace Example
if (Input.IsKeyPressed(KeyCode.R))
{
/*
Mat4 transform = GetTransform();
transform.Translation = new Vec3(0.0f);
SetTransform(transform);
*/
}
}

View File

@ -21,6 +21,7 @@ namespace Example
public void OnUpdate(float ts)
{
/*
Rotation += ts;
@ -34,19 +35,10 @@ namespace Example
translation.Z += Velocity.Z * ts;
translation.Y -= SinkRate * ts;
/*
if (Input.IsKeyPressed(KeyCode.Up))
translation.Y += speed;
else if (Input.IsKeyPressed(KeyCode.Down))
translation.Y -= speed;
if (Input.IsKeyPressed(KeyCode.Right))
translation.X += speed;
else if (Input.IsKeyPressed(KeyCode.Left))
translation.X -= speed;
*/
transform.Translation = translation;
SetTransform(transform);
*/
}
}

View File

@ -14,6 +14,7 @@ namespace Example
void OnUpdate(float ts)
{
/*
Mat4 transform = GetTransform();
Vec3 translation = transform.Translation;
@ -21,6 +22,7 @@ namespace Example
transform.Translation = translation;
SetTransform(transform);
*/
}
}