I also capture the motion controller positions and calculate a velocity vector from that.
Here’s something you’ll want to watch out for: If you’re only capturing the MC position at the current frame and comparing it against the position at the last frame, you’re going to get some pretty wild results if there is even a slight bit of jitter in the hand movement between Frame 0 and Frame T-1. You’ll want to apply some smoothing over time to account for this.
Here’s how I do it:
I create a queue with a fixed length of 10. Every frame, I enqueue the current position of the motion controller. If the queue is full, the last item is dequeued. So, here’s what my data structure looks like:
0 1 2 3 4 5 6 7 8 9
When the player releases the grip on the motion controller, I then take the current vector values from the priority queue and then I multiply them by a weighting value, then calculate the deltas and sum them together. The most recent hand position should have the most weight and influence over the trajectory, and the oldest positions should have the least weight. The result is a weighted average vector which gives a smoothed value for velocity. So, if the players hand twitches a tad or they do some really rapid movement, we can still get a nice velocity vector and throw the held object in the hand velocity vector.
If the queue has only one position vector, then we can’t possibly create a delta to find velocity, so we just apply a zero velocity. This can happen if the player taps the grab and throw button extremely quickly. It’s rare, but if you don’t handle it, you’ll crash.