弹跳卡算法

时间:2008-12-30 21:44:34

标签: animation

像你在单人纸牌游戏中看到的“弹跳牌”的优秀算法是什么?

你见过最酷的卡片动画是什么?

编辑 - 除了Windows游戏之外还有什么?

3 个答案:

答案 0 :(得分:5)

x轴速度是恒定的。每帧的y速度增加一些值。每个帧,当前的x和y位置增加各自的速度。如果卡片最终落在窗口下方,则y-velocity乘以-0.9之类的值。 (负数> -1)这会产生一系列下降反弹。

答案 1 :(得分:2)

两部分:

  1. 垂直方向的运动是 由二阶方程控制, 比如 d = 1 /2at²。对于地球而言 当然,a = 32英尺/秒²但是 你必须扭动 常数。
  2. 当卡片撞到边缘时,如 “递归”说,速度 向量乘以-1的倍数 组件垂直于表面。如果 你希望它能很好地反弹到一个 停下来,稍微调整一下-1 较小的值,如-0.9。
  3. 通过每秒更新和重新绘制卡片的次数来设置动画,每次更改卡片的位置。简单的方法是计算类似(伪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时,就会与侧面发生碰撞。

答案 2 :(得分:1)

在查理提供的代码挣扎了一个小时之后,我想出了正确的算法(彻底阅读了递归的反应之后)。在真正的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:)