-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from VaclavElias/main
refactor: Bepu references corrected and other updates
- Loading branch information
Showing
12 changed files
with
200 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build
Check warning on line 41 in src/Stride.CommunityToolkit/Bepu/Body2DComponent.cs GitHub Actions / build
|
||
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 3 additions & 24 deletions
27
src/Stride.CommunityToolkit/Engine/Primitive2DCreationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Stride.CommunityToolkit/Engine/Primitive2DCreationOptionsWithBepu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }; | ||
} |
Oops, something went wrong.