Created
December 19, 2022 19:29
-
-
Save MisterKidX/8d7362a47e5bef60a6794bb95945cbea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Scene physicsScene = new Scene(); | |
var go1 = physicsScene.Instantiate(); | |
var rb1 = new Rigidbody(go1) { Gravity = 0, Velocity = new Vector2(0, 1) }; | |
go1.AddComponent(rb1); | |
go1.AddComponent(new Collider(rb1, go1, 1)); | |
var go2 = physicsScene.Instantiate(); | |
go2.transform.Position = new Vector2(0, 10); | |
var rb2 = new Rigidbody(go2) { Gravity = 0, Velocity = new Vector2(0, 0) }; | |
go2.AddComponent(rb2); | |
go2.AddComponent(new Collider(rb2, go2, 1)); | |
physicsScene.Play(); | |
class Scene | |
{ | |
List<GameObject> _gameObjects = new List<GameObject>(); | |
public void Play() | |
{ | |
var deltatime = 0.016f; | |
while (true) | |
{ | |
foreach (var go in _gameObjects) | |
{ | |
go.Update(deltatime); | |
Physics.Instance.Update(deltatime); | |
} | |
Thread.Sleep((int)(deltatime * 1000)); | |
} | |
} | |
public GameObject Instantiate() | |
{ | |
var go = new GameObject(); | |
go.Affiliation = this; | |
_gameObjects.Add(go); | |
return go; | |
} | |
} | |
class GameObject : IUpdate | |
{ | |
List<Component> _components; | |
public Transform transform => _components[0] as Transform; | |
public Scene Affiliation; | |
public GameObject() | |
{ | |
_components = new List<Component>(); | |
_components.Add(new Transform(this)); | |
} | |
public void Update(float deltaTime) | |
{ | |
foreach (var comp in _components) | |
{ | |
comp.Update(deltaTime); | |
} | |
} | |
public void AddComponent(Component c) | |
{ | |
_components.Add(c); | |
} | |
} | |
abstract class Component : IUpdate | |
{ | |
public GameObject gameobject; | |
public Component(GameObject go) | |
{ | |
gameobject = go; | |
} | |
public virtual void Update(float deltaTime) | |
{ | |
} | |
} | |
class Transform : Component | |
{ | |
public Vector2 Position; | |
public Transform(GameObject go) : base(go) | |
{ | |
} | |
} | |
class Rigidbody : Component | |
{ | |
public const float EarthGravity = -9.8f; | |
public float Gravity = -9.8f; | |
public Vector2 Velocity; | |
Collider _collider; | |
public Collider Collider => _collider; | |
public Rigidbody(GameObject go) : base(go) | |
{ | |
Physics.Instance.Add(this); | |
} | |
public void AssignCollider(Collider collider) | |
{ | |
_collider = collider; | |
} | |
public override void Update(float deltaTime) | |
{ | |
Vector2 displacement = new Vector2(Velocity.x, Velocity.y + Gravity * deltaTime); | |
gameobject.transform.Position = new Vector2 | |
(gameobject.transform.Position.x + displacement.x, gameobject.transform.Position.y + displacement.y); | |
} | |
} | |
class Collider : Component | |
{ | |
public float Radius; | |
public Collider(Rigidbody rb, GameObject go, float radius = 3) : base(go) | |
{ | |
Radius = radius; | |
rb.AssignCollider(this); | |
} | |
public void OnCollision(Collider other) | |
{ | |
Console.WriteLine("I'm walking here."); | |
} | |
} | |
struct Vector2 | |
{ | |
public float x; | |
public float y; | |
public float Magnitude => MathF.Sqrt(x * x + y * y); | |
public Vector2 Subtract(Vector2 other) | |
{ | |
return new Vector2(x - other.x, y - other.y); | |
} | |
public Vector2(float x, float y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
public override string ToString() | |
{ | |
return $"({x}, {y})"; | |
} | |
} | |
interface IUpdate | |
{ | |
void Update(float deltaTime); | |
} | |
class Physics : IUpdate | |
{ | |
List<Rigidbody> _rigidbodies = new List<Rigidbody>(); | |
public void Add(Rigidbody rb) | |
{ | |
_rigidbodies.Add(rb); | |
} | |
public void Update(float deltaTime) | |
{ | |
foreach (var rb in _rigidbodies) | |
{ | |
var collider = rb.Collider; | |
foreach (var rb2 in _rigidbodies) | |
{ | |
if (rb2 == rb) | |
continue; | |
var v2 = rb2.gameobject.transform.Position; | |
var v1 = rb.gameobject.transform.Position; | |
var v3 = v2.Subtract(v1); | |
var magnitude = v3.Magnitude; | |
var radii = rb.Collider.Radius + rb2.Collider.Radius; | |
if (magnitude < radii) | |
{ | |
rb.Collider.OnCollision(rb2.Collider); | |
rb2.Collider.OnCollision(rb.Collider); | |
} | |
} | |
} | |
} | |
private static Physics _instance; | |
public static Physics Instance | |
{ | |
get | |
{ | |
if (_instance == null) | |
_instance = new Physics(); | |
return _instance; | |
} | |
} | |
private Physics() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment