Skip to content

Commit b3d7760

Browse files
authored
feat: SetTargetPose and deprecate setters for Position and Orientation (#2701)
1 parent 5e5429f commit b3d7760

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BodyComponent.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ public Vector3 AngularVelocity
233233
public Vector3 Position
234234
{
235235
get => BodyReference?.Pose.Position.ToStride() ?? default;
236-
set => SetPose(value, Orientation);
236+
[Obsolete($"Setter will be removed in a future version, use {nameof(SetTargetPose)} or {nameof(Teleport)}")]
237+
set => Teleport(value, Orientation);
237238
}
238239

239240
/// <summary>
@@ -248,7 +249,8 @@ public Vector3 Position
248249
public Quaternion Orientation
249250
{
250251
get => BodyReference?.Pose.Orientation.ToStride() ?? Quaternion.Identity;
251-
set => SetPose(Position, value);
252+
[Obsolete($"Setter will be removed in a future version, use {nameof(SetTargetPose)} or {nameof(Teleport)}")]
253+
set => Teleport(Position, value);
252254
}
253255

254256
/// <summary>
@@ -354,15 +356,38 @@ public void ApplyLinearImpulse(Vector3 impulse)
354356
}
355357

356358
/// <summary>
357-
/// Teleporting this body into a new pose, faster than setting both <see cref="Position"/> and <see cref="Orientation"/> individually
359+
/// Set the pose this body should try to match on the next physics tick, this will collide with objects on the way
360+
/// </summary>
361+
/// <remarks>
362+
/// 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.
363+
/// Consider using a constraint between this body and whatever it is following, or using the different Impulse methods instead <br/><br/>
364+
/// <paramref name="targetPosition"/> is slightly offset from this entity's Transform <see cref="TransformComponent.Position"/> based on its <see cref="CollidableComponent.CenterOfMass"/> <br/><br/>
365+
/// 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
366+
/// </remarks>
367+
public void SetTargetPose(Vector3 targetPosition, Quaternion targetOrientation)
368+
{
369+
if (Simulation is null)
370+
return;
371+
372+
Awake = true;
373+
374+
float deltaTime = (float)Simulation.FixedTimeStep.TotalSeconds;
375+
376+
LinearVelocity = (targetPosition - Position) / deltaTime;
377+
var quatDelta = Quaternion.Invert(Orientation) * targetOrientation;
378+
AngularVelocity = new Vector3(quatDelta.X, quatDelta.Y, quatDelta.Z) / deltaTime;
379+
}
380+
381+
/// <summary>
382+
/// Teleport this body into a new pose
358383
/// </summary>
359384
/// <remarks>
360385
/// Using this function to move objects around is not recommended,
361386
/// as it disregards any collider that may overlap with the body at this new position,
362387
/// you should make sure the area is clear to ensure this object does not become stuck in the scenery.<br/><br/>
363388
/// <paramref name="position"/> is slightly offset from this entity's Transform <see cref="TransformComponent.Position"/> based on its <see cref="CollidableComponent.CenterOfMass"/>
364389
/// </remarks>
365-
public void SetPose(Vector3 position, Quaternion orientation)
390+
public void Teleport(Vector3 position, Quaternion orientation)
366391
{
367392
if (BodyReference is { } bodyRef)
368393
{
@@ -377,6 +402,18 @@ public void SetPose(Vector3 position, Quaternion orientation)
377402
Entity.Transform.Rotation = orientation;
378403
}
379404

405+
/// <summary>
406+
/// Teleport this body into a new pose
407+
/// </summary>
408+
/// <remarks>
409+
/// Using this function to move objects around is not recommended,
410+
/// as it disregards any collider that may overlap with the body at this new position,
411+
/// you should make sure the area is clear to ensure this object does not become stuck in the scenery.<br/><br/>
412+
/// <paramref name="position"/> is slightly offset from this entity's Transform <see cref="TransformComponent.Position"/> based on its <see cref="CollidableComponent.CenterOfMass"/>
413+
/// </remarks>
414+
[Obsolete($"This method will be removed in the future, use {nameof(Teleport)} instead")]
415+
public void SetPose(Vector3 position, Quaternion orientation) => Teleport(position, orientation);
416+
380417
protected override ref MaterialProperties MaterialProperties => ref Simulation!.CollidableMaterials[BodyReference!.Value];
381418
protected internal override NRigidPose? Pose => BodyReference?.Pose;
382419

0 commit comments

Comments
 (0)