add box2D colliders simple impl, some little tweaks, fixed camera bug

This commit is contained in:
2025-12-13 22:42:03 +08:00
parent 4140a5b4be
commit c92c831d02
30 changed files with 964 additions and 310 deletions

View File

@ -6,27 +6,20 @@ namespace Example
{
public float Speed;
private Entity m_PlayerEntity;
public void OnCreate()
{
m_PlayerEntity = FindEntityByTag("Player");
}
public void OnUpdate(float ts)
{
Mat4 transform = GetTransform();
Vec3 translation = transform.Translation;
float speed = Speed * 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;
translation.XY = m_PlayerEntity.GetTransform().Translation.XY;
translation.Y = Math.Max(translation.Y, 4.5f);
transform.Translation = translation;
SetTransform(transform);
}

View File

@ -11,11 +11,17 @@ namespace Example
class PlayerCube : Entity
{
public float HorizontalForce = 10.0f;
public float VerticalForce = 10.0f;
public float JumpForce = 10.0f;
private RigidBody2DComponent m_PhysicsBody;
private MaterialInstance m_MeshMaterial;
int m_CollisionCounter = 0;
public Vec2 MaxSpeed = new Vec2();
private bool Colliding => m_CollisionCounter > 0;
void OnCreate()
{
m_PhysicsBody = GetComponent<RigidBody2DComponent>();
@ -23,23 +29,46 @@ namespace Example
MeshComponent meshComponent = GetComponent<MeshComponent>();
m_MeshMaterial = meshComponent.Mesh.GetMaterial(0);
m_MeshMaterial.Set("u_Metalness", 0.0f);
AddCollision2DBeginCallback(OnPlayerCollisionBegin);
AddCollision2DEndCallback(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;
}
if (Input.IsKeyPressed(KeyCode.D))
m_PhysicsBody.ApplyLinearImpulse(new Vec2(HorizontalForce, 0), new Vec2(), true);
m_PhysicsBody.ApplyLinearImpulse(new Vec2(movementForce, 0), new Vec2(), true);
else if (Input.IsKeyPressed(KeyCode.A))
m_PhysicsBody.ApplyLinearImpulse(new Vec2(-HorizontalForce, 0), new Vec2(), true);
m_PhysicsBody.ApplyLinearImpulse(new Vec2(-movementForce, 0), new Vec2(), true);
if (Input.IsKeyPressed(KeyCode.Space))
m_PhysicsBody.ApplyLinearImpulse(new Vec2(0, VerticalForce), new Vec2(0, -10), true);
if (Colliding && Input.IsKeyPressed(KeyCode.Space))
m_PhysicsBody.ApplyLinearImpulse(new Vec2(0, JumpForce), new Vec2(0, 0), true);
Vec3 color = new Vec3(0.8f, 0.8f, 0.8f);
if (Input.IsKeyPressed(KeyCode.Q))
color = new Vec3(0.0f, 1.0f, 0.0f);
if (m_CollisionCounter > 0)
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));
m_MeshMaterial.Set("u_AlbedoColor", color);
Vec2 linearVelocity = m_PhysicsBody.GetLinearVelocity();
linearVelocity.Clamp(new Vec2(-MaxSpeed.X, -1000), MaxSpeed);
m_PhysicsBody.SetLinearVelocity(linearVelocity);
if (Input.IsKeyPressed(KeyCode.R))
{