mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Fixed circle rendering and added 2D circle collider
This commit is contained in:
@@ -2,6 +2,7 @@ using System;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.Core;
|
||||
using Box2D;
|
||||
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
@@ -49,6 +50,8 @@ namespace GlitchyEngine.World
|
||||
public ColorRGBA Color = .White;
|
||||
public Vector4 UvTransform = .(0, 0, 1, 1);
|
||||
|
||||
public bool IsCircle = false;
|
||||
|
||||
public this()
|
||||
{
|
||||
}
|
||||
@@ -363,7 +366,15 @@ namespace GlitchyEngine.World
|
||||
|
||||
public bool FixedRotation = false;
|
||||
|
||||
internal int _runtimeBody = 0;
|
||||
private int _runtimeBody = 0;
|
||||
|
||||
internal b2Body* RuntimeBody
|
||||
{
|
||||
[Inline]
|
||||
get => (b2Body*)(void*)_runtimeBody;
|
||||
[Inline]
|
||||
set mut => _runtimeBody = (int)(void*)value;
|
||||
}
|
||||
}
|
||||
|
||||
struct BoxCollider2DComponent
|
||||
@@ -377,6 +388,43 @@ namespace GlitchyEngine.World
|
||||
public float Restitution = 0.0f;
|
||||
public float RestitutionThreshold = 0.5f;
|
||||
|
||||
internal int _runtimeFixture = 0;
|
||||
private int _runtimeFixture = 0;
|
||||
|
||||
internal b2Fixture* RuntimeFixture
|
||||
{
|
||||
[Inline]
|
||||
get => (b2Fixture*)(void*)_runtimeFixture;
|
||||
[Inline]
|
||||
set mut => _runtimeFixture = (int)(void*)value;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
internal ref b2Vec2 b2Offset mut => ref *(Box2D.b2Vec2*)(void*)&Offset;
|
||||
}
|
||||
|
||||
struct CircleCollider2DComponent
|
||||
{
|
||||
public Vector2 Offset = .(0.0f, 0.0f);
|
||||
|
||||
public float Radius = 0.5f;
|
||||
|
||||
// TODO: move into 2D physics material
|
||||
public float Density = 1.0f;
|
||||
public float Friction = 0.5f;
|
||||
public float Restitution = 0.0f;
|
||||
public float RestitutionThreshold = 0.5f;
|
||||
|
||||
private int _runtimeFixture = 0;
|
||||
|
||||
internal b2Fixture* RuntimeFixture
|
||||
{
|
||||
[Inline]
|
||||
get => (b2Fixture*)(void*)_runtimeFixture;
|
||||
[Inline]
|
||||
set mut => _runtimeFixture = (int)(void*)value;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
internal ref b2Vec2 b2Offset mut => ref *(Box2D.b2Vec2*)(void*)&Offset;
|
||||
}
|
||||
}
|
||||
@@ -121,12 +121,13 @@ namespace GlitchyEngine.World
|
||||
b2Body* body = Box2D.World.CreateBody(_physicsWorld2D, &def);
|
||||
Box2D.Body.SetFixedRotation(body, rigidBody.FixedRotation);
|
||||
|
||||
rigidBody._runtimeBody = (int)(void*)body;
|
||||
rigidBody.RuntimeBody = body;
|
||||
|
||||
if (entity.TryGetComponent<BoxCollider2DComponent>(let boxCollider))
|
||||
{
|
||||
b2Shape* boxShape = Box2D.Shape.CreatePolygon();
|
||||
Box2D.Shape.PolygonSetAsBox(boxShape, boxCollider.Size.X * transform.Scale.X, boxCollider.Size.Y * transform.Scale.Y);
|
||||
Box2D.Shape.PolygonSetAsBoxWithCenterAngle(boxShape, boxCollider.Size.X * transform.Scale.X, boxCollider.Size.Y * transform.Scale.Y, ref boxCollider.b2Offset, 0.0f);
|
||||
|
||||
b2FixtureDef fixtureDef = .();
|
||||
fixtureDef.shape = boxShape;
|
||||
@@ -136,7 +137,24 @@ namespace GlitchyEngine.World
|
||||
fixtureDef.restitutionThreshold = boxCollider.RestitutionThreshold;
|
||||
|
||||
b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef);
|
||||
boxCollider._runtimeFixture = (int)(void*)fixture;
|
||||
boxCollider.RuntimeFixture = fixture;
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent<CircleCollider2DComponent>(let circleCollider))
|
||||
{
|
||||
b2Shape* circleShape = Box2D.Shape.CreateCircle();
|
||||
Box2D.Shape.CircleSetPosition(circleShape, ref circleCollider.b2Offset);
|
||||
Box2D.Shape.SetRadius(circleShape, circleCollider.Radius);
|
||||
|
||||
b2FixtureDef fixtureDef = .();
|
||||
fixtureDef.shape = circleShape;
|
||||
fixtureDef.density = circleCollider.Density;
|
||||
fixtureDef.friction = circleCollider.Friction;
|
||||
fixtureDef.restitution = circleCollider.Restitution;
|
||||
fixtureDef.restitutionThreshold = circleCollider.RestitutionThreshold;
|
||||
|
||||
b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef);
|
||||
circleCollider.RuntimeFixture = fixture;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,7 +202,7 @@ namespace GlitchyEngine.World
|
||||
var transform = entity.Transform;
|
||||
var rigidbody = entry.Component;
|
||||
|
||||
b2Body* body = (b2Body*)(void*)rigidbody._runtimeBody;
|
||||
b2Body* body = rigidbody.RuntimeBody;
|
||||
b2Vec2 position = Box2D.Body.GetPosition(body);
|
||||
float angle = Box2D.Body.GetAngle(body);
|
||||
|
||||
|
||||
@@ -88,6 +88,7 @@ namespace GlitchyEngine.World
|
||||
|
||||
Serialize.Value(writer, "Color", component.Color);
|
||||
Serialize.Value(writer, "UvTransform", component.UvTransform);
|
||||
Serialize.Value(writer, "IsCircle", component.IsCircle);
|
||||
});
|
||||
|
||||
SerializeComponent<TransformComponent>(writer, entity, "TransformComponent", scope (component) =>
|
||||
@@ -156,6 +157,17 @@ namespace GlitchyEngine.World
|
||||
Serialize.Value(writer, "Restitution", component.Restitution);
|
||||
Serialize.Value(writer, "RestitutionThreshold", component.RestitutionThreshold);
|
||||
});
|
||||
|
||||
SerializeComponent<CircleCollider2DComponent>(writer, entity, "CircleCollider2D", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "Offset", component.Offset);
|
||||
Serialize.Value(writer, "Radius", component.Radius);
|
||||
|
||||
Serialize.Value(writer, "Density", component.Density);
|
||||
Serialize.Value(writer, "Friction", component.Friction);
|
||||
Serialize.Value(writer, "Restitution", component.Restitution);
|
||||
Serialize.Value(writer, "RestitutionThreshold", component.RestitutionThreshold);
|
||||
});
|
||||
}
|
||||
|
||||
writer.EntryEnd();
|
||||
@@ -297,6 +309,8 @@ namespace GlitchyEngine.World
|
||||
Try!(Deserialize.Value(reader, "Color", out component.Color));
|
||||
reader.EntryEnd();
|
||||
Try!(Deserialize.Value(reader, "UvTransform", out component.UvTransform));
|
||||
reader.EntryEnd();
|
||||
Try!(Deserialize.Value(reader, "IsCircle", out component.IsCircle));
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
@@ -419,6 +433,24 @@ namespace GlitchyEngine.World
|
||||
reader.EntryEnd();
|
||||
Deserialize.Value(reader, "RestitutionThreshold", out component.RestitutionThreshold);
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
case "CircleCollider2D":
|
||||
Try!(DeserializeComponent<CircleCollider2DComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
Deserialize.Value(reader, "Offset", out component.Offset);
|
||||
reader.EntryEnd();
|
||||
Deserialize.Value(reader, "Radius", out component.Radius);
|
||||
reader.EntryEnd();
|
||||
|
||||
Deserialize.Value(reader, "Density", out component.Density);
|
||||
reader.EntryEnd();
|
||||
Deserialize.Value(reader, "Friction", out component.Friction);
|
||||
reader.EntryEnd();
|
||||
Deserialize.Value(reader, "Restitution", out component.Restitution);
|
||||
reader.EntryEnd();
|
||||
Deserialize.Value(reader, "RestitutionThreshold", out component.RestitutionThreshold);
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user