Skip to content

Commit 98cf13a

Browse files
committed
feat(Action): Add simulation for throw action
1 parent 9f2a876 commit 98cf13a

File tree

1 file changed

+79
-2
lines changed

1 file changed

+79
-2
lines changed

Assets/JCSUnity/Scripts/Actions/3D/JCS_3DThrowAction.cs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* $Notice: See LICENSE.txt for modification and distribution information
77
* Copyright (c) 2016 by Shen, Jen-Chieh $
88
*/
9+
using System.Collections.Generic;
910
using UnityEngine;
1011
using MyBox;
1112

@@ -143,11 +144,12 @@ public void ThrowByTime(Vector3 targetPos, float time)
143144
{
144145
Vector3 displacement = targetPos - this.transform.position;
145146

147+
// Calculate initial velocity.
146148
mVelocity.x = displacement.x / time;
147149
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;
149151

150-
// start dropping.
152+
// start the action.
151153
this.mActive = true;
152154
}
153155

@@ -177,5 +179,80 @@ public void ThrowByForce(Vector3 targetPos, float vel)
177179

178180
ThrowByTime(targetPos, time);
179181
}
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
180257
}
181258
}

0 commit comments

Comments
 (0)