10using System.Collections.Generic;
26 private float _windowLength = 0.05F;
31 private float _windowDelay = 0.02F;
37 private AnimationCurve _velocityMultiplierCurve =
new AnimationCurve(
38 new Keyframe(0.0F, 1.0F, 0, 0),
39 new Keyframe(3.0F, 1.5F, 0, 0));
41 private struct VelocitySample {
43 public Vector3 position;
44 public Quaternion rotation;
46 public VelocitySample(Vector3 position, Quaternion rotation,
float time) {
47 this.position = position;
48 this.rotation = rotation;
49 this.time = Time.fixedTime;
52 public static VelocitySample Interpolate(VelocitySample a, VelocitySample b,
float time) {
53 float alpha = Mathf.Clamp01(Mathf.InverseLerp(a.time, b.time, time));
55 return new VelocitySample(Vector3.Lerp(a.position, b.position, alpha),
56 Quaternion.Slerp(a.rotation, b.rotation, alpha),
61 private Queue<VelocitySample> _velocityQueue =
new Queue<VelocitySample>(64);
68 _velocityQueue.Enqueue(
new VelocitySample(intObj.
rigidbody.position,
73 VelocitySample oldestVelocity = _velocityQueue.Peek();
77 if (oldestVelocity.time + (Time.fixedDeltaTime * 4) < Time.fixedTime
80 _velocityQueue.Dequeue();
92 if (_velocityQueue.Count < 2) {
94 intObj.
rigidbody.angularVelocity = Vector3.zero;
98 float windowEnd = Time.fixedTime - _windowDelay;
99 float windowStart = windowEnd - _windowLength;
103 VelocitySample start0, start1;
104 VelocitySample end0, end1;
105 VelocitySample s0, s1;
107 s0 = s1 = start0 = start1 = end0 = end1 = _velocityQueue.Dequeue();
109 while (_velocityQueue.Count != 0) {
111 s1 = _velocityQueue.Dequeue();
113 if (s0.time < windowStart && s1.time >= windowStart) {
118 if (s0.time < windowEnd && s1.time >= windowEnd) {
123 _velocityQueue.Clear();
128 VelocitySample start = VelocitySample.Interpolate(start0, start1, windowStart);
129 VelocitySample end = VelocitySample.Interpolate(end0, end1, windowEnd);
131 Vector3 interpolatedVelocity = PhysicsUtility.ToLinearVelocity(start.position,
135 intObj.
rigidbody.velocity = interpolatedVelocity;
136 intObj.
rigidbody.angularVelocity = PhysicsUtility.ToAngularVelocity(start.rotation,
140 intObj.
rigidbody.velocity *= _velocityMultiplierCurve.Evaluate(intObj.
rigidbody.velocity.magnitude);
InteractionBehaviours are components that enable GameObjects to interact with interaction controllers...
Rigidbody rigidbody
The Rigidbody associated with this interaction object.
The sliding window throw handler implements a simple heuristic that provides a reasonably accurate me...
void OnThrow(InteractionBehaviour intObj, InteractionController throwingController)
Transfers the averaged velocity to the released object.
void OnHold(InteractionBehaviour intObj, ReadonlyList< InteractionController > controllers)
Samples the current velocity and adds it to a rolling average.
Interaction objects feed their throw handlers callbacks when they are held (for data collection) and ...
A simple wrapper around List to provide readonly access. Useful when you want to return a list to som...