comparison Orchestland/Assets/LeapMotion/DemoResources/Scripts/AerodynamicLeaf.cs @ 3:0030a1b971fb default tip

merge
author Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:23:43 +0900
parents f7675884f2a1
children
comparison
equal deleted inserted replaced
2:fdab88fc2cb9 3:0030a1b971fb
1 /******************************************************************************\
2 * Copyright (C) Leap Motion, Inc. 2011-2014. *
3 * Leap Motion proprietary. Licensed under Apache 2.0 *
4 * Available at http://www.apache.org/licenses/LICENSE-2.0.html *
5 \******************************************************************************/
6
7 using UnityEngine;
8 using System.Collections;
9
10 public class AerodynamicLeaf : MonoBehaviour {
11
12 public float airDragForce = 0.2f;
13 public float airDragTorque = 0.001f;
14
15 // Air to water transition level.
16 public float waterHeight = 0.0f;
17 public float transitionWidth = 0.2f;
18
19 // Water drag.
20 public float waterDrag = 5.0f;
21 public float waterAngularDrag = 10.0f;
22
23 // Water forces.
24 public float waterBuoancyForce = 0.1f;
25 public float waterDragTorque = 0.002f;
26 public float waterDragForce = 0.002f;
27 public float waterSurfaceTorque = 0.002f;
28
29 // Water curent.
30 public Vector3 waterCurrentVelocity;
31 public float waterCurrentForce = 0.1f;
32
33 private float air_drag_;
34 private float air_angular_drag_;
35 private float drag_force_;
36 private float drag_torque_;
37
38 void Start() {
39 air_drag_ = GetComponent<Rigidbody>().drag;
40 air_angular_drag_ = GetComponent<Rigidbody>().angularDrag;
41 drag_force_ = airDragForce;
42 drag_torque_ = airDragTorque;
43 }
44
45 void DragUpdate() {
46 Vector3 velocity = GetComponent<Rigidbody>().velocity;
47 Vector3 normal = transform.up;
48
49 float dot = Vector3.Dot(velocity, normal);
50 GetComponent<Rigidbody>().AddForce(-normal * drag_force_ * dot);
51
52 Vector3 cross = Vector3.Cross(velocity, normal);
53 GetComponent<Rigidbody>().AddTorque(-drag_torque_ * cross);
54 }
55
56 void AirUpdate() {
57 GetComponent<Rigidbody>().drag = air_drag_;
58 GetComponent<Rigidbody>().angularDrag = air_angular_drag_;
59 drag_force_ = airDragForce;
60 drag_torque_ = airDragTorque;
61 DragUpdate();
62 }
63
64 void WaterUpdate(float level) {
65 GetComponent<Rigidbody>().drag = waterDrag;
66 GetComponent<Rigidbody>().angularDrag = waterAngularDrag;
67
68 drag_force_ = waterDragForce;
69 drag_torque_ = waterDragTorque;
70 DragUpdate();
71
72 float transition = Mathf.Clamp(-level / transitionWidth, 0.0f, 1.0f);
73 GetComponent<Rigidbody>().AddForce(new Vector3(0, waterBuoancyForce * transition, 0));
74
75 if (Vector3.Dot(transform.up, Vector3.up) >= 0) {
76 Vector3 torque_vector = Vector3.Cross(transform.up, Vector3.up);
77 GetComponent<Rigidbody>().AddTorque((1 - transition) * waterSurfaceTorque * torque_vector);
78 }
79 else {
80 Vector3 torque_vector = Vector3.Cross(-transform.up, Vector3.up);
81 GetComponent<Rigidbody>().AddTorque((1 - transition) * waterSurfaceTorque * torque_vector);
82 }
83
84 // Running water current.
85 Vector3 delta_current = waterCurrentVelocity - GetComponent<Rigidbody>().velocity;
86 delta_current.y = 0;
87 GetComponent<Rigidbody>().AddForce(waterCurrentForce * delta_current);
88 }
89
90 float UnitsAboveWater() {
91 return transform.position.y - waterHeight;
92 }
93
94 public bool TouchingWater() {
95 return UnitsAboveWater() < 0;
96 }
97
98 void FixedUpdate() {
99 if (TouchingWater())
100 WaterUpdate(UnitsAboveWater());
101 else
102 AirUpdate();
103 }
104 }