I’m trying to build a mini-golf game using AS 2 and I’m basing it on a few maze and billiard game tutorials I’ve seen. My issue is that with certain angles (I can’t find a pattern), the ball leaves the boundary or gets ‘stuck’ in its walls.
Is there a way to keep an object inside of a non-rectangle boundary using hitTest / getBounds?
I’ve attached a scaled down .FLA file if anyone could possibly take a look to see if I’m missing something.
The bulk of the code is in the ‘GolfBall’ mc on the ‘ball’ layer. The power is hardcoded to ‘10’ for the time being (frame 2 of the ‘action’ layer on main timeline).
Move the mouse left / right to adjust the angle.
To recreate the issue, adjust the angle to 175 and click to shoot
Thanks VERY much in advance for any insight!
The problem is you are using simple frame based hitTest detection where it is possible within the duration of a frame for the ball to travel within or beyond the bounds of the walls, and also to collide with more than one surface while only reacting to one.
You need to use a more accurate detection routine that calculates vectors for the extremities of the ball and then interpolates along the vectors to look for collisions in the future within the next frame, and then calculate the result vector and final position. This is mathematically complex, but fortunately there’s plenty code already out there to do this. Pretty much every flash game framework available will have accurate 2d collision detection and physics available.
excellent - thanks VERY much!
Thanks again for pointing me in this direction - after reading over the link you sent, I discovered 2 more in the same series, and another very interesting article regarding vectors:
Links to parts 1 and 3:
Johan van Mol .org - Collision detection & bouncing part 1: intersection of line segments
Johan van Mol .org - Collision detection & bouncing part 3: bouncing balls
another great resource…