Actionscript physics of curveball
September 19, 2008 5:23 PM   Subscribe

Flash-physics-filter: I'm working on a simple flash game similar to curveball, but I can't figure out 1) how the script determines the amount of spin/curve to put on the ball, and 2) how the AI gets progressively better as the levels increase.
posted by c:\awesome to Computers & Internet (11 answers total)
 
Response by poster: Forgot a bit: Do any of you Actionscript folk know the fancy wizardry behind this seemingly black magic?
posted by c:\awesome at 5:43 PM on September 19, 2008


In theoretical terms, I imagine it's easiest to first code a perfect opponent - one that moves towards the correct spot every time. Then, slow it down for the easier levels so that it often won't have time to reach the correct point.
posted by chrisamiller at 5:55 PM on September 19, 2008


Response by poster: That's what I was thinking too. The curve physics still has me stumped, however.
posted by c:\awesome at 5:59 PM on September 19, 2008


Best answer: I'm not positive, but it seems to me that the amount of curve depends on the velocity and direction of the paddle when the ball hits. If you just put the paddle in the ball's path and hold it still, you don't get any curve; if you're moving the paddle, you get some spin.

chrisamiller's method is probably the easiest way to do the AI. A slight improvement might be to make the opponent take longer to figure out where to move to when you put curve on the ball--i.e. the more curve you give it, the longer the opponent has to just sit there staring before he figures out where to go.
posted by equalpants at 6:06 PM on September 19, 2008


It seems that the curve is based somewhat on the velocity of the paddle when the ball hits. And the AI just seems to move a bit more quickly (as does the ball) as the levels increase.
posted by sanko at 6:06 PM on September 19, 2008


Response by poster: How do you determine the velocity / angle of the paddle at the moment of impact?
posted by c:\awesome at 6:12 PM on September 19, 2008


Best answer: How do you determine the velocity / angle of the paddle at the moment of impact?

You know the position of the paddle at impact right? And do you have some kind of time counter in the game? In that case, you could find a velocity just by comparing the position and time at impact with the position and time from the previous frame/time unit/whatever. So you would need to always store the paddle's previous position in memory.

position at impact: (x1,y1); time: t1
position just before impact: (x0,y0); time: t0

delta_distance = ((x1-x0)^2 + (y1-y0)^2)^0.5
delta_time = (t1-t0)
vel = delta_distance/time
posted by theyexpectresults at 7:43 PM on September 19, 2008


Angle is found from:

angle = atan((y1-y0) / (x1-x0))

Except you also need to write some code to check for angle wrap-around (i.e. which quadrant the angle is in) and check for dividing by zero.

I hope that might be useful, I am not a progammer.
posted by theyexpectresults at 7:48 PM on September 19, 2008


Just another point about the AI (back from when I was addcited to curveball).

I think that it encouraged you to curve it more to beat harder opponents. So from what I recall, the AI on the first level would look at the projected trajectory of the ball as soon as it left your paddle, and move to that position. So any curve at all would beat them. On later levels the AI would wait a bit to see where the ball was curving so it required more violent curving (and RSI in your wrist) to beat it.

This is probably all in my mind though, and in any case just making a variable to slow down the perfect opponent (like chrisamiller said) is a much simpler and better idea.
posted by theyexpectresults at 7:55 PM on September 19, 2008


angle = atan((y1-y0) / (x1-x0))

Translated into Actionscript, that's angle = Math.atan2((y1-y0) / (x1-x0))
posted by grumblebee at 8:12 PM on September 19, 2008


If you find you still can't figure it out, you could try emailing Keith ("Making Things Move") Peters. He'd probably enjoy reading this thread.
posted by grumblebee at 8:14 PM on September 19, 2008


« Older How can I start getting square with my HECS debt?   |   what's the worse that could happen in finances... Newer »
This thread is closed to new comments.