mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-10 21:40:19 +00:00
add interpolation for rigidbody sync
This commit is contained in:
parent
6b4281aa85
commit
c3adda3ae9
@ -15,6 +15,10 @@ namespace QSB.Syncs.RigidbodySync
|
|||||||
public abstract bool IsReady { get; }
|
public abstract bool IsReady { get; }
|
||||||
public abstract bool UseInterpolation { get; }
|
public abstract bool UseInterpolation { get; }
|
||||||
|
|
||||||
|
protected virtual float DistanceLeeway { get; } = 5f;
|
||||||
|
private float _previousDistance;
|
||||||
|
private const float SmoothTime = 0.1f;
|
||||||
|
|
||||||
protected bool _isInitialized;
|
protected bool _isInitialized;
|
||||||
protected IntermediaryTransform _intermediaryTransform;
|
protected IntermediaryTransform _intermediaryTransform;
|
||||||
protected Vector3 _velocity;
|
protected Vector3 _velocity;
|
||||||
@ -22,6 +26,8 @@ namespace QSB.Syncs.RigidbodySync
|
|||||||
protected Vector3 _prevVelocity;
|
protected Vector3 _prevVelocity;
|
||||||
protected Vector3 _prevAngularVelocity;
|
protected Vector3 _prevAngularVelocity;
|
||||||
private string _logName => $"{NetId}:{GetType().Name}";
|
private string _logName => $"{NetId}:{GetType().Name}";
|
||||||
|
private Vector3 _positionSmoothVelocity;
|
||||||
|
private Quaternion _rotationSmoothVelocity;
|
||||||
|
|
||||||
protected abstract OWRigidbody GetRigidbody();
|
protected abstract OWRigidbody GetRigidbody();
|
||||||
|
|
||||||
@ -172,11 +178,17 @@ namespace QSB.Syncs.RigidbodySync
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AttachedObject.SetPosition(targetPos);
|
if (UseInterpolation)
|
||||||
AttachedObject.SetRotation(targetRot);
|
{
|
||||||
//AttachedObject.transform.position = targetPos;
|
AttachedObject.SetPosition(SmartPositionSmoothDamp(AttachedObject.transform.position, targetPos));
|
||||||
//AttachedObject.transform.rotation = targetRot;
|
AttachedObject.SetRotation(QuaternionHelper.SmoothDamp(AttachedObject.transform.rotation, targetRot, ref _rotationSmoothVelocity, SmoothTime));
|
||||||
//AttachedObject.SetVelocity(ReferenceTransform.GetAttachedOWRigidbody().GetPointVelocity(targetPos) + _velocity);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
AttachedObject.SetPosition(targetPos);
|
||||||
|
AttachedObject.SetRotation(targetRot);
|
||||||
|
}
|
||||||
|
|
||||||
SetVelocity(AttachedObject, ReferenceTransform.GetAttachedOWRigidbody().GetPointVelocity(targetPos) + _velocity);
|
SetVelocity(AttachedObject, ReferenceTransform.GetAttachedOWRigidbody().GetPointVelocity(targetPos) + _velocity);
|
||||||
AttachedObject.SetAngularVelocity(ReferenceTransform.GetAttachedOWRigidbody().GetAngularVelocity() + _angularVelocity);
|
AttachedObject.SetAngularVelocity(ReferenceTransform.GetAttachedOWRigidbody().GetAngularVelocity() + _angularVelocity);
|
||||||
}
|
}
|
||||||
@ -211,6 +223,20 @@ namespace QSB.Syncs.RigidbodySync
|
|||||||
_intermediaryTransform.SetReferenceTransform(transform);
|
_intermediaryTransform.SetReferenceTransform(transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO : remove .Distance
|
||||||
|
private Vector3 SmartPositionSmoothDamp(Vector3 currentPosition, Vector3 targetPosition)
|
||||||
|
{
|
||||||
|
var distance = Vector3.Distance(currentPosition, targetPosition);
|
||||||
|
if (distance > _previousDistance + DistanceLeeway)
|
||||||
|
{
|
||||||
|
DebugLog.DebugWrite($"Warning - {AttachedObject.name} moved too far!", MessageType.Warning);
|
||||||
|
_previousDistance = distance;
|
||||||
|
return targetPosition;
|
||||||
|
}
|
||||||
|
_previousDistance = distance;
|
||||||
|
return Vector3.SmoothDamp(currentPosition, targetPosition, ref _positionSmoothVelocity, SmoothTime);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO : optimize by using sqrMagnitude
|
// TODO : optimize by using sqrMagnitude
|
||||||
public override bool HasMoved()
|
public override bool HasMoved()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user