ExtendRealityLtd / VRTK

An example of how to use the Tilia packages to create great content with VRTK v4.
https://www.vrtk.io/
MIT License
3.69k stars 1k forks source link

Controller movement speed #149

Closed Roland09 closed 8 years ago

Roland09 commented 8 years ago

Is there a way to detect the speed / force that a controller is being moved? e. g. for a boxing game, how hard you punch?

thestonefox commented 8 years ago

Yeah, check out the sword example:

https://github.com/thestonefox/SteamVR_Unity_Toolkit/blob/master/Assets/SteamVR_Unity_Toolkit/Examples/Resources/Scripts/Sword.cs#L31

It's quite basic. But it works out the magnitude of the collision

Roland09 commented 8 years ago

Thanks. That gives only the value at the collision. Is there a way to detect the speed / force while it's being moved? Like a "whoooooosh" sound while you move tennis racket. Or like when you move a lightsaber slow or fast.

thestonefox commented 8 years ago

would the velocity magnitude not tell you how fast it's moving? a higher velocity means it's moving quicker, right?

Roland09 commented 8 years ago

Thank you, I found the problem. I was using an attached child with a rigidbody, that's why velocity.magnitude didn't work. This is how it works now:

float speed = 0;
Vector3 lastPosition = Vector3.zero;

protected override void FixedUpdate()
{
    base.FixedUpdate();

    speed = (((transform.position - lastPosition).magnitude) / Time.deltaTime);
    lastPosition = transform.position;
}
thestonefox commented 8 years ago

Ah cool. Yeah I guess velocity won't work on an object you're moving with the controller because the object itself has no velocity because it's just tracking the controller. But using the magnitude of the positions would give a close enough approximation :)

Do you need to do it in the FixedUpdate? Would Update be ok?

I know FixedUpdate will be a lot more precise but is the precision worth the performance hit?

Roland09 commented 8 years ago

I only put it in FixedUpdate because I read so. It's not necessary to have it there. If that's a performance problem, I'll put it into Update, thanks for the hint.

thestonefox commented 8 years ago

It's just that FixedUpdate is run tonnes of times per frame, meaning your speed calculation is run a load more times, the Update is only run once per frame.

FixedUpdate is really useful when dealing with physics calculations because it's really precise and you get a fewer glitches with collisions, etc.

But I feel for getting an approximate speed, you could get away with using just Update. Unless you need it to be really precise.

Roland09 commented 8 years ago

Good to know. It's currently just for gaming atmosphere, i. e. playing a sound when the user makes a fast gesture.

On Wed, Jun 8, 2016 at 8:52 AM, StoneFox notifications@github.com wrote:

It's just that FixedUpdate is run tonnes of times per frame, meaning your speed calculation is run a load more times, the Update is only run once per frame.

FixedUpdate is really useful when dealing with physics calculations because it's really precise and you get a fewer glitches with collisions, etc.

But I feel for getting an approximate speed, you could get away with using just Update. Unless you need it to be really precise.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/thestonefox/SteamVR_Unity_Toolkit/issues/149#issuecomment-224504450, or mute the thread https://github.com/notifications/unsubscribe/AKdJ6InPf4FR-LcWVmy0xzFKeKe1nu6lks5qJmaqgaJpZM4IwH_L .