Game Development Reference
In-Depth Information
The kjAjpanBn]ia event handler is responsible for using these variables to calculate velocity:
lner]pabqj_pekjkjAjpanBn]ia$arajp6Arajp%
w
nk^kp*rt9nk^kp*t)nk^kp*kh`T7
nk^kp*ru9nk^kp*u)nk^kp*kh`U7
nk^kp*kh`T9nk^kp*t7
nk^kp*kh`U9nk^kp*u7
++@eolh]unk^kp#orahk_epuej]`uj]ie_patpbeah`
`eolh]u*patp9Opnejc$RT6'nk^kp*rt'Xj'RU6'nk^kp*ru%7
y
It might look a little convoluted, but what's happening is really very mundane:
1. The object's velocity is calculated by comparing its current position with the position that it
was at in the previous frame. kh`T and kh`U store the object's previous position. For example,
if the object was at position 100 in the previous frame and at 105 in the current frame, its
velocity would be 5.
nk^kp*rt9nk^kp*t)nk^kp*kh`T7
nk^kp*ru9nk^kp*u)nk^kp*kh`U7
2. You need to store the object's current position in the kh`T and kh`U variables so that you can
access these values in the next frame. The object's current position becomes its old position
when the frame advances:
nk^kp*kh`T9nk^kp*t7
nk^kp*kh`U9nk^kp*u7
With this system, you can capture an animated object's velocity and use it for any other calculations
or collision detection that your game might need to do.
Although this system for calculating velocity will hold you in good stead in a situation like this, you
might work on a game in which you need to calculate velocity and update the player's position at the
same time. In that case, you need to use additional variables that temporarily store the current t and
u values. This is necessary so that current x and y values aren't lost when the new velocity is added to
the object's position.
++Pailkn]nehuopknapdalh]uan#o_qnnajplkoepekj
pailkn]nuT9t7
pailkn]nuU9u7
++?]h_qh]parahk_epu
rt9t)kh`T7
ru9u)kh`U7
++Ql`]palh]uan#olkoepekj
t'9rt7
u'9ru7
 
Search Nedrilad ::




Custom Search