Rebounding of any angle
BlitzMax Forums/BlitzMax Programming/Rebounding of any angle
| ||
I am sure code exists somewhere for this. Especially for pinball type games. However, what method would you use to enable a ball to rebound off any angle such as demonstrated here: |
| ||
I'd create an array of vectors that represent the 'walls'* and then check for collisions with a lines_intersect function (see code archives), i.e. check each wall vector for a collision with the ball trajectory/movement vector. Once you've found a collision, you can calc the normal of the wall vector (or have them pre-calced) and calculate your reflection vector from there. Can't give any code as I don't have bmax. :P * you could knock up a quick editor to make setting these vectors easy. |
| ||
Thanks. As it happens I have been playing with that code. It's very nice too. For defining circular areas though the process would be fairly long-winded and prone to error. Still, if line/ball intersection is the way to go then the effort is worth it Secondly, what about slopes in the ground? By now you can guess I am thinking of a 2D golf game type thing here Any ideas how you would tackle slopes like this? .. |
| ||
Any ideas how you would tackle slopes like this? Store the information/pixmap based on colour (or another map where slopes are defined = heightmap) on an array, and then read from this one the 'color-height' and move accordingly to the moving vector (you can rise or go down a slope...) |
| ||
Good idea I have hit a snag with Line intersect <> ball code from the archives though. When a new line is added for the collision test, the ball will deflect regardless of where the line ends It's as if an imaginary line goes all the way across the screen The RED line has been added in this screenshot. The ball bounces as shown and cannot 'pass by' where the line ends: The line only works one way too. Depending which way the line is draw it is possible for the ball to pass straight through. After which it rebounds back as expected Any other ways to resolve this? Help appreciated What I have so far .. MAIN SOURCE #################################### IMPORT code "BallLineColl.bmx" |
| ||
This has been asked so many times before. I've got a snippet in the code archives, and a nice post with pictures here. |
| ||
Yeah I made a big post about it a while back. It's actually quite hard to get perfect. |
| ||
There is a Reflected method that returns the reflected vector in my TVec2 object where you: -Create a incident vector - like your ball -Create a wall vector (normalized or not - it doesn't matter) -Then you call: Reflected_Vector = Incident_Vector.Reflected(Wall_Normal) The Vector object is here: http://blitzbasic.com/codearcs/codearcs.php?code=2320#comments |