From b59e4607416adaa6b518eecb104c2864664ca7f6 Mon Sep 17 00:00:00 2001 From: Thiago Menezes Date: Wed, 29 May 2024 17:55:12 -0300 Subject: [PATCH] perf: Only subscribe to OnUpdate if it is overriden perf: Micro optimizations that probably wont matter --- Basalt.Raylib/Components/BoxRenderer.cs | 15 -------------- Basalt.Raylib/Components/Button.cs | 11 ---------- .../Components/EntityLineRenderer.cs | 7 ------- Basalt.Raylib/Components/Image.cs | 11 ---------- Basalt.Raylib/Components/Label.cs | 14 +------------ Basalt.Raylib/Components/LineRenderer.cs | 13 ------------ Basalt.Raylib/Components/ModelRenderer.cs | 9 --------- Basalt.Raylib/Components/Panel.cs | 9 --------- Basalt.Raylib/Components/ProgressBar.cs | 11 ---------- Basalt.Raylib/Components/SphereRenderer.cs | 9 --------- Basalt.TestField/Components/DebugInfo.cs | 10 +++++----- .../Components/PlayerController.cs | 2 +- Basalt.TestField/Program.cs | 2 +- Basalt.TestField/TestingUtils.cs | 6 +++--- Basalt/Common/Components/BoxCollider.cs | 20 ------------------- Basalt/Common/Components/ChainLink.cs | 8 -------- Basalt/Common/Components/Component.cs | 12 ++++++----- Basalt/Common/Components/FixedLink.cs | 9 --------- Basalt/Common/Components/Rigidbody.cs | 7 ------- Basalt/Common/Components/Transform.cs | 15 -------------- Basalt/Common/Physics/CollisionHandler.cs | 8 ++++---- Basalt/Common/Physics/PhysicsEngine.cs | 3 +-- 22 files changed, 23 insertions(+), 188 deletions(-) diff --git a/Basalt.Raylib/Components/BoxRenderer.cs b/Basalt.Raylib/Components/BoxRenderer.cs index bac0043..f514cce 100644 --- a/Basalt.Raylib/Components/BoxRenderer.cs +++ b/Basalt.Raylib/Components/BoxRenderer.cs @@ -48,21 +48,6 @@ public BoxRenderer(Entity entity) : base(entity) { } - /// - /// Called when the component starts. - /// - public override void OnStart() - { - - } - - /// - /// Called every frame to update the component. - /// - public override void OnUpdate() - { - - } /// /// Called every frame to render the component. diff --git a/Basalt.Raylib/Components/Button.cs b/Basalt.Raylib/Components/Button.cs index 18a83b8..e2ae3f5 100644 --- a/Basalt.Raylib/Components/Button.cs +++ b/Basalt.Raylib/Components/Button.cs @@ -65,17 +65,6 @@ public Button(Entity entity) : base(entity) { } - /// - public override void OnStart() - { - - } - - /// - public override void OnUpdate() - { - - } /// public override void OnUIRender() diff --git a/Basalt.Raylib/Components/EntityLineRenderer.cs b/Basalt.Raylib/Components/EntityLineRenderer.cs index f9d7ce0..61c50aa 100644 --- a/Basalt.Raylib/Components/EntityLineRenderer.cs +++ b/Basalt.Raylib/Components/EntityLineRenderer.cs @@ -81,13 +81,6 @@ public override void OnStart() } } - /// - /// Called every frame to update the component. - /// - public override void OnUpdate() - { - } - /// /// Called every frame to render the component. /// diff --git a/Basalt.Raylib/Components/Image.cs b/Basalt.Raylib/Components/Image.cs index 08b9020..4578538 100644 --- a/Basalt.Raylib/Components/Image.cs +++ b/Basalt.Raylib/Components/Image.cs @@ -33,17 +33,6 @@ public Image(Entity entity) : base(entity) { } - /// - public override void OnStart() - { - - } - - /// - public override void OnUpdate() - { - - } /// public override void OnUIRender() diff --git a/Basalt.Raylib/Components/Label.cs b/Basalt.Raylib/Components/Label.cs index a36d1e9..6b9f7a9 100644 --- a/Basalt.Raylib/Components/Label.cs +++ b/Basalt.Raylib/Components/Label.cs @@ -38,18 +38,6 @@ public Label(Entity entity) : base(entity) { } - /// - public override void OnStart() - { - - } - - /// - public override void OnUpdate() - { - - } - /// public override void OnUIRender() { @@ -58,7 +46,7 @@ public override void OnUIRender() Raylib_cs.Raylib.DrawTextPro(Raylib_cs.Raylib.GetFontDefault(), Text, position, - Raylib_cs.Raylib.MeasureTextEx(Raylib_cs.Raylib.GetFontDefault(), Text, FontSize, Spacing) / 2, + Raylib_cs.Raylib.MeasureTextEx(Raylib_cs.Raylib.GetFontDefault(), Text, FontSize, Spacing) * 0.5f, Rotation, FontSize, Spacing, diff --git a/Basalt.Raylib/Components/LineRenderer.cs b/Basalt.Raylib/Components/LineRenderer.cs index 9f756f9..7bddeab 100644 --- a/Basalt.Raylib/Components/LineRenderer.cs +++ b/Basalt.Raylib/Components/LineRenderer.cs @@ -49,19 +49,6 @@ public LineRenderer(Entity entity) : base(entity) { } - /// - /// Called when the component is started. - /// - public override void OnStart() - { - } - - /// - /// Called every frame to update the component. - /// - public override void OnUpdate() - { - } /// /// Called every frame to render the component. diff --git a/Basalt.Raylib/Components/ModelRenderer.cs b/Basalt.Raylib/Components/ModelRenderer.cs index a184095..e3b1c74 100644 --- a/Basalt.Raylib/Components/ModelRenderer.cs +++ b/Basalt.Raylib/Components/ModelRenderer.cs @@ -55,15 +55,6 @@ public ModelRenderer(Entity entity) : base(entity) { } - /// - public override void OnStart() - { - } - - /// - public override void OnUpdate() - { - } /// public override unsafe void OnRender() diff --git a/Basalt.Raylib/Components/Panel.cs b/Basalt.Raylib/Components/Panel.cs index bf2effa..f2f1cde 100644 --- a/Basalt.Raylib/Components/Panel.cs +++ b/Basalt.Raylib/Components/Panel.cs @@ -28,15 +28,6 @@ public Panel(Entity entity) : base(entity) { } - /// - public override void OnStart() - { - } - - /// - public override void OnUpdate() - { - } /// public override void OnUIRender() diff --git a/Basalt.Raylib/Components/ProgressBar.cs b/Basalt.Raylib/Components/ProgressBar.cs index 2d17c1e..3a1115f 100644 --- a/Basalt.Raylib/Components/ProgressBar.cs +++ b/Basalt.Raylib/Components/ProgressBar.cs @@ -39,17 +39,6 @@ public ProgressBar(Entity entity) : base(entity) { } - /// - public override void OnStart() - { - - } - - /// - public override void OnUpdate() - { - - } /// public override void OnUIRender() diff --git a/Basalt.Raylib/Components/SphereRenderer.cs b/Basalt.Raylib/Components/SphereRenderer.cs index 10aed1d..685a5e3 100644 --- a/Basalt.Raylib/Components/SphereRenderer.cs +++ b/Basalt.Raylib/Components/SphereRenderer.cs @@ -56,15 +56,6 @@ public SphereRenderer(Entity entity) : base(entity) } - /// - public override void OnStart() - { - } - - /// - public override void OnUpdate() - { - } /// public override unsafe void OnRender() diff --git a/Basalt.TestField/Components/DebugInfo.cs b/Basalt.TestField/Components/DebugInfo.cs index 5e3d0bd..d3ba5f2 100644 --- a/Basalt.TestField/Components/DebugInfo.cs +++ b/Basalt.TestField/Components/DebugInfo.cs @@ -38,11 +38,11 @@ public override void OnPhysicsUpdate() public override void OnUIRender() { DrawFPS(10, 10); - DrawText($"Entities: {Engine.Instance.EntityManager.EntityCount}", 10, 30, 18, Color.DarkGreen); - DrawText($"Threads Used: {Process.GetCurrentProcess().Threads.Count}", 10, 50, 18, Color.DarkGreen); - DrawText($"RAM Used: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB", 10, 70,18, Color.DarkGreen); - DrawText($"Average Physics DeltaTime: {pastPhysicsDeltaTime.Average()} ms", 10, 90, 18, Color.DarkGreen); - DrawText($"Average FPS: {pastFps.Average()}", 10, 110, 18, Color.DarkGreen); + //DrawText($"Entities: {Engine.Instance.EntityManager.EntityCount}", 10, 30, 18, Color.DarkGreen); + //DrawText($"Threads Used: {Process.GetCurrentProcess().Threads.Count}", 10, 50, 18, Color.DarkGreen); + //DrawText($"RAM Used: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB", 10, 70,18, Color.DarkGreen); + //DrawText($"Average Physics DeltaTime: {pastPhysicsDeltaTime.Average()} ms", 10, 90, 18, Color.DarkGreen); + //DrawText($"Average FPS: {pastFps.Average()}", 10, 110, 18, Color.DarkGreen); } } } diff --git a/Basalt.TestField/Components/PlayerController.cs b/Basalt.TestField/Components/PlayerController.cs index 77de993..0f30cfb 100644 --- a/Basalt.TestField/Components/PlayerController.cs +++ b/Basalt.TestField/Components/PlayerController.cs @@ -10,7 +10,7 @@ namespace Basalt.TestField.Components { public class PlayerController : Component { - public float MoveSpeed = 25; + public float MoveSpeed = 10; private IInputSystem inputSystem; public PlayerController(Entity entity) : base(entity) { diff --git a/Basalt.TestField/Program.cs b/Basalt.TestField/Program.cs index ab9efd5..02d88eb 100644 --- a/Basalt.TestField/Program.cs +++ b/Basalt.TestField/Program.cs @@ -62,5 +62,5 @@ -TestingUtils.SetupTestingScene(); +TestingUtils.SetupTestingScene(250); TestingUtils.SetupDebugInfo(); \ No newline at end of file diff --git a/Basalt.TestField/TestingUtils.cs b/Basalt.TestField/TestingUtils.cs index c6e70ae..09ad3a5 100644 --- a/Basalt.TestField/TestingUtils.cs +++ b/Basalt.TestField/TestingUtils.cs @@ -9,7 +9,7 @@ namespace Basalt.TestField { public static class TestingUtils { - public static void SetupTestingScene(int boxCount = 10, int ropeLength = 20) + public static void SetupTestingScene(int boxCount = 50, int ropeLength = 20) { var ground = new Entity(); ground.Transform.Position = new Vector3(0, -1, 0); @@ -24,8 +24,8 @@ public static void SetupTestingScene(int boxCount = 10, int ropeLength = 20) { var box = new Entity(); box.Transform.Position = new Vector3(Random.Shared.Next(-30, 30), 25 + i, Random.Shared.Next(-30, 30)); - box.AddComponent(new ModelRenderer(box) { ModelCacheKey = "robot", Size = new Vector3((i + 1) / 5f), Offset = -Vector3.UnitY * (i + 1) / 2.5f }); - box.AddComponent(new BoxCollider(box) { Size = new Vector3(i) }); + box.AddComponent(new ModelRenderer(box) { ModelCacheKey = "knot", Size = Vector3.One }); + box.AddComponent(new BoxCollider(box) { Size = Vector3.One }); box.AddComponent(new Rigidbody(box) { IsKinematic = false, Mass = 1 }); Engine.CreateEntity(box); diff --git a/Basalt/Common/Components/BoxCollider.cs b/Basalt/Common/Components/BoxCollider.cs index eaa3ab4..c2e749f 100644 --- a/Basalt/Common/Components/BoxCollider.cs +++ b/Basalt/Common/Components/BoxCollider.cs @@ -28,26 +28,6 @@ public BoxCollider(Entity entity) : base(entity) { } - - /// - /// Called when the component starts. - /// - public override void OnStart() - { - } - - /// - /// Called every frame to update the component. - /// - public override void OnUpdate() { } - - /// - /// Called when the component needs to be rendered. - /// - public override void OnRender() - { - - } } } diff --git a/Basalt/Common/Components/ChainLink.cs b/Basalt/Common/Components/ChainLink.cs index 95d9db7..de4ab78 100644 --- a/Basalt/Common/Components/ChainLink.cs +++ b/Basalt/Common/Components/ChainLink.cs @@ -12,14 +12,6 @@ public ChainLink(Entity entity) : base(entity) { } - public override void OnStart() - { - - } - public override void OnUpdate() - { - - } public override void OnPhysicsUpdate() { // Calculate the vector between the two entities diff --git a/Basalt/Common/Components/Component.cs b/Basalt/Common/Components/Component.cs index 29184fa..f18d99e 100644 --- a/Basalt/Common/Components/Component.cs +++ b/Basalt/Common/Components/Component.cs @@ -42,12 +42,12 @@ public virtual void OnRender() /// /// Called when the component is started. /// - public abstract void OnStart(); + public virtual void OnStart() { } /// /// Called every frame to update the component. /// - public abstract void OnUpdate(); + public virtual void OnUpdate() { } /// /// Called every physics update to update the component's physics. @@ -110,14 +110,16 @@ private virtual protected void SubscribeToEvents() var eventbus = Engine.Instance.GetEngineComponent()!; Type type = this.GetType(); - eventbus.Subscribe(BasaltConstants.UpdateEventKey, OnUpdateEvent); + // Check if OnUpdate was overriden + if (type.GetMethod(nameof(OnUpdate))!.DeclaringType != typeof(Component)) + eventbus.Subscribe(BasaltConstants.UpdateEventKey, OnUpdateEvent); // Check if OnRender was overriden - if (type.GetMethod(nameof(OnRender)).DeclaringType != typeof(Component)) + if (type.GetMethod(nameof(OnRender))!.DeclaringType != typeof(Component)) eventbus.Subscribe(BasaltConstants.RenderEventKey, OnRenderEvent); // Check if OnPhysicsUpdate was overriden - if (type.GetMethod(nameof(OnPhysicsUpdate)).DeclaringType != typeof(Component)) + if (type.GetMethod(nameof(OnPhysicsUpdate))!.DeclaringType != typeof(Component)) eventbus.Subscribe(BasaltConstants.PhysicsUpdateEventKey, OnPhysicsUpdateEvent); } diff --git a/Basalt/Common/Components/FixedLink.cs b/Basalt/Common/Components/FixedLink.cs index a83783d..5304188 100644 --- a/Basalt/Common/Components/FixedLink.cs +++ b/Basalt/Common/Components/FixedLink.cs @@ -12,15 +12,6 @@ public FixedLink(Entity entity) : base(entity) { } - public override void OnStart() - { - - } - - public override void OnUpdate() - { - // Optional: Add any behavior you want to occur during regular updates - } public override void OnPhysicsUpdate() { diff --git a/Basalt/Common/Components/Rigidbody.cs b/Basalt/Common/Components/Rigidbody.cs index ed3f1eb..1ebd8ad 100644 --- a/Basalt/Common/Components/Rigidbody.cs +++ b/Basalt/Common/Components/Rigidbody.cs @@ -100,13 +100,6 @@ public override void OnStart() } - /// - /// Called on each frame update. - /// - public override void OnUpdate() - { - } - public void AddForce(Vector3 force, ForceType type = ForceType.Force) { if (IsKinematic) diff --git a/Basalt/Common/Components/Transform.cs b/Basalt/Common/Components/Transform.cs index fe5adaf..bf134e5 100644 --- a/Basalt/Common/Components/Transform.cs +++ b/Basalt/Common/Components/Transform.cs @@ -89,20 +89,5 @@ public Transform(Entity entity) : base(entity) Position = new Vector3(); } - /// - /// Called when the component starts. - /// - public override void OnStart() - { - - } - - /// - /// Called every frame to update the component. - /// - public override void OnUpdate() - { - - } } } diff --git a/Basalt/Common/Physics/CollisionHandler.cs b/Basalt/Common/Physics/CollisionHandler.cs index a159c0e..548ad80 100644 --- a/Basalt/Common/Physics/CollisionHandler.cs +++ b/Basalt/Common/Physics/CollisionHandler.cs @@ -68,8 +68,8 @@ private static void BoxBoxCollision(Collider col1, Collider col2) return; // Cannot collide with itself } - Vector3 extents1 = box1.Size / 2f; - Vector3 extents2 = box2.Size / 2f; + Vector3 extents1 = box1.Size * 0.5f; + Vector3 extents2 = box2.Size * 0.5f; // Calculate the min and max points of the two colliders along each axis Vector3 min1 = box1.Position - extents1; @@ -140,8 +140,8 @@ private static void BoxBoxCollision(Collider col1, Collider col2) rb2.Velocity -= impulse / rb2.Mass; } - box1.Entity.Transform.Position += separationDirection * separationDistance / 2f; - box2.Entity.Transform.Position -= separationDirection * separationDistance / 2f; + box1.Entity.Transform.Position += separationDirection * separationDistance * 0.5f; + box2.Entity.Transform.Position -= separationDirection * separationDistance * 0.5f; } } } diff --git a/Basalt/Common/Physics/PhysicsEngine.cs b/Basalt/Common/Physics/PhysicsEngine.cs index c3e8651..a215477 100644 --- a/Basalt/Common/Physics/PhysicsEngine.cs +++ b/Basalt/Common/Physics/PhysicsEngine.cs @@ -112,12 +112,11 @@ public void Simulate() }); elapsedTime = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime; - Time.PhysicsDeltaTime = targetFrameTimeMs / 1000f; if (elapsedTime > targetFrameTimeMs) { logger?.LogWarning($"Physics engine is running behind. Elapsed time: {elapsedTime}ms"); - Time.PhysicsDeltaTime = elapsedTime / 1000f; + Time.PhysicsDeltaTime = elapsedTime * 0.001f; continue; }