Skip to content

Commit

Permalink
Merge pull request #116 from VaclavElias/main
Browse files Browse the repository at this point in the history
refactor: Bepu references corrected and other updates
  • Loading branch information
VaclavElias authored Mar 2, 2024
2 parents 3613651 + 5c3fd21 commit a7e6db7
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 175 deletions.
6 changes: 0 additions & 6 deletions Stride.CommunityToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Common", "examples\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.CommunityToolkit.Skyboxes", "src\Stride.CommunityToolkit.Skyboxes\Stride.CommunityToolkit.Skyboxes.csproj", "{423A1266-4A6D-425F-96CE-945A1802F711}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.BepuPhysics._2D", "src\Stride.BepuPhysics._2D\Stride.BepuPhysics._2D.csproj", "{EAD3CB11-EE8E-4C66-9A7B-CE0EC230CC25}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -135,10 +133,6 @@ Global
{423A1266-4A6D-425F-96CE-945A1802F711}.Debug|Any CPU.Build.0 = Debug|Any CPU
{423A1266-4A6D-425F-96CE-945A1802F711}.Release|Any CPU.ActiveCfg = Release|Any CPU
{423A1266-4A6D-425F-96CE-945A1802F711}.Release|Any CPU.Build.0 = Release|Any CPU
{EAD3CB11-EE8E-4C66-9A7B-CE0EC230CC25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAD3CB11-EE8E-4C66-9A7B-CE0EC230CC25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAD3CB11-EE8E-4C66-9A7B-CE0EC230CC25}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EAD3CB11-EE8E-4C66-9A7B-CE0EC230CC25}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 1 addition & 23 deletions examples/code-only/Example01_Basic3DScene/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,17 @@
using Stride.CommunityToolkit.Skyboxes;
using Stride.Core.Mathematics;
using Stride.Engine;
using Stride.Games;
using Stride.Graphics;

using var game = new Game();

SpriteBatch? spriteBatch = null;

SpriteFont? font = null;

game.Run(start: (Scene rootScene) =>
{
game.SetupBase3DScene();
game.AddSkybox();

font = game.Content.Load<SpriteFont>("StrideDefaultFont");
spriteBatch = new SpriteBatch(game.GraphicsDevice);

var entity = game.Create3DPrimitive(PrimitiveModelType.Capsule);

entity.Transform.Position = new Vector3(0, 8, 0);

entity.Scene = rootScene;
}, update: Update);

void Update(Scene scene, GameTime time)
{

// don't forget the begin
spriteBatch.Begin(game.GraphicsContext);

// draw the text "Helloworld!" in red from the center of the screen
spriteBatch.DrawString(font, "Helloworld!", new Vector2(0.5f, 0.5f), Color.Red);

// don't forget the end
spriteBatch.End();
}
});
2 changes: 1 addition & 1 deletion examples/code-only/Example_Bepu_Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Example.Common;
using Stride.BepuPhysics;
using Stride.BepuPhysics._2D;
using Stride.CommunityToolkit.Bepu;
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.CommunityToolkit.Skyboxes;
Expand Down
69 changes: 69 additions & 0 deletions src/Stride.CommunityToolkit/Bepu/Body2DComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using BepuPhysics;
using BepuPhysics.Collidables;
using Stride.BepuPhysics;
using Stride.Engine;
using System.Reflection;

namespace Stride.CommunityToolkit.Bepu;

// ToDo Remove this class once it is implemented in Stride
[ComponentCategory("Bepu")]
public class Body2DComponent : BodyComponent
{
Vector3 _rotationLock = new Vector3(0, 0, 0);

[DataMemberIgnore]
internal Vector3 RotationLock
{
get
{
return _rotationLock;
}
set
{
_rotationLock = value;
AccessBodyReference(value);
//if (BodyReference is { } bRef)
//{
// bRef.LocalInertia.InverseInertiaTensor.XX *= value.X;
// bRef.LocalInertia.InverseInertiaTensor.YX *= value.X * value.Y;
// bRef.LocalInertia.InverseInertiaTensor.ZX *= value.Z * value.X;
// bRef.LocalInertia.InverseInertiaTensor.YY *= value.Y;
// bRef.LocalInertia.InverseInertiaTensor.ZY *= value.Z * value.Y;
// bRef.LocalInertia.InverseInertiaTensor.ZZ *= value.Z;
//}
}
}

protected override void AttachInner(RigidPose containerPose, BodyInertia shapeInertia, TypedIndex shapeIndex)
{
base.AttachInner(containerPose, shapeInertia, shapeIndex);
#warning what about a body that become kinematic after some time ?

Check warning on line 41 in src/Stride.CommunityToolkit/Bepu/Body2DComponent.cs

View workflow job for this annotation

GitHub Actions / build

#warning: 'what about a body that become kinematic after some time ?'

Check warning on line 41 in src/Stride.CommunityToolkit/Bepu/Body2DComponent.cs

View workflow job for this annotation

GitHub Actions / build

#warning: 'what about a body that become kinematic after some time ?'

Check warning on line 41 in src/Stride.CommunityToolkit/Bepu/Body2DComponent.cs

View workflow job for this annotation

GitHub Actions / build

#warning: 'what about a body that become kinematic after some time ?'
if (!Kinematic)
RotationLock = new Vector3(0, 0, 1);
}

public void AccessBodyReference(Vector3 value)
{
// Get the type of the BodyComponent to access its members
var bodyComponentType = typeof(BodyComponent);

var bodyReferenceProperty = bodyComponentType.GetProperty("BodyReference", BindingFlags.NonPublic | BindingFlags.Instance);

if (bodyReferenceProperty != null)
{
// Get the value of 'BodyReference' property for 'this' instance
var bodyReferenceValue = bodyReferenceProperty.GetValue(this);

if (bodyReferenceValue is BodyReference bRef)
{
bRef.LocalInertia.InverseInertiaTensor.XX *= value.X;
bRef.LocalInertia.InverseInertiaTensor.YX *= value.X * value.Y;
bRef.LocalInertia.InverseInertiaTensor.ZX *= value.Z * value.X;
bRef.LocalInertia.InverseInertiaTensor.YY *= value.Y;
bRef.LocalInertia.InverseInertiaTensor.ZY *= value.Z * value.Y;
bRef.LocalInertia.InverseInertiaTensor.ZZ *= value.Z;
}
}
}
}
52 changes: 52 additions & 0 deletions src/Stride.CommunityToolkit/Bepu/Simulation2DComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Components;
using Stride.Engine;

namespace Stride.CommunityToolkit.Bepu;

// ToDo Remove this class once it is implemented in Stride
[ComponentCategory("Bepu")]
public class Simulation2DComponent : SimulationUpdateComponent
{
//public float MaxZLiberty { get; set; } = 0.05f;

public override void SimulationUpdate(float simTimeStep)
{

}

public override void AfterSimulationUpdate(float simTimeStep)
{
if (BepuSimulation == null)
return;

for (int i = 0; i < BepuSimulation.Simulation.Bodies.ActiveSet.Count; i++)
{
var handle = BepuSimulation.Simulation.Bodies.ActiveSet.IndexToHandle[i];
var body = BepuSimulation.GetContainer(handle);

if (body is not Body2DComponent)
continue;

//if (body.Position.Z > MaxZLiberty || body.Position.Z < -MaxZLiberty)
if (body.Position.Z != 0)
body.Position *= new Vector3(1, 1, 0);//Fix Z = 0
//if (body.LinearVelocity.Z > MaxZLiberty || body.LinearVelocity.Z < -MaxZLiberty)
if (body.LinearVelocity.Z != 0)
body.LinearVelocity *= new Vector3(1, 1, 0);

var bodyRot = body.Orientation;
Quaternion.RotationYawPitchRoll(ref bodyRot, out var yaw, out var pitch, out var roll);
//if (yaw > MaxZLiberty || pitch > MaxZLiberty || yaw < -MaxZLiberty || pitch < -MaxZLiberty)
if (yaw != 0 || pitch != 0)
body.Orientation = Quaternion.RotationYawPitchRoll(0, 0, roll);
//if (body.AngularVelocity.X > MaxZLiberty || body.AngularVelocity.Y > MaxZLiberty || body.AngularVelocity.X < -MaxZLiberty || body.AngularVelocity.Y < -MaxZLiberty)
if (body.AngularVelocity.X != 0 || body.AngularVelocity.Y != 0)
body.AngularVelocity *= new Vector3(0, 0, 1);
}
}

public override void Update()
{
}
}
11 changes: 6 additions & 5 deletions src/Stride.CommunityToolkit/Engine/GameExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public static Entity Create3DPrimitive(this IGame game, PrimitiveModelType type,

if (!options.IncludeCollider || options.PhysicsComponent is null) return entity;

var colliderShape = Get3DColliderShape(type, options.Size, options.Is2D);
var colliderShape = Get3DColliderShape(type, options.Size);

if (colliderShape is null) return entity;

Expand Down Expand Up @@ -829,21 +829,22 @@ public static void SetMaxFPS(this IGame game, int targetFPS)
Primitive2DModelType.Rectangle => size is null ? new BoxColliderShapeDesc() { Is2D = true } : new() { Size = new(size.Value.X, size.Value.Y, 0), Is2D = true },
Primitive2DModelType.Square => size is null ? new BoxColliderShapeDesc() { Is2D = true } : new() { Size = new(size.Value.X, size.Value.Y, 0), Is2D = true },
Primitive2DModelType.Circle => size is null ? new SphereColliderShapeDesc() : new() { Radius = size.Value.X, Is2D = true },
Primitive2DModelType.Capsule => size is null ? new CapsuleColliderShapeDesc() : new() { Radius = size.Value.X, Is2D = true },
_ => throw new InvalidOperationException(),
};

private static IInlineColliderShapeDesc? Get3DColliderShape(PrimitiveModelType type, Vector3? size = null, bool is2D = false)
private static IInlineColliderShapeDesc? Get3DColliderShape(PrimitiveModelType type, Vector3? size = null)
=> type switch
{
PrimitiveModelType.Plane => size is null ? new BoxColliderShapeDesc() : new() { Size = new Vector3(size.Value.X, 0, size.Value.Y) },
PrimitiveModelType.InfinitePlane => new StaticPlaneColliderShapeDesc(),
PrimitiveModelType.Sphere => size is null ? new SphereColliderShapeDesc() : new() { Radius = size.Value.X, Is2D = is2D },
PrimitiveModelType.Cube => size is null ? new BoxColliderShapeDesc() : new() { Size = size ?? Vector3.One, Is2D = is2D },
PrimitiveModelType.Sphere => size is null ? new SphereColliderShapeDesc() : new() { Radius = size.Value.X },
PrimitiveModelType.Cube => size is null ? new BoxColliderShapeDesc() : new() { Size = size ?? Vector3.One },
PrimitiveModelType.Cylinder => size is null ? new CylinderColliderShapeDesc() : new() { Radius = size.Value.X, Height = size.Value.Y },
PrimitiveModelType.Torus => null,
PrimitiveModelType.Teapot => null,
PrimitiveModelType.Cone => size is null ? new ConeColliderShapeDesc() : new() { Radius = size.Value.X, Height = size.Value.Y },
PrimitiveModelType.Capsule => size is null ? new CapsuleColliderShapeDesc() { Radius = 0.35f } : new() { Radius = size.Value.X, Length = size.Value.Y, Is2D = is2D },
PrimitiveModelType.Capsule => size is null ? new CapsuleColliderShapeDesc() { Radius = 0.35f } : new() { Radius = size.Value.X, Length = size.Value.Y },
_ => throw new InvalidOperationException(),
};
}
27 changes: 3 additions & 24 deletions src/Stride.CommunityToolkit/Engine/Primitive2DCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
using Stride.Engine;
using Stride.Engine;
using Stride.Physics;
using Stride.Rendering;

namespace Stride.CommunityToolkit.Engine;

public class Primitive2DCreationOptions
public class Primitive2DCreationOptions : PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the name of the entity.
/// </summary>
public string? EntityName { get; set; }

/// <summary>
/// Gets or sets the material to be applied to the primitive model.
/// </summary>
public Material? Material { get; set; }

/// <summary>
/// Determines whether to include a collider component in the entity. Defaults to true.
/// </summary>
public bool IncludeCollider { get; set; } = true;

/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector2? Size { get; set; }

/// <summary>
/// Gets or sets the render group for the entity. Defaults to RenderGroup.Group0.
/// </summary>
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0;
public float Depth { get; set; } = 0.04f;

/// <summary>
/// Gets or sets the physics component to be added to the entity. Defaults to a new instance of RigidbodyComponent.
/// </summary>
public PhysicsComponent? PhysicsComponent { get; set; } = new RigidbodyComponent();

public float Depth { get; set; } = 0.04f;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Definitions.Colliders;
using Stride.CommunityToolkit.Bepu;

namespace Stride.CommunityToolkit.Engine;

public class Primitive2DCreationOptionsWithBepu : PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector2? Size { get; set; }

public float Depth { get; set; } = 1;

/// <summary>
/// Gets or sets the physics component to be added to the entity.
/// </summary>
public ContainerComponent Component { get; set; } = new Body2DComponent() { Collider = new CompoundCollider() };
}
Loading

0 comments on commit a7e6db7

Please sign in to comment.