mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
2D Physics
This commit is contained in:
@@ -25,3 +25,6 @@
|
||||
[submodule "GlitchyEngineHelper/vendor/DirectXTK"]
|
||||
path = GlitchyEngineHelper/vendor/DirectXTK
|
||||
url = https://github.com/microsoft/DirectXTK.git
|
||||
[submodule "GlitchyEngine/vendor/box2D"]
|
||||
path = GlitchyEngine/vendor/box2D
|
||||
url = https://github.com/jazzbre/box2d-beef.git
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
FileVersion = 1
|
||||
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}, msdfgen-beef = {Path = "GlitchyEngine/vendor/msdfgen/msdfgen-beef"}, ImGui = {Path = "GlitchyEngine/vendor/imgui/ImGui"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplDX11"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplWin32"}, ImGuizmo = {Path = "GlitchyEngine/vendor/imgui/ImGuizmo"}, GlitchyEngineHelper = {Path = "GlitchyEngineHelper"}, bon = {Path = "GlitchyEngine/vendor/bon"}}
|
||||
WorkspaceFolders = {GlitchyEngine = ["GlitchyEngine", "GlitchLog", "GlitchyEngineHelper"], "GlitchyEngine/Dependencies" = ["cgltf-beef", "DirectX", "FreeType", "ImGui", "ImGuiImplDX11", "ImGuiImplWin32", "ImGuizmo", "LodePng", "msdfgen-beef", "bon"]}
|
||||
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}, msdfgen-beef = {Path = "GlitchyEngine/vendor/msdfgen/msdfgen-beef"}, ImGui = {Path = "GlitchyEngine/vendor/imgui/ImGui"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplDX11"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplWin32"}, ImGuizmo = {Path = "GlitchyEngine/vendor/imgui/ImGuizmo"}, GlitchyEngineHelper = {Path = "GlitchyEngineHelper"}, bon = {Path = "GlitchyEngine/vendor/bon"}, box2d-beef = {Path = "GlitchyEngine/vendor/box2D"}}
|
||||
WorkspaceFolders = {GlitchyEngine = ["GlitchyEngine", "GlitchLog", "GlitchyEngineHelper"], "GlitchyEngine/Dependencies" = ["cgltf-beef", "DirectX", "FreeType", "ImGui", "ImGuiImplDX11", "ImGuiImplWin32", "ImGuizmo", "LodePng", "msdfgen-beef", "bon", "box2d-beef"]}
|
||||
|
||||
[Workspace]
|
||||
StartupProject = "GlitchyEditor"
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
{
|
||||
Name = "Scene name here pls!!!",
|
||||
Entities = [
|
||||
{
|
||||
Id = 0,
|
||||
NameComponent = {
|
||||
Name = "Quad"
|
||||
},
|
||||
SpriterRendererComponent = {
|
||||
Color = {
|
||||
R = 0,
|
||||
G = 0.551178,
|
||||
B = 0.328787,
|
||||
A = 1
|
||||
},
|
||||
UvTransform = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 1,
|
||||
W = 1
|
||||
}
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = 0,
|
||||
Y = 2,
|
||||
Z = 0
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0,
|
||||
W = 1
|
||||
},
|
||||
Scale = {
|
||||
X = 1,
|
||||
Y = 1,
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0
|
||||
}
|
||||
},
|
||||
Rigidbody2D = {
|
||||
BodyType = .Dynamic,
|
||||
FixedRotation = false
|
||||
},
|
||||
BoxCollider2D = {
|
||||
Offset = {
|
||||
X = 0,
|
||||
Y = 0
|
||||
},
|
||||
Size = {
|
||||
X = 0.5,
|
||||
Y = 0.5
|
||||
},
|
||||
Density = 1,
|
||||
Friction = 0.5,
|
||||
Restitution = 0,
|
||||
RestitutionThreshold = 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
Id = 1,
|
||||
NameComponent = {
|
||||
Name = "Floor"
|
||||
},
|
||||
SpriterRendererComponent = {
|
||||
Color = {
|
||||
R = 1,
|
||||
G = 0.941886,
|
||||
B = 0.401485,
|
||||
A = 1
|
||||
},
|
||||
UvTransform = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 1,
|
||||
W = 1
|
||||
}
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = 0,
|
||||
Y = -0.5,
|
||||
Z = 0
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0,
|
||||
W = 1
|
||||
},
|
||||
Scale = {
|
||||
X = 10,
|
||||
Y = 1,
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0
|
||||
}
|
||||
},
|
||||
Rigidbody2D = {
|
||||
BodyType = .Static,
|
||||
FixedRotation = false
|
||||
},
|
||||
BoxCollider2D = {
|
||||
Offset = {
|
||||
X = 0,
|
||||
Y = 0
|
||||
},
|
||||
Size = {
|
||||
X = 0.5,
|
||||
Y = 0.5
|
||||
},
|
||||
Density = 1,
|
||||
Friction = 0.5,
|
||||
Restitution = 0,
|
||||
RestitutionThreshold = 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
Id = 2,
|
||||
NameComponent = {
|
||||
Name = "Camera"
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = 0,
|
||||
Y = 1,
|
||||
Z = -5
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0,
|
||||
W = 1
|
||||
},
|
||||
Scale = {
|
||||
X = 1,
|
||||
Y = 1,
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0
|
||||
}
|
||||
},
|
||||
CameraComponent = {
|
||||
Primary = true,
|
||||
ProjectionType = .InfinitePerspective,
|
||||
PerspectiveFovY = 1.308997,
|
||||
PerspectiveNearPlane = 0.1,
|
||||
PerspectiveFarPlane = 10000,
|
||||
OrthographicHeight = 10,
|
||||
OrthographicNearPlane = 0,
|
||||
OrthographicFarPlane = 10,
|
||||
AspectRatio = 2.329843,
|
||||
FixedAspectRatio = false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -64,6 +64,8 @@ namespace GlitchyEditor.EditWindows
|
||||
ShowComponentEditor<MeshRendererComponent>("Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu<MeshRendererComponent>);
|
||||
ShowComponentEditor<LightComponent>("Light", entity, => ShowLightComponentEditor, => ShowComponentContextMenu<LightComponent>);
|
||||
ShowComponentEditor<MeshComponent>("Mesh", entity, => ShowMeshComponentEditor, => ShowComponentContextMenu<MeshComponent>);
|
||||
ShowComponentEditor<Rigidbody2DComponent>("Rigidbody 2D", entity, => ShowRigidBody2DComponentEditor, => ShowComponentContextMenu<Rigidbody2DComponent>);
|
||||
ShowComponentEditor<BoxCollider2DComponent>("Box collider 2D", entity, => ShowBoxCollider2DComponentEditor, => ShowComponentContextMenu<BoxCollider2DComponent>);
|
||||
|
||||
ShowAddComponentButton(entity);
|
||||
}
|
||||
@@ -411,6 +413,64 @@ namespace GlitchyEditor.EditWindows
|
||||
}
|
||||
}
|
||||
|
||||
private static void ShowRigidBody2DComponentEditor(Entity entity, Rigidbody2DComponent* rigidBodyComponent)
|
||||
{
|
||||
const String[?] bodyTypeStrings = .("Static", "Dynamic", "Kinematic");
|
||||
String bodyTypeName = bodyTypeStrings[rigidBodyComponent.BodyType.Underlying];
|
||||
|
||||
if (ImGui.BeginCombo("Type", bodyTypeName.CStr()))
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
bool isSelected = (bodyTypeName == bodyTypeStrings[i]);
|
||||
|
||||
if (ImGui.Selectable(bodyTypeStrings[i], isSelected))
|
||||
{
|
||||
rigidBodyComponent.BodyType = (.)i;
|
||||
}
|
||||
|
||||
if (isSelected)
|
||||
ImGui.SetItemDefaultFocus();
|
||||
}
|
||||
|
||||
ImGui.EndCombo();
|
||||
}
|
||||
|
||||
|
||||
ImGui.Checkbox("Fixed Rotation", &rigidBodyComponent.FixedRotation);
|
||||
}
|
||||
|
||||
private static void ShowBoxCollider2DComponentEditor(Entity entity, BoxCollider2DComponent* boxCollider)
|
||||
{
|
||||
float textWidth = ImGui.CalcTextSize("Offset".CStr()).x;
|
||||
textWidth += ImGui.GetStyle().FramePadding.x * 3.0f;
|
||||
|
||||
|
||||
Vector2 offset = boxCollider.Offset;
|
||||
if (ImGui.EditVector2("Offset", ref offset, .Zero, 0.1f, textWidth))
|
||||
boxCollider.Offset = offset;
|
||||
|
||||
Vector2 size = boxCollider.Size;
|
||||
if (ImGui.EditVector2("Size", ref size, .Zero, 0.1f, textWidth))
|
||||
boxCollider.Size = size;
|
||||
|
||||
float density = boxCollider.Density;
|
||||
if (ImGui.DragFloat("Density", &density, 0.0f, 0.1f, textWidth))
|
||||
boxCollider.Density = density;
|
||||
|
||||
float friction = boxCollider.Friction;
|
||||
if (ImGui.DragFloat("Friction", &friction, 0.0f, 0.1f, textWidth))
|
||||
boxCollider.Friction = friction;
|
||||
|
||||
float restitution = boxCollider.Restitution;
|
||||
if (ImGui.DragFloat("Restitution", &restitution, 0.0f, 0.1f, textWidth))
|
||||
boxCollider.Restitution = restitution;
|
||||
|
||||
float restitutionThreshold = boxCollider.RestitutionThreshold;
|
||||
if (ImGui.DragFloat("RestitutionThreshold", &restitutionThreshold, 0.0f, 0.1f, textWidth))
|
||||
boxCollider.RestitutionThreshold = restitutionThreshold;
|
||||
}
|
||||
|
||||
private static void LabelColumn(StringView label)
|
||||
{
|
||||
ImGui.TextUnformatted(label);
|
||||
@@ -517,6 +577,10 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
void ShowComponentButton<TComponent>(String name) where TComponent : struct, new
|
||||
{
|
||||
// If the entity already has this component, don't show the option to add it
|
||||
if (entity.HasComponent<TComponent>())
|
||||
return;
|
||||
|
||||
float textWidth = ImGui.CalcTextSize(name.CStr()).x;
|
||||
buttonWidth = Math.Max(buttonWidth, textWidth + ImGui.GetStyle().FramePadding.x * 2);
|
||||
|
||||
@@ -547,6 +611,8 @@ namespace GlitchyEditor.EditWindows
|
||||
ShowComponentButton<CameraComponent>("Camera");
|
||||
ShowComponentButton<SpriterRendererComponent>("Sprite Renderer");
|
||||
ShowComponentButton<LightComponent>("Light");
|
||||
ShowComponentButton<Rigidbody2DComponent>("Rigidbody 2D");
|
||||
ShowComponentButton<BoxCollider2DComponent>("Box collider 2D");
|
||||
|
||||
ImGui.EndCombo();
|
||||
}
|
||||
|
||||
@@ -310,12 +310,16 @@ namespace GlitchyEditor
|
||||
{
|
||||
_sceneState = .Play;
|
||||
_editor.SceneViewportWindow.EditorMode = false;
|
||||
|
||||
_scene.OnRuntimeStart();
|
||||
}
|
||||
|
||||
private void OnSceneStop()
|
||||
{
|
||||
_sceneState = .Edit;
|
||||
_scene.OnRuntimeStop();
|
||||
|
||||
_editor.SceneViewportWindow.EditorMode = true;
|
||||
_sceneState = .Edit;
|
||||
}
|
||||
|
||||
// Just for testing
|
||||
@@ -459,19 +463,26 @@ namespace GlitchyEditor
|
||||
_camera.Position = .(-1.5f, 1.5f, -2.5f);
|
||||
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
|
||||
|
||||
/*// Create default camera
|
||||
{
|
||||
let camEntity = _scene.CreateEntity("Camera");
|
||||
let transform = camEntity.Transform;
|
||||
transform.Position =
|
||||
}
|
||||
|
||||
// Create the default light source
|
||||
{
|
||||
var lightNtt = _scene.CreateEntity("Light");
|
||||
let transform = lightNtt.GetComponent<TransformComponent>();
|
||||
let lightNtt = _scene.CreateEntity("Light");
|
||||
let transform = lightNtt.Transform;
|
||||
transform.Position = .(0, 0, 0);
|
||||
transform.RotationEuler = .(MathHelper.ToRadians(45), MathHelper.ToRadians(-100), 0);
|
||||
|
||||
let light = lightNtt.AddComponent<LightComponent>();
|
||||
light.SceneLight.Illuminance = 10.0f;
|
||||
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
|
||||
}
|
||||
}*/
|
||||
|
||||
TestEntitiesWithModels();
|
||||
//TestEntitiesWithModels();
|
||||
}
|
||||
|
||||
/// Saves the scene in the file that is was loaded from or saved to last. If there is no such path (i.e. it is a new scene) the save file dialog will open.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
FileVersion = 1
|
||||
Dependencies = {GlitchLog = "*", corlib = "*", DirectX = "*", FreeType = "*", cgltf-beef = "*", msdfgen-beef = "*", ImGui = "*", ImGuiImplDX11 = "*", ImGuiImplWin32 = "*", ImGuizmo = "*", Beefy2D = "*", LodePng = "*", GlitchyEngineHelper = "*", bon = "*"}
|
||||
Dependencies = {GlitchLog = "*", corlib = "*", DirectX = "*", FreeType = "*", cgltf-beef = "*", msdfgen-beef = "*", ImGui = "*", ImGuiImplDX11 = "*", ImGuiImplWin32 = "*", ImGuizmo = "*", Beefy2D = "*", LodePng = "*", GlitchyEngineHelper = "*", bon = "*", box2d-beef = "*"}
|
||||
|
||||
[Project]
|
||||
Name = "GlitchyEngine"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Bon;
|
||||
|
||||
using internal GlitchyEngine.Math;
|
||||
|
||||
@@ -13,6 +14,7 @@ namespace GlitchyEngine.Math
|
||||
/**
|
||||
Represents a four component floating point color
|
||||
*/
|
||||
[BonTarget]
|
||||
[CRepr]
|
||||
struct ColorRGBA
|
||||
{
|
||||
|
||||
@@ -337,4 +337,29 @@ namespace GlitchyEngine.World
|
||||
{
|
||||
public SceneLight SceneLight;
|
||||
}
|
||||
|
||||
struct Rigidbody2DComponent
|
||||
{
|
||||
public enum BodyType { Static = 0, Dynamic = 1, Kinematic = 2 }
|
||||
|
||||
public BodyType BodyType = .Static;
|
||||
|
||||
public bool FixedRotation = false;
|
||||
|
||||
internal int _runtimeBody = 0;
|
||||
}
|
||||
|
||||
struct BoxCollider2DComponent
|
||||
{
|
||||
public Vector2 Offset = .(0.0f, 0.0f);
|
||||
public Vector2 Size = .(0.5f, 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;
|
||||
|
||||
internal int _runtimeFixture = 0;
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,8 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
}
|
||||
|
||||
public TransformComponent* Transform => GetComponent<TransformComponent>();
|
||||
|
||||
public T* AddComponent<T>(T value = T()) where T: struct, new
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(!HasComponent<T>(), scope $"Entity already has component.");
|
||||
@@ -86,6 +88,18 @@ namespace GlitchyEngine.World
|
||||
return _scene._ecsWorld.HasComponent<T>(_entity);
|
||||
}
|
||||
|
||||
public bool TryGetComponent<T>(out T* component) where T: struct, new
|
||||
{
|
||||
if (HasComponent<T>())
|
||||
{
|
||||
component = GetComponent<T>();
|
||||
return true;
|
||||
}
|
||||
|
||||
component = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RemoveComponent<T>() where T: struct, new
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(HasComponent<T>(), "Entity doesn't have component!");
|
||||
|
||||
@@ -2,19 +2,26 @@ using GlitchyEngine.Math;
|
||||
using GlitchyEngine.Renderer;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Box2D;
|
||||
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
using internal ScriptableEntity;
|
||||
using internal GlitchyEngine.World;
|
||||
|
||||
class Scene
|
||||
{
|
||||
internal EcsWorld _ecsWorld = new .() ~ delete _;
|
||||
|
||||
internal b2World* _physicsWorld2D;
|
||||
|
||||
private Dictionary<Type, function void(Entity entity, Type componentType, void* component)> _onComponentAddedHandlers = new .() ~ delete _;
|
||||
|
||||
private RenderTargetGroup _compositeTarget ~ _.ReleaseRef();
|
||||
|
||||
// Temporary target for camera. Needs to change as soon as we support multiple cameras
|
||||
private RenderTargetGroup _cameraTarget ~ _.ReleaseRef();
|
||||
|
||||
private Effect _gammaCorrectEffect ~ _.ReleaseRef();
|
||||
|
||||
public Entity ActiveCamera => {
|
||||
@@ -54,6 +61,16 @@ namespace GlitchyEngine.World
|
||||
RenderTargetFormat.D24_UNorm_S8_UInt);
|
||||
_compositeTarget = new RenderTargetGroup(desc);
|
||||
|
||||
_cameraTarget = new RenderTargetGroup(.(){
|
||||
Width = 100,
|
||||
Height = 100,
|
||||
ColorTargetDescriptions = TargetDescription[](
|
||||
.(.R16G16B16A16_Float),
|
||||
.(.R32_UInt)
|
||||
),
|
||||
DepthTargetDescription = .(.D24_UNorm_S8_UInt)
|
||||
});
|
||||
|
||||
_gammaCorrectEffect = Application.Get().EffectLibrary.Load("content/Shaders/GammaCorrect.hlsl");
|
||||
}
|
||||
|
||||
@@ -61,6 +78,71 @@ namespace GlitchyEngine.World
|
||||
{
|
||||
}
|
||||
|
||||
b2Vec2 _gravity2D = .(0.0f, -9.8f);
|
||||
|
||||
static b2BodyType GetBox2DBodyType(Rigidbody2DComponent.BodyType bodyType)
|
||||
{
|
||||
switch (bodyType)
|
||||
{
|
||||
case .Static:
|
||||
return .b2_staticBody;
|
||||
case .Dynamic:
|
||||
return .b2_dynamicBody;
|
||||
case .Kinematic:
|
||||
return .b2_kinematicBody;
|
||||
default:
|
||||
Log.EngineLogger.AssertDebug(false, "Unknown body type");
|
||||
return .b2_staticBody;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnRuntimeStart()
|
||||
{
|
||||
_physicsWorld2D = Box2D.World.Create(ref _gravity2D);
|
||||
|
||||
for (var entry in _ecsWorld.Enumerate<Rigidbody2DComponent>())
|
||||
{
|
||||
Entity entity = .(entry.Entity, this);
|
||||
|
||||
var transform = entity.Transform;
|
||||
var rigidBody = entry.Component;
|
||||
|
||||
b2BodyDef def = .();
|
||||
def.type = GetBox2DBodyType(rigidBody.BodyType);
|
||||
|
||||
// TODO: breaks with hierarchy
|
||||
def.position = b2Vec2(transform.Position.X, transform.Position.Y);
|
||||
def.angle = transform.RotationEuler.Z;
|
||||
|
||||
b2Body* body = Box2D.World.CreateBody(_physicsWorld2D, &def);
|
||||
Box2D.Body.SetFixedRotation(body, rigidBody.FixedRotation);
|
||||
|
||||
rigidBody._runtimeBody = (int)(void*)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);
|
||||
|
||||
b2FixtureDef fixtureDef = .();
|
||||
fixtureDef.shape = boxShape;
|
||||
fixtureDef.density = boxCollider.Density;
|
||||
fixtureDef.friction = boxCollider.Friction;
|
||||
fixtureDef.restitution = boxCollider.Restitution;
|
||||
fixtureDef.restitutionThreshold = boxCollider.RestitutionThreshold;
|
||||
|
||||
b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef);
|
||||
boxCollider._runtimeFixture = (int)(void*)fixture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnRuntimeStop()
|
||||
{
|
||||
Box2D.World.Delete(_physicsWorld2D);
|
||||
_physicsWorld2D = null;
|
||||
}
|
||||
|
||||
public void UpdateRuntime(GameTime gameTime, RenderTargetGroup finalTarget)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
@@ -69,6 +151,44 @@ namespace GlitchyEngine.World
|
||||
|
||||
TransformSystem.Update(_ecsWorld);
|
||||
|
||||
// Run scripts
|
||||
for (var (entity, script) in _ecsWorld.Enumerate<NativeScriptComponent>())
|
||||
{
|
||||
if (script.Instance == null)
|
||||
{
|
||||
script.Instance = script.InstantiateFunction();
|
||||
script.Instance._entity = Entity(entity, this);
|
||||
script.Instance.[Friend]OnCreate();
|
||||
}
|
||||
|
||||
script.Instance.[Friend]OnUpdate(gameTime);
|
||||
}
|
||||
|
||||
// Update 2D physics
|
||||
{
|
||||
const int32 velocityIterations = 6;
|
||||
const int32 positionIterations = 2;
|
||||
const int32 particleIterations = 2;
|
||||
|
||||
Box2D.World.Step(_physicsWorld2D, gameTime.DeltaTime, velocityIterations, positionIterations, particleIterations);
|
||||
|
||||
// Retrieve transform from Box2D
|
||||
for (var entry in _ecsWorld.Enumerate<Rigidbody2DComponent>())
|
||||
{
|
||||
Entity entity = .(entry.Entity, this);
|
||||
|
||||
var transform = entity.Transform;
|
||||
var rigidbody = entry.Component;
|
||||
|
||||
b2Body* body = (b2Body*)(void*)rigidbody._runtimeBody;
|
||||
b2Vec2 position = Box2D.Body.GetPosition(body);
|
||||
float angle = Box2D.Body.GetAngle(body);
|
||||
|
||||
transform.Position = .(position.x, position.y, transform.Position.Z);
|
||||
transform.RotationEuler = .(transform.RotationEuler.XY, angle);
|
||||
}
|
||||
}
|
||||
|
||||
// Find camera
|
||||
Camera* primaryCamera = null;
|
||||
Matrix primaryCameraTransform = default;
|
||||
@@ -80,7 +200,8 @@ namespace GlitchyEngine.World
|
||||
{
|
||||
primaryCamera = &camera.Camera;
|
||||
primaryCameraTransform = transform.WorldTransform;
|
||||
renderTarget = camera.RenderTarget..AddRef();
|
||||
// TODO: bind render targets to cameras
|
||||
// renderTarget = camera.RenderTarget..AddRef();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,6 +513,7 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
|
||||
_compositeTarget.Resize(ViewportWidth, ViewportHeight);
|
||||
_cameraTarget.Resize(ViewportWidth, ViewportHeight);
|
||||
}
|
||||
|
||||
private void OnComponentAdded(Entity entity, Type componentType, void* component)
|
||||
|
||||
@@ -81,6 +81,7 @@ namespace GlitchyEngine.World
|
||||
// TODO: Texture
|
||||
|
||||
Serialize.Value(writer, "Color", component.Color);
|
||||
Serialize.Value(writer, "UvTransform", component.UvTransform);
|
||||
});
|
||||
|
||||
SerializeComponent<TransformComponent>(writer, entity, "TransformComponent", scope (component) =>
|
||||
@@ -128,6 +129,24 @@ namespace GlitchyEngine.World
|
||||
|
||||
Serialize.Value(writer, "Color", light.Color);
|
||||
});
|
||||
|
||||
SerializeComponent<Rigidbody2DComponent>(writer, entity, "Rigidbody2D", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "BodyType", component.BodyType);
|
||||
|
||||
Serialize.Value(writer, "FixedRotation", component.FixedRotation);
|
||||
});
|
||||
|
||||
SerializeComponent<BoxCollider2DComponent>(writer, entity, "BoxCollider2D", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "Offset", component.Offset);
|
||||
Serialize.Value(writer, "Size", component.Size);
|
||||
|
||||
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();
|
||||
@@ -252,6 +271,8 @@ namespace GlitchyEngine.World
|
||||
// TODO: Texture
|
||||
|
||||
Try!(Deserialize.Value(reader, "Color", out component.Color));
|
||||
reader.EntryEnd();
|
||||
Try!(Deserialize.Value(reader, "UvTransform", out component.UvTransform));
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
@@ -337,9 +358,38 @@ namespace GlitchyEngine.World
|
||||
Deserialize.Value(reader, "Color", out light.[Friend]_color);
|
||||
return .Ok;
|
||||
}));
|
||||
case "Rigidbody2D":
|
||||
Try!(DeserializeComponent<Rigidbody2DComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
Deserialize.Value(reader, "BodyType", out component.BodyType);
|
||||
reader.EntryEnd();
|
||||
|
||||
Deserialize.Value(reader, "FixedRotation", out component.FixedRotation);
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
case "BoxCollider2D":
|
||||
Try!(DeserializeComponent<BoxCollider2DComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
Deserialize.Value(reader, "Offset", out component.Offset);
|
||||
reader.EntryEnd();
|
||||
Deserialize.Value(reader, "Size", out component.Size);
|
||||
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;
|
||||
}));
|
||||
|
||||
default:
|
||||
return .Err;
|
||||
Log.EngineLogger.AssertDebug(false, "Unknown component type");
|
||||
//return .Err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
Submodule GlitchyEngine/vendor/box2D added at 5990578fdc
Reference in New Issue
Block a user