-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Coborax/simple-aabb-physics
feat: added simple physics system
- Loading branch information
Showing
18 changed files
with
253 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace SignE.Core.ECS.Components.Physics | ||
{ | ||
public class AABBComponent : IComponent | ||
{ | ||
public int Width { get; set; } | ||
public int Height { get; set; } | ||
|
||
public void InitComponent() | ||
{ | ||
|
||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
SignE.Core/ECS/Components/Physics/PhysicsMoverComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SignE.Core.ECS.Components.Physics | ||
{ | ||
public class PhysicsMoverComponent : IComponent | ||
{ | ||
public float VelX { get; set; } | ||
public float VelY { get; set; } | ||
|
||
public float Gravity { get; set; } = 200.0f; | ||
|
||
public void InitComponent() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using SignE.Core.ECS.Components; | ||
using SignE.Core.ECS.Components.Physics; | ||
using SignE.Core.Extensions; | ||
|
||
namespace SignE.Core.ECS.Systems.Physics | ||
{ | ||
public class SimplePhysicsSystem : GameSystem | ||
{ | ||
|
||
private List<Entity> _simpleMovers; | ||
|
||
public override void UpdateSystem() | ||
{ | ||
|
||
} | ||
|
||
public override void LateUpdateSystem() | ||
{ | ||
foreach (var entity in _simpleMovers) | ||
{ | ||
var mover = entity.GetComponent<PhysicsMoverComponent>(); | ||
var entityPos = entity.GetComponent<Position2DComponent>(); | ||
var aabb = entity.GetComponent<AABBComponent>(); | ||
|
||
mover.VelY += mover.Gravity; | ||
|
||
var posX = new Position2DComponent | ||
{ | ||
X = entityPos.X + mover.VelX * SignE.Graphics.DeltaTime, | ||
Y = entityPos.Y | ||
}; | ||
|
||
var posY = new Position2DComponent | ||
{ | ||
X = entityPos.X, | ||
Y = entityPos.Y + mover.VelY * SignE.Graphics.DeltaTime | ||
}; | ||
|
||
foreach (var other in Entities) | ||
{ | ||
if (entity == other) | ||
continue; | ||
|
||
var otherPos = other.GetComponent<Position2DComponent>(); | ||
var otherAabb = other.GetComponent<AABBComponent>(); | ||
|
||
if (CheckCollision(posX, aabb, otherPos, otherAabb)) | ||
mover.VelX = 0; | ||
|
||
if (CheckCollision(posY, aabb, otherPos, otherAabb)) | ||
mover.VelY = 0; | ||
} | ||
|
||
entityPos.X += mover.VelX * SignE.Graphics.DeltaTime; | ||
entityPos.Y += mover.VelY * SignE.Graphics.DeltaTime; | ||
} | ||
} | ||
|
||
private static bool CheckCollision(Position2DComponent aPos, AABBComponent aAabb, Position2DComponent bPos, AABBComponent bAabb) | ||
{ | ||
var aIsToTheRightOfB = aPos.X - aAabb.Width / 2 > bPos.X + bAabb.Width / 2; | ||
var aIsToTheLeftOfB = aPos.X + aAabb.Width / 2 < bPos.X - bAabb.Width / 2; | ||
var aIsAboveB = aPos.Y + aAabb.Height / 2 < bPos.Y - bAabb.Height / 2; | ||
var aIsBelowB = aPos.Y - aAabb.Height / 2 > bPos.Y + bAabb.Height / 2; | ||
|
||
return !(aIsToTheRightOfB | ||
|| aIsToTheLeftOfB | ||
|| aIsAboveB | ||
|| aIsBelowB); | ||
} | ||
|
||
public override void DrawSystem() | ||
{ | ||
foreach (var entity in Entities) | ||
{ | ||
var pos = entity.GetComponent<Position2DComponent>(); | ||
var aabb = entity.GetComponent<AABBComponent>(); | ||
|
||
SignE.Graphics.DrawRectangle(pos.X, pos.Y, aabb.Width, aabb.Height, false); | ||
} | ||
} | ||
|
||
public override void GetEntities(World world) | ||
{ | ||
_simpleMovers = world.Entities.WithComponent<Position2DComponent>().WithComponent<AABBComponent>() | ||
.WithComponent<PhysicsMoverComponent>().ToList(); | ||
Entities = world.Entities.WithComponent<Position2DComponent>().WithComponent<AABBComponent>().ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.