Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Moved all bepu libraries into their own package #170

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions Stride.CommunityToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example09_Renderer", "examp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example10_StrideUI_DragAndDrop", "examples\code-only\Example10_StrideUI_DragAndDrop\Example10_StrideUI_DragAndDrop.csproj", "{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.CommunityToolkit.ImGuiDebug", "src\Stride.CommunityToolkit.ImGui\Stride.CommunityToolkit.ImGuiDebug.csproj", "{4F653FF3-F4E5-496F-A603-85355771F359}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.CommunityToolkit.ImGuiDebug", "src\Stride.CommunityToolkit.ImGui\Stride.CommunityToolkit.ImGuiDebug.csproj", "{43BB82C0-78B8-4DE9-8F14-0664973CCB7B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example11_ImGui", "examples\code-only\Example11_ImGui\Example11_ImGui\Example11_ImGui.csproj", "{1E4489E6-128D-45B0-86E2-3F5707A389BA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example11_ImGui", "examples\code-only\Example11_ImGui\Example11_ImGui\Example11_ImGui.csproj", "{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.CommunityToolkit.Bepu", "src\Stride.CommunityToolkit.Bepu\Stride.CommunityToolkit.Bepu.csproj", "{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -184,14 +186,18 @@ Global
{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803}.Release|Any CPU.Build.0 = Release|Any CPU
{4F653FF3-F4E5-496F-A603-85355771F359}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F653FF3-F4E5-496F-A603-85355771F359}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F653FF3-F4E5-496F-A603-85355771F359}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F653FF3-F4E5-496F-A603-85355771F359}.Release|Any CPU.Build.0 = Release|Any CPU
{1E4489E6-128D-45B0-86E2-3F5707A389BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E4489E6-128D-45B0-86E2-3F5707A389BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E4489E6-128D-45B0-86E2-3F5707A389BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E4489E6-128D-45B0-86E2-3F5707A389BA}.Release|Any CPU.Build.0 = Release|Any CPU
{43BB82C0-78B8-4DE9-8F14-0664973CCB7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43BB82C0-78B8-4DE9-8F14-0664973CCB7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43BB82C0-78B8-4DE9-8F14-0664973CCB7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43BB82C0-78B8-4DE9-8F14-0664973CCB7B}.Release|Any CPU.Build.0 = Release|Any CPU
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008}.Release|Any CPU.Build.0 = Release|Any CPU
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -217,7 +223,7 @@ Global
{DE8458AC-2F74-428E-808F-C39EA986D110} = {A9C673A6-1426-40DD-A060-B0749261DB7C}
{5996C17E-629C-4F58-88F9-494559EB4FBF} = {30457B66-9E10-4290-9744-D0079DE0448B}
{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803} = {30457B66-9E10-4290-9744-D0079DE0448B}
{1E4489E6-128D-45B0-86E2-3F5707A389BA} = {30457B66-9E10-4290-9744-D0079DE0448B}
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008} = {30457B66-9E10-4290-9744-D0079DE0448B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C7A8C18E-C1F5-492F-8173-51EC9495B78C}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>-->

<ItemGroup>
<ProjectReference Include="..\..\..\src\Stride.CommunityToolkit.Bepu\Stride.CommunityToolkit.Bepu.csproj" />
<ProjectReference Include="..\..\..\src\Stride.CommunityToolkit.Skyboxes\Stride.CommunityToolkit.Skyboxes.csproj" />
<ProjectReference Include="..\..\..\src\Stride.CommunityToolkit.Windows\Stride.CommunityToolkit.Windows.csproj" />
<ProjectReference Include="..\Example.Common\Example.Common.csproj" />
Expand Down
1 change: 1 addition & 0 deletions examples/code-only/Example_Bepu_Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Example.Common;
using Stride.BepuPhysics;
using Stride.CommunityToolkit.Bepu;
using Stride.CommunityToolkit.Bepu.Engine;
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.CommunityToolkit.Skyboxes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using BepuPhysics;
using BepuPhysics.Collidables;
using Stride.BepuPhysics;
using Stride.Core;
using Stride.Core.Mathematics;
using Stride.Engine;
using System.Reflection;

Expand Down
231 changes: 231 additions & 0 deletions src/Stride.CommunityToolkit.Bepu/Engine/BepuGameExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
using Stride.BepuPhysics.Definitions.Colliders;
using Stride.BepuPhysics;
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.Engine;
using Stride.CommunityToolkit.Rendering.Compositing;
using Stride.Games;
using Stride.Core.Mathematics;

namespace Stride.CommunityToolkit.Bepu.Engine;
public static class BepuGameExtensions
{
private const string DefaultGroundName = "Ground";
private const string GraphicsCompositorNotSet = "GraphicsCompositor is not set.";

private static readonly Vector2 _default3DGroundSize = new(15f);
private static readonly Vector3 _default2DGroundSize = new(15, 0.1f, 0);
private static readonly Color _defaultMaterialColor = Color.FromBgra(0xFF8C8C8C);
private static readonly Color _defaultGroundMaterialColor = Color.FromBgra(0xFF242424);

public static void SetupBase2DSceneWithBepu(this Game game)
{
game.AddGraphicsCompositor().AddCleanUIStage();
game.Add2DCamera().Add2DCameraController();
game.Add3DGroundWithBepu();
}

public static void SetupBase3DSceneWithBepu(this Game game)
{
game.AddGraphicsCompositor().AddCleanUIStage();
game.Add3DCamera().Add3DCameraController();
game.AddDirectionalLight();
game.Add3DGroundWithBepu();
}
public static Entity Add3DGroundWithBepu(this Game game, string? entityName = DefaultGroundName, Vector2? size = null, bool includeCollider = true)
=> CreateGroundWithBepu(game, entityName, size, includeCollider, PrimitiveModelType.Plane);


private static Entity CreateGroundWithBepu(Game game, string? entityName, Vector2? size, bool includeCollider, PrimitiveModelType type)
{
var validSize = size ?? _default3DGroundSize;

var material = game.CreateMaterial(_defaultGroundMaterialColor, 0.0f, 0.1f);

var entity = game.Create3DPrimitiveWithBepu(type, new Primitive3DCreationOptionsWithBepu()
{
EntityName = entityName,
Material = material,
IncludeCollider = includeCollider,
Size = (Vector3)validSize,
Component = new StaticComponent() { Collider = new CompoundCollider() }
});

entity.Scene = game.SceneSystem.SceneInstance.RootScene;

return entity;
}

public static Entity Create2DPrimitiveWithBepu(this IGame game, Primitive2DModelType type, Primitive2DCreationOptionsWithBepu? options = null)
{
options ??= new();

var modelBase = Procedural2DModelBuilder.Build(type, options.Size, options.Depth);

var model = modelBase.Generate(game.Services);

if (options.Material != null)
{
model.Materials.Add(options.Material);
}

var entity = new Entity(options.EntityName);

if (type == Primitive2DModelType.Circle)
{
var childEntity = new Entity("Child") { new ModelComponent(model) { RenderGroup = options.RenderGroup } };
childEntity.Transform.Rotation = Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(90));
entity.AddChild(childEntity);
}
else
{
entity.Add(new ModelComponent(model) { RenderGroup = options.RenderGroup });
}

if (!options.IncludeCollider) return entity;

if (type == Primitive2DModelType.Triangle)
{
// This is needed when using ConvexHullCollider
//var meshData = TriangularPrismProceduralModel.New(options.Size is null ? new(1, 1, options.Depth) : new(options.Size.Value.X, options.Size.Value.Y, options.Depth));

//var points = meshData.Vertices.Select(w => w.Position).ToList();
//var uintIndices = meshData.Indices.Select(w => (uint)w).ToList();
//var collider = new ConvexHullColliderShapeDesc()
//{
// Model = model, // seems doing nothing
// ConvexHulls = [],
// ConvexHullsIndices = []
//};

//collider.ConvexHulls.Add([points]);
//collider.ConvexHullsIndices.Add([uintIndices]);

//List<IAssetColliderShapeDesc> descriptions = [];

//descriptions.Add(collider);

//var collider2 = new ConvexHullCollider() { Hull = new PhysicsColliderShape(descriptions) };

//var compoundCollier = options.Component.Collider as CompoundCollider;

//compoundCollier.Colliders.Add(collider2);

// Or you can use just his
options.Component.Collider = new MeshCollider() { Model = model, Closed = true };

entity.Add(options.Component);
}
else
{
var colliderShape = Get2DColliderShapeWithBepu(type, options.Size, options.Depth);

if (colliderShape is null) return entity;

var compoundCollier = options.Component.Collider as CompoundCollider;

compoundCollier.Colliders.Add(colliderShape);

entity.Add(options.Component);
}

return entity;
}

public static Entity Create3DPrimitiveWithBepu(this IGame game, PrimitiveModelType type, Primitive3DCreationOptionsWithBepu? options = null)
{
options ??= new();

var modelBase = Procedural3DModelBuilder.Build(type, options.Size);

var model = modelBase.Generate(game.Services);

if (options.Material != null)
{
model.Materials.Add(options.Material);
}

var entity = new Entity(options.EntityName) { new ModelComponent(model) { RenderGroup = options.RenderGroup } };

if (!options.IncludeCollider) return entity;

if (type == PrimitiveModelType.TriangularPrism)
{
// This is needed when using ConvexHullCollider
//var meshData = TriangularPrismProceduralModel.New(options.Size is null ? new(1, 1, options.Depth) : new(options.Size.Value.X, options.Size.Value.Y, options.Depth));

//var points = meshData.Vertices.Select(w => w.Position).ToList();
//var uintIndices = meshData.Indices.Select(w => (uint)w).ToList();
//var collider = new ConvexHullColliderShapeDesc()
//{
// Model = model, // seems doing nothing
// ConvexHulls = [],
// ConvexHullsIndices = []
//};

//collider.ConvexHulls.Add([points]);
//collider.ConvexHullsIndices.Add([uintIndices]);

//List<IAssetColliderShapeDesc> descriptions = [];

//descriptions.Add(collider);

//var collider2 = new ConvexHullCollider() { Hull = new PhysicsColliderShape(descriptions) };

//var compoundCollier = options.Component.Collider as CompoundCollider;

//compoundCollier.Colliders.Add(collider2);

// Or you can use just his
options.Component.Collider = new MeshCollider() { Model = model, Closed = true };

entity.Add(options.Component);
}
else
{
var colliderShape = Get3DColliderShapeWithBepu(type, options.Size);

if (colliderShape is null) return entity;

var compoundCollier = options.Component.Collider as CompoundCollider;

compoundCollier.Colliders.Add(colliderShape);

entity.Add(options.Component);
}

return entity;
}

private static ColliderBase? Get2DColliderShapeWithBepu(Primitive2DModelType type, Vector2? size = null, float depth = 0)
{
return type switch
{
Primitive2DModelType.Rectangle => size is null ? new BoxCollider() : new() { Size = new(size.Value.X, size.Value.Y, depth) },
Primitive2DModelType.Square => size is null ? new BoxCollider() : new() { Size = new(size.Value.X, size.Value.Y, depth) },
Primitive2DModelType.Circle => size is null ? new CylinderCollider() : new()
{
Radius = size.Value.X,
Length = depth,
RotationLocal = Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(90))
},
//Primitive2DModelType.Triangle => triangleCollider ?? new TriangleCollider(),
_ => throw new InvalidOperationException(),
};
}

private static ColliderBase? Get3DColliderShapeWithBepu(PrimitiveModelType type, Vector3? size = null)
=> type switch
{
PrimitiveModelType.Plane => size is null ? new BoxCollider() : new() { Size = new Vector3(size.Value.X, 0, size.Value.Y) },
PrimitiveModelType.Cube => size is null ? new BoxCollider() : new() { Size = size ?? Vector3.One },
PrimitiveModelType.RectangularPrism => size is null ? new BoxCollider() : new() { Size = size ?? Vector3.One },
PrimitiveModelType.Cylinder => size is null ? new CylinderCollider() : new()
{
Radius = size.Value.X,
Length = size.Value.Z,
//RotationLocal = Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(90))
},
_ => throw new InvalidOperationException(),
};
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Definitions.Colliders;
using Stride.CommunityToolkit.Bepu;

namespace Stride.CommunityToolkit.Engine;
using Stride.BepuPhysics;
using Stride.CommunityToolkit.Engine;
using Stride.Core.Mathematics;

namespace Stride.CommunityToolkit.Bepu.Engine;
public class Primitive2DCreationOptionsWithBepu : PrimitiveCreationOptions
{
/// <summary>
Expand All @@ -17,4 +17,4 @@ public class Primitive2DCreationOptionsWithBepu : PrimitiveCreationOptions
/// Gets or sets the physics component to be added to the entity.
/// </summary>
public ContainerComponent Component { get; set; } = new Body2DComponent() { Collider = new CompoundCollider() };
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Definitions.Colliders;
using Stride.BepuPhysics;
using Stride.CommunityToolkit.Engine;
using Stride.Core.Mathematics;

namespace Stride.CommunityToolkit.Engine;

namespace Stride.CommunityToolkit.Bepu.Engine;
/// <summary>
/// Provides options for creating a primitive entity in a 3D scene.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Components;
using Stride.Core.Mathematics;
using Stride.Engine;

namespace Stride.CommunityToolkit.Bepu;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Stride.BepuPhysics" Version="0.9.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Stride.CommunityToolkit\Stride.CommunityToolkit.csproj" />
</ItemGroup>

</Project>
Loading