Skip to content
Open
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 @@ -25,6 +25,7 @@
using BRigidPose = BepuPhysics.RigidPose;
using SRigidPose = Stride.BepuPhysics.Definitions.RigidPose;
using SBodyVelocity = Stride.BepuPhysics.Definitions.BodyVelocity;
using Stride.BepuPhysics.Systems.Characters;

namespace Stride.BepuPhysics;

Expand All @@ -48,6 +49,8 @@ public sealed class BepuSimulation : IDisposable
private AwaitRunner _preTickRunner = new();
private AwaitRunner _postTickRunner = new();

internal CharacterControllers Characters { get; }

internal BufferPool BufferPool { get; }

internal CollidableProperty<MaterialProperties> CollidableMaterials { get; } = new();
Expand Down Expand Up @@ -295,19 +298,21 @@ public BepuSimulation()

var strideNarrowPhaseCallbacks = new StrideNarrowPhaseCallbacks(this, ContactEvents, CollidableMaterials);
var stridePoseIntegratorCallbacks = new StridePoseIntegratorCallbacks(CollidableMaterials);
var solveDescription = new SolveDescription(1, 1);
var solveDescription = new SolveDescription(8, 1);

Simulation = Simulation.Create(BufferPool, strideNarrowPhaseCallbacks, stridePoseIntegratorCallbacks, solveDescription);
Simulation.Solver.VelocityIterationCount = 8;
Simulation.Solver.SubstepCount = 1;

CollidableMaterials.Initialize(Simulation);
ContactEvents.Initialize();

Characters = new CharacterControllers(BufferPool);
Characters.Initialize(Simulation);
//CollisionBatcher = new CollisionBatcher<BatcherCallbacks>(BufferPool, Simulation.Shapes, Simulation.NarrowPhase.CollisionTaskRegistry, 0, DefaultBatcherCallbacks);
}

public void Dispose()
{
Characters.Dispose();
_threadDispatcher.Dispose();
BufferPool.Clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,57 @@ public void ApplyLinearImpulse(Vector3 impulse)
/// This method sets this body's <see cref="LinearVelocity"/> and <see cref="AngularVelocity"/>, setting these properties after the call would overwrite the result of this method
/// </remarks>
public void SetTargetPose(Vector3 targetPosition, Quaternion targetOrientation)
{
SetTargetPose(targetPosition);
SetTargetPose(targetOrientation);
}

/// <summary>
/// Set the position this body should try to match on the next physics tick, this will collide with objects on the way
/// </summary>
/// <remarks>
/// Using this function to move objects around is not recommended as it results in unrealistic forces being applied on this body, or unexpected stuttering depending on the input.
/// Consider using a constraint between this body and whatever it is following, or using the different Impulse methods instead <br/><br/>
/// <paramref name="targetPosition"/> is slightly offset from this entity's Transform <see cref="TransformComponent.Position"/> based on its <see cref="CollidableComponent.CenterOfMass"/> <br/><br/>
/// This method sets this body's <see cref="LinearVelocity"/>, setting these properties after the call would overwrite the result of this method
/// </remarks>
public void SetTargetPose(Vector3 targetPosition)
{
if (Simulation is null)
return;

Awake = true;
float deltaTime = (float)Simulation.FixedTimeStep.TotalSeconds;

var newVelocity = (targetPosition - Position) / deltaTime;
if (newVelocity.LengthSquared() > float.Epsilon)
{
Awake = true;
LinearVelocity = newVelocity;
}
}

/// <summary>
/// Set the orientation this body should try to match on the next physics tick, this will collide with objects on the way
/// </summary>
/// <remarks>
/// Using this function to move objects around is not recommended as it results in unrealistic forces being applied on this body, or unexpected stuttering depending on the input.
/// Consider using a constraint between this body and whatever it is following, or using the different Impulse methods instead <br/><br/>
/// This method sets this body's <see cref="AngularVelocity"/>, setting these properties after the call would overwrite the result of this method
/// </remarks>
public void SetTargetPose(Quaternion targetOrientation)
{
if (Simulation is null)
return;

float deltaTime = (float)Simulation.FixedTimeStep.TotalSeconds;

LinearVelocity = (targetPosition - Position) / deltaTime;
var quatDelta = Quaternion.Invert(Orientation) * targetOrientation;
AngularVelocity = new Vector3(quatDelta.X, quatDelta.Y, quatDelta.Z) / deltaTime;
var newVelocity = new Vector3(quatDelta.X, quatDelta.Y, quatDelta.Z) / deltaTime;
if (newVelocity.LengthSquared() > float.Epsilon)
{
Awake = true;
AngularVelocity = newVelocity;
}
}

/// <summary>
Expand Down
Loading
Loading