0

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?

4

3 回答 3

5

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.

于 2008-12-30T22:01:50.677 回答
2

两部分:

  1. 垂直方向的运动由二阶方程控制,如d=1/2at²。对于地球,当然,a= 32 ft/sec²,但你必须调整常数。
  2. 当卡片碰到边缘时,正如“递归”所说,速度矢量乘以 -1 乘以垂直于表面的分量。如果您希望它很好地反弹到停止位置,请将 -1 设置为稍小的值,例如 -0.9。

通过每秒更新和重绘卡片若干次来为其设置动画,每次都改变卡片的位置。简单的方法是计算类似(伪 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 时,您就会与边发生碰撞。

于 2008-12-30T23:26:20.910 回答
1

在与 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 :)

于 2013-11-22T20:10:16.283 回答