Tanoda
Spaceship.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 Leap.Unity;
11using System;
12using System.Collections;
13using System.Collections.Generic;
14using UnityEngine;
15
16namespace Leap.Unity.Examples {
17
32 [AddComponentMenu("")]
33 public class Spaceship : MonoBehaviour {
34
35 private Rigidbody _body;
43 public
44 #if UNITY_EDITOR
45 new
46 #endif
47 Rigidbody rigidbody { get { return _body; } }
48
49 private float _mass = 10F;
50
51 private Vector3 _velocity;
52 public Vector3 velocity {
53 get { return _velocity; }
54 set { _velocity = value; }
55 }
56
57 public Vector3 shipAlignedVelocity {
58 get { return Quaternion.Inverse(this.transform.rotation) * _velocity; }
59 }
60
61 private Vector3 _angularVelocity;
62 public Vector3 angularVelocity {
63 get { return _angularVelocity; }
64 set { _angularVelocity = value; }
65 }
66
68 get { return Quaternion.Inverse(this.transform.rotation) * _angularVelocity; }
69 }
70
71 private Vector3 _accumulatedForce;
72 private Vector3 _accumulatedTorque;
73
74 void Awake() {
75 _body = GetComponent<Rigidbody>();
76 _body.mass = _mass;
77 _body.isKinematic = true;
78 }
79
80 void Start() {
81 // The ship is moved in the manager's OnPrePhysicalUpdate callback, which ensures
82 // (1) the ship's transform is updated before the Interaction Manager runs, and
83 // (2) the ship's transform is updated in FixedUpdate.
84 //
85 // The Interaction Manager takes into account how it has moved since its last
86 // update, and informs interaction controllers appropriately, allowing
87 // interfaces to function properly.
88
90 }
91
92 private void updateShipPhysics() {
93 // Update velocity.
94 Vector3 acceleration = _accumulatedForce / _mass;
95 _velocity += acceleration * Time.deltaTime;
96
97 _accumulatedForce = Vector3.zero;
98
99 // Update position.
100 Vector3 newPosition = this.transform.position + _velocity * Time.deltaTime;
101 this.transform.position = newPosition;
102
103
104 // Update angular velocity.
105 Vector3 eulerAcceleration = _accumulatedTorque;
106 _angularVelocity += eulerAcceleration * Time.deltaTime;
107
108 _accumulatedTorque = Vector3.zero;
109
110 // Update rotation.
111 Quaternion newRotation = Quaternion.Euler(_angularVelocity * Time.deltaTime) * this.transform.rotation;
112 this.transform.rotation = newRotation;
113
114 // Sync transforms with the Physics engine so Rigidbody changes reflect
115 // the movement of the ship. (Required for 2017.3 and newer.)
116#if UNITY_2017_3_OR_NEWER
117 Physics.SyncTransforms();
118#endif
119 }
120
121 #region Ship Forces API
122
123 public void AddForce(Vector3 force) {
124 _accumulatedForce += force;
125 }
126
127 public void AddForceAtPosition(Vector3 force, Vector3 position) {
128 Vector3 toCenterOfMass = this.transform.TransformPoint(_body.centerOfMass) - position;
129
130 float forceCMAngle = Vector3.Angle(force, toCenterOfMass);
131
132 // Linear force
133 Vector3 linForce = force * Mathf.Cos(forceCMAngle);
134 _accumulatedForce += linForce;
135
136 // Torque
137 Vector3 torqueVector = force * Mathf.Sin(forceCMAngle) * toCenterOfMass.magnitude;
138
139 _accumulatedTorque += Vector3.Cross(torqueVector, toCenterOfMass);
140 }
141
142 public void AddShipAlignedTorque(Vector3 shipAlignedTorque) {
143 _accumulatedTorque += this.transform.rotation * shipAlignedTorque;
144 }
145
146 public void AddShipAlignedForce(Vector3 shipAlignedForce) {
147 _accumulatedForce += this.transform.rotation * shipAlignedForce;
148 }
149
150 #endregion
151
152 }
153
154}
155
The spaceship in this example is a kinematic rigidbody with a force API, but having a rigidbody on yo...
Definition: Spaceship.cs:33
void AddShipAlignedForce(Vector3 shipAlignedForce)
Definition: Spaceship.cs:146
Rigidbody rigidbody
The ship contains Colliders, so it is given a kinematic Rigidbody to prevent the overhead of moving C...
Definition: Spaceship.cs:47
void AddForceAtPosition(Vector3 force, Vector3 position)
Definition: Spaceship.cs:127
void AddForce(Vector3 force)
Definition: Spaceship.cs:123
void AddShipAlignedTorque(Vector3 shipAlignedTorque)
Definition: Spaceship.cs:142
static InteractionManager instance
Often, only one InteractionManager is necessary per Unity scene. This property will contain that Inte...