Tanoda
SwapGraspExample.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
9using UnityEngine;
11
12namespace Leap.Unity.Examples {
13
15
16 [AddComponentMenu("")]
17 public class SwapGraspExample : MonoBehaviour {
18
19 public IntObj objA, objB;
20
22
23 private bool _swapScheduled = false;
24
25 void Start() {
26 swapButton.OnUnpress += scheduleSwap;
27
28 // Wait for just after the PhysX update to swap a grasp;
29 // this allows the swapped object to inherit the _latest_ rigidbody position and
30 // rotation from the original held object (which needs the PhysX update to receive
31 // scheduled force / MovePosition / MoveRotation changes from the grasped movement
32 // system).
33 PhysicsCallbacks.OnPostPhysics += onPostPhysics;
34 }
35
36 private void scheduleSwap() {
37 _swapScheduled = true;
38 }
39
40 private void onPostPhysics() {
41 //Swapping when both objects are grasped is unsupported
42 if(objA.isGrasped && objB.isGrasped) { return; }
43
44 if (_swapScheduled && (objA.isGrasped || objB.isGrasped)) {
45
46 // Swap "a" for "b"; a will be whichever object is the grasped one.
47 IntObj a, b;
48 if (objA.isGrasped) {
49 a = objA;
50 b = objB;
51 }
52 else {
53 a = objB;
54 b = objA;
55 }
56
57 // (Optional) Remember B's pose and motion to apply to A post-swap.
58 var bPose = new Pose(b.rigidbody.position, b.rigidbody.rotation);
59 var bVel = b.rigidbody.velocity;
60 var bAngVel = b.rigidbody.angularVelocity;
61
62 // Match the rigidbody pose of the originally held object before swapping.
63 // If it exists, always use the latestScheduledGraspPose to perform a SwapGrasp!
64 // This prevents subtle slippage with non-kinematic objects that may experience
65 // gravity forces, drag, or hit other objects, which can leak into the new
66 // grasping pose when the SwapGrasp is performed.
67 if (a.latestScheduledGraspPose.HasValue) {
68 b.rigidbody.position = a.latestScheduledGraspPose.Value.position;
69 b.rigidbody.rotation = a.latestScheduledGraspPose.Value.rotation;
70 }
71 else {
72 b.rigidbody.position = a.rigidbody.position;
73 b.rigidbody.rotation = a.rigidbody.rotation;
74 }
75
76 // Swap!
77 a.graspingController.SwapGrasp(b);
78
79 // Move A over to where B was, and for fun, let's give it B's motion as well.
80 a.rigidbody.position = bPose.position;
81 a.rigidbody.rotation = bPose.rotation;
82 a.rigidbody.velocity = bVel;
83 a.rigidbody.angularVelocity = bAngVel;
84 }
85
86 _swapScheduled = false;
87 }
88 }
89
90}
91
InteractionBehaviours are components that enable GameObjects to interact with interaction controllers...
bool isGrasped
Gets whether this object is grasped by any interaction controller.
A physics-enabled button. Activated by physically pressing the button, with events for press and unpr...
InteractionBehaviour IntObj