Map bounce algorithm

What's a good algorithm for "bouncing cards" like the ones you see in solitaire games?

What's the coolest map animation you've seen?

Edit - Anyone other than playing on Windows?

0


source to share


3 answers


The x-axis speed is constant. Y-speed increases by some value for each frame. Each frame, the current x and y positions are incremented at the appropriate speed. If the map is below the window, the y-speed is multiplied by -0.9. (negative number> -1) This creates a series of downward bounces.



+5


source


Two parts:

  • movement in the vertical direction governed by a second-order equation as d = 1 / 2at [sup2 ;. For the Earth, of course, a = 32 ft / sec & sup2; but you will need constants.
  • When the map hits the edge like "Recursive" it says that the velocity vector is multiplied by -1 times normal to the surface. If you want it to bounce your stop well, make -1 a slightly smaller value, like -0.9.

Animate it by updating and redrawing the map several times per second, changing the position of the map each time. An easy way is to compute something like (pseudo-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)

      

As long as the cards remain four square with sides, you are faced with sides when the distance between the center of the card and the wall is 1/2 the width or height if necessary.

+2


source


After wrestling with the code Charlie provided for an hour or so, I came up with the correct algorithm (after reading the recursive answer completely). In real 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

      

Gives the following usage of wxPython: http://i.imgur.com/t51SXVC.png :)

+1


source







All Articles