# What is a good algorithm for generating a linear regression of positions in 3D space? (for getting the direction of a thrown object in VR)

I'm trying to get throwing to feel right in my VR game. I don't plan on actually using physics to do this; my idea is to accurately determine the lateral direction of the throw, then move the object in the intended direction (with a doctored Y value) at a speed dependent on other factors.

The language of the code doesn't matter too much, but I'm using Godot, so something that wouldn't require me to import a bunch of different math libraries (by converting them to Godot's Python-style GDScript) would be ideal. It doesn't need to be robust.

To give you an idea of what I was thinking, my original plan was to save the position of the grabbing controller every frame while an object is being held, removing old positions if the controller is moving backwards, then averaging out the movement deltas over the last 10 or 15 frames or so before letting go (it should be consistent since Godot has a fixed-timestep "update" function available) and normalizing the vector to get the direction the object should travel in.

However, this blog post got me thinking that spending the time learning how to do the linear regression in a more robust way, then applying that to 3D/VR, might be worthwhile. I just wonder if, since I don't need the actual linear or angular velocity, it might be overkill in terms of time spent on the feature.

• Did you try applying your first idea? How does it perform? Does it deviate from your desired behaviour in any specific way that we can help you correct? – DMGregory Oct 22 '19 at 21:06
• I was finally able to implement the initial idea this morning, and, to be honest, it seems fine, so far. Just creating an array of the last 15 position deltas, normalizing the average of them, then passing that plus some "throw strength" to the object being thrown, to be multiplied by its own base "standard speed" and delta time, seems to work well enough. Haven't even bothered accounting for wind-ups yet, since 15 frames is such a small amount of time. That would be different on Oculus Quest, though... – Roderick Oct 24 '19 at 4:03