Tanoda
NonKinematicGraspedMovement.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
10using UnityEngine;
11
12namespace Leap.Unity.Interaction {
13
21
22 protected float _maxVelocity = 6F;
23
24 private Vector3 _lastSolvedCoMPosition = Vector3.zero;
25 protected AnimationCurve _strengthByDistance = new AnimationCurve(new Keyframe(0.0f, 1.0f, 0.0f, 0.0f),
26 new Keyframe(0.02f, 0.3f, 0.0f, 0.0f));
27
28 public void MoveTo(Vector3 solvedPosition, Quaternion solvedRotation,
29 InteractionBehaviour intObj, bool justGrasped) {
30 Vector3 solvedCenterOfMass = solvedRotation * intObj.rigidbody.centerOfMass + solvedPosition;
31 Vector3 currCenterOfMass = intObj.rigidbody.rotation * intObj.rigidbody.centerOfMass + intObj.rigidbody.position;
32
33 Vector3 targetVelocity = PhysicsUtility.ToLinearVelocity(currCenterOfMass, solvedCenterOfMass, Time.fixedDeltaTime);
34
35 Vector3 targetAngularVelocity = PhysicsUtility.ToAngularVelocity(intObj.rigidbody.rotation, solvedRotation, Time.fixedDeltaTime);
36
37 // Clamp targetVelocity by _maxVelocity.
38 float maxScaledVelocity = _maxVelocity * intObj.manager.SimulationScale;
39 float targetSpeedSqrd = targetVelocity.sqrMagnitude;
40 if (targetSpeedSqrd > maxScaledVelocity * maxScaledVelocity) {
41 float targetPercent = maxScaledVelocity / Mathf.Sqrt(targetSpeedSqrd);
42 targetVelocity *= targetPercent;
43 targetAngularVelocity *= targetPercent;
44 }
45
46 float followStrength = 1F;
47 if (!justGrasped) {
48 float remainingDistanceLastFrame = Vector3.Distance(_lastSolvedCoMPosition, currCenterOfMass);
49 followStrength = _strengthByDistance.Evaluate(remainingDistanceLastFrame / intObj.manager.SimulationScale);
50 }
51
52 Vector3 lerpedVelocity = Vector3.Lerp(intObj.rigidbody.velocity, targetVelocity, followStrength);
53 Vector3 lerpedAngularVelocity = Vector3.Lerp(intObj.rigidbody.angularVelocity, targetAngularVelocity, followStrength);
54
55 intObj.rigidbody.velocity = lerpedVelocity;
56 intObj.rigidbody.angularVelocity = lerpedAngularVelocity;
57
58 _lastSolvedCoMPosition = solvedCenterOfMass;
59
60 // Store the target position and rotation to prevent slippage in SwapGrasp
61 // scenarios.
62 intObj.latestScheduledGraspPose = new Pose(solvedPosition, solvedRotation);
63 }
64 }
65
66}
InteractionBehaviours are components that enable GameObjects to interact with interaction controllers...
Pose? latestScheduledGraspPose
Nonkinematic grasping motion applies clamped velocities to Interaction Behaviours when they are grasp...
Rigidbody rigidbody
The Rigidbody associated with this interaction object.
float SimulationScale
A scale that can be used to appropriately transform distances that otherwise expect one Unity unit to...
This implementation of IGraspedMovementHandler moves an interaction object to its target position and...
void MoveTo(Vector3 solvedPosition, Quaternion solvedRotation, InteractionBehaviour intObj, bool justGrasped)
Called by an interaction object when its grasped pose handler has determined a target pose; this meth...
An IGraspedMovementHandler takes in a target position and rotation (a "pose", usually provided by an ...
A position and rotation. You can multiply two poses; this acts like Matrix4x4 multiplication,...
Definition: Pose.cs:21