What's a good algorithm for "bouncing cards" like the ones you see in solitaire games?
What's the coolest card animation you've seen?
Edit - Any besides the Windows game?
What's a good algorithm for "bouncing cards" like the ones you see in solitaire games?
What's the coolest card animation you've seen?
Edit - Any besides the Windows game?
The x-axis velocity is constant. The y-velocity is incremented by some value every frame. Each frame, the current x and y positions are incremented by the respective velocities. If the card would end up below the window, y-velocity is multiplied by something like -0.9. (negative number > -1) This produces the series of descending bounces.
两部分:
通过每秒更新和重绘卡片若干次来为其设置动画,每次都改变卡片的位置。简单的方法是计算类似(伪 Python):
vel_x = # some value, units/sec
vel_y = # some value, units/sec
acc_x = # some value in units/sec^2
acc_y = # some value in units/sec^2
while not_stopped():
x = x+vel_x
y = y+vel_y
# redraw the card here at new position
if card_collided():
# do the bounce calculation
vel_x = x + (0.5 * acc_x) # 1st derivative, gives units/sec
vel_y = y + (0.5 * acc_y)
只要卡片与边保持四方形,那么当卡片中心与墙壁之间的距离为适当的宽度或高度的 1/2 时,您就会与边发生碰撞。
在与 Charlie 提供的代码苦苦挣扎了一个小时左右之后,我想出了正确的算法(在彻底阅读了递归的响应之后)。在真正的 Python 中:
def bouncing_cards():
x = 0.0
y = 0.0
vel_x = 3.0
vel_y = 4.0
while x < windowWidth:
drawImage(img, x, y)
x += vel_x
y += vel_y
vel_y += 1.0
if y + cardHeight >= windowHeight:
y = windowHeight - cardHeight
vel_y *= -0.9
使用 wxPython 提供以下内容:http: //i.imgur.com/t51SXVC.png :)