Guys, I need some help with acceleration/deceleration script. I made some extension for sprite class to enable it perform various animations such as moving to various direction, darken, lighten, fading, shaking, etc. The script below is just a
simplification of the actual script and not tested yet, I'm sorry if I made some errors; The actual script works fine however. I want to modify the linear moving animation with some acceleration/deceleration capabilities but I can't figure out how, I'm suck at math and physics.
FPS = 40
class Animated < Sprite
attr_accessor :in_animation
@in_animation = false
def initialize
#assign some picture for this class
end
def move(distance,duration) #distance = distance to travel in pixels ; #duration = time for travel distance in seconds
#declaration of variables which I think necessary for acceleration/deceleration
@distance_total = distance
@distance_covered = 0
@distance_remaining = @distance_total
@time_total = FPS * duration
@time_spent = 0 #in frames
@time_remaining = @time_total #in frames
@current_speed = 0
@last_speed = 0
@speed_type = 'constant';
@in_animation = true
end
def animate()
#calculate how many pixel to move in a frame
@current_speed = @distance_total/@time_total
#move the thing to left (
$yeah.x -= current_speed
#set some variables
@distance_covered += @current_speed
@time_spent += 1
#ends the animation if the object has reach the destination
if @distance_covered >= @distance_total && @time_spent >= @time_total
@in_animation = false
end
end
end
Now, in the game loop's update function :
if $yeah.in_animation == true
$yeah.animate()
end
Lets start, interact an event with these script :
$yeah = Animated.new
$yeah.move(100,1)
After that, the picture is moving to left for 100 pixels in 1 second, in the actual script, this works fine. But that's it, linear movement.
The question is how to turns the linear movement into dynamic movement with below scenario
1. After moving for 60% of frames in constant speed, movement start to decelerate
slowly (speed dropping from frame to frame) until it stops upon reaching final destination
2. Decelerating
must not have any impact to total time and total distance
Any help will be appreciated!
thx before.