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: Adding Bepu back #126

Merged
merged 2 commits into from
Apr 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<!--<ItemGroup>
<PackageReference Include="Stride.Core.Assets.CompilerApp" Version="4.2.0.2122" IncludeAssets="build" />
</ItemGroup>-->

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy ..\Resources\JumpyJetBackground.jpg $(OutDir)" />
</Target>
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 ?
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()
{
}
}
Loading