|
6 | 6 | * $Notice: See LICENSE.txt for modification and distribution information |
7 | 7 | * Copyright (c) 2016 by Shen, Jen-Chieh $ |
8 | 8 | */ |
| 9 | +using System.Collections.Generic; |
9 | 10 | using UnityEngine; |
10 | 11 | using MyBox; |
11 | 12 |
|
@@ -143,11 +144,12 @@ public void ThrowByTime(Vector3 targetPos, float time) |
143 | 144 | { |
144 | 145 | Vector3 displacement = targetPos - this.transform.position; |
145 | 146 |
|
| 147 | + // Calculate initial velocity. |
146 | 148 | mVelocity.x = displacement.x / time; |
147 | 149 | mVelocity.z = displacement.z / time; |
148 | | - mVelocity.y = (displacement.y - (JCS_Constants.GRAVITY * mGravityProduct * time * time / 2)) / time; |
| 150 | + mVelocity.y = (displacement.y - (JCS_Constants.GRAVITY * mGravityProduct * time * time / 2.0f)) / time; |
149 | 151 |
|
150 | | - // start dropping. |
| 152 | + // start the action. |
151 | 153 | this.mActive = true; |
152 | 154 | } |
153 | 155 |
|
@@ -177,5 +179,80 @@ public void ThrowByForce(Vector3 targetPos, float vel) |
177 | 179 |
|
178 | 180 | ThrowByTime(targetPos, time); |
179 | 181 | } |
| 182 | + |
| 183 | + #region Simulation |
| 184 | + |
| 185 | + /// <summary> |
| 186 | + /// Return a list of arch positions. |
| 187 | + /// </summary> |
| 188 | + /// <param name="pointCount"> This decides how many points you want. </param> |
| 189 | + /// <param name="startPos"> Starting position. </param> |
| 190 | + /// <param name="targetPos"> End position </param> |
| 191 | + /// <param name="time"> Time to perform. </param> |
| 192 | + /// <param name="gravityProduct"> Arch height. </param> |
| 193 | + public static List<Vector3> GetArchByTime( |
| 194 | + int pointCount, |
| 195 | + Vector3 startPos, Vector3 targetPos, |
| 196 | + float time, float gravityProduct) |
| 197 | + { |
| 198 | + List<Vector3> points = new(); |
| 199 | + |
| 200 | + Vector3 displacement = targetPos - startPos; |
| 201 | + |
| 202 | + Vector3 velocity = Vector3.zero; |
| 203 | + |
| 204 | + // Calculate initial velocity. |
| 205 | + velocity.x = displacement.x / time; |
| 206 | + velocity.z = displacement.z / time; |
| 207 | + velocity.y = (displacement.y - (JCS_Constants.GRAVITY * gravityProduct * time * time / 2.0f)) / time; |
| 208 | + |
| 209 | + /* 開始模擬 */ |
| 210 | + |
| 211 | + float timer = 0.0f; |
| 212 | + |
| 213 | + float interval = time / pointCount; |
| 214 | + |
| 215 | + // Add first point. |
| 216 | + points.Add(startPos); |
| 217 | + |
| 218 | + while (timer < time) |
| 219 | + { |
| 220 | + float dt = interval; // Interval is the delta time! |
| 221 | + |
| 222 | + // make it effect by gravity. |
| 223 | + velocity.y += JCS_Constants.GRAVITY * gravityProduct * dt; |
| 224 | + |
| 225 | + // add up velocity. |
| 226 | + startPos += velocity * dt; |
| 227 | + |
| 228 | + // Records the positions. |
| 229 | + points.Add(startPos); |
| 230 | + |
| 231 | + timer += interval; |
| 232 | + } |
| 233 | + |
| 234 | + return points; |
| 235 | + } |
| 236 | + |
| 237 | + /// <summary> |
| 238 | + /// Return a list of arch positions. |
| 239 | + /// </summary> |
| 240 | + /// <param name="pointCount"> This decides how many points you want. </param> |
| 241 | + /// <param name="startPos"> Starting position. </param> |
| 242 | + /// <param name="targetPos"> End position </param> |
| 243 | + /// <param name="vel"> Force velocity. </param> |
| 244 | + /// <param name="gravityProduct"> Arch height. </param> |
| 245 | + public static List<Vector3> GetArchByForce( |
| 246 | + int pointCount, |
| 247 | + Vector3 startPos, Vector3 targetPos, |
| 248 | + float vel, float gravityProduct) |
| 249 | + { |
| 250 | + float distance = Vector3.Distance(targetPos, startPos); |
| 251 | + float time = distance / vel; |
| 252 | + |
| 253 | + return GetArchByTime(pointCount, startPos, targetPos, time, gravityProduct); |
| 254 | + } |
| 255 | + |
| 256 | + #endregion |
180 | 257 | } |
181 | 258 | } |
0 commit comments