I’m looking at creating a little mini-golf/putt-putt game at the moment since something of the like has been requested at work.
For getting to grips with 2D vectors, there is little better than ball games, and particularly ones which need not be real time, eg. top-down golf and snooker/pool.
In fact I was in the midst of putting together this golf game when I thought, “why not make a great old mathematical example out of what I’m doing here?” I often see these awesome little Java and Flash applets which assist one in understanding mathematical concepts, for example this one which is a huge help in visualising the dot product of two vectors, as well as having numeric readouts (which I must confess, mine does not have, but does not really need… read on).
The diagram represents the course of a ball hitting an obstacle and rebounding, over the course of a single frame of animation (i.e. there are usually 20-30 of them per second). If you have a ball travelling toward an obstacle and, on it’s next projected update (tick/game-loop cycle) at the current velocity, it will appear inside a wall, then you have a problem. You cannot allow it to appear behind the surface of the solid wall, and you also cannot simply place it touching the wall on the current frame, as this will destroy the illusion of motion at a constant speed, or even constant acceleration (any constant application of force, even if that force is zero) which we have when the ball is travelling, unhindered, through open space.
As seen in this demo, the most logical approach is to reflect it’s motion outside of the surface of the wall by the same depth it would have “penetrated” into the wall, and at an angle which is a reflection the original (you may now excuse the appalling title of this post), on the opposite side of the wall. (In fact, this “mirror effect” is calculated using the surface normal, which you can see as the grey lines which appears when the green and red vectors cross over).
Have a good play. ![]()