Converting this explict sequence into a recusrive sequence.

CoolShank8

New member
Joined
Aug 14, 2020
Messages
4
local function GetPositionAtTime(Time, Origin, IntitalVelocity, Acceleration)

local Force = Vector3.new((Acceleration.X * Time^2) / 2,(Acceleration.Y * Time^2) / 2, (Acceleration.Z * Time^2) / 2)

return Origin + (IntitalVelocity * Time) + Force

end

Basically I have this function someone wrote, I want to convert it into an recursive sequence meaning depending on it's last position and last velocity and time from previousframe. I don't understand how I would do this. I mean I know how to do it only when velocity is involved.

Position = CurrentPosition + (Currentvelocity * TimeFromPreviousFrame)

However when extra forces are involved im totally confused :\ Also please don't tell me the answer just give me an idea how to get started I want to learn how to do it, so next time I won't ask this question
 
You probably don't want a recursive function (one that calls itself repeatedly until some "end condition" is met). You probably want a function that will update the the velocity and position of an object (and persist these new values) GIVEN:- a "delta time"; a starting position; a starting velocity and a constant acceleration (constant for that particular delta time). Is this correct? Are you familiar with equations of motion?

EDIT: The function described could then be called multiple times. To obtain 1 second of motion for example you could call the function 10 times with deltaTime=0.1, or call it once with deltaTime=1.0 (you'd choose a deltaTime depending on how fast the acceleration is changing OR if you want to tabulate/animate the movement steps)
 
Last edited:
By recursive function I don't mean like that, I mean a recursive sequence.
Position = CurrentPosition + (Currentvelocity * TimeFromPreviousFrame)


This thing is what I mean by a recursive sequence I probably worded it wrong. What I mean is something which uses the current position to get to the next position. Using the current term to get to the next term.




GIVEN:- a "delta time"; a starting position; a starting velocity and a constant acceleration (constant for that particular delta time). Is this correct? Are you familiar with equations of motion?


yes kind of, I need something where I can change the trajectory on the fly. That includes acceleration and velocity. (Bouncing Projectiles)

Position = CurrentPosition + (Currentvelocity * TimeFromPreviousFrame) For this one I can do that.

However for this Position = Origin + (Velocity * totalElapsedTime) If I change the velocity for example flip it by a negative when the projectile is hit it won't work.


The function described could then be called multiple times. To obtain 1 second of motion for example you could call the function 10 times with deltaTime=0.1, or call it once with deltaTime=1.0 (you'd choose a deltaTime depending on how fast the acceleration is changing OR if you want to tabulate/animate the movement steps)

Yah I have this part figured there's already a event which lets you run your code every frame 1/60 second, and gives you back the TimeFromPreviousFrame
 
Last edited:
If you need to change velocity and acceleration on the fly then it's best if your function uses the previous frame's position as input rather than starting from the origin all the time. In other words, instead of doing this...

Code:
// Frame 1
positon = origin
draw(...)
// Frame 2
position = GetPositionAtTime( 1/60, origin, ...)
draw(...)
// Frame 3
position = GetPositionAtTime( 2/60, origin, ...)
draw(...)

It would be better to do this...

Code:
// Frame 1
position=origin
velocity=<whatever>
accel=<whatever>
draw(...)
// Frame 2
[position,velocity] = UpdatePosition( 1/60, position, velocity, accel)
draw(...)
// Frame 3
[position,velocity] = UpdatePosition( 1/60, position, velocity, accel)
draw(...)

...and you could add lines to change the velocity, say after an impact, between each frame and the physics would appear correct

Take a look at this link for equations of motion that include acceleration. I'd use equation 2 then equation 1 from that link to update the position and velocity. Your function will need to return new values for these two variables. I might not be around for a couple of days, but hopefully others will help if you have more questions.
 
Top