I am currently making a brick breaker game (for fun). I have my bat and ball interacting but I'm having a slight problem angling the ball when it hits certain areas of the bat.
Currently my ball is in a loop and adds +\- 1 to the X axis, +\-1 to the Y axis and repaints the ball in a direction accordingly. That's fine and it works. But the problem is, this method limits me to 8 directions, which isn't enough.
I've tried +/-2 to add different angles but the problem is that it visually looks as though the ball speed has increased.
Does anybody have any suggestions? Maybe the only option is the trig' route :eek:
Thanks in advance
It looks like it's moving faster because it is. Originally it was moving 1 pixel, now it's moving 2 so it is going twice as fast in that direction. I'd imagine to get it more realistic you are going to have to use math and figure which angle it hit the bat at and find a new angle it should come off at. Others may have more advice. I do believe that the more you want it to be able to vary the more math is going to be required.
Yes I know that, but to other users it would appear to be going faster (sorry if i didnt explain myself, its late)
I guess the only option is maths.
Thankfully the math shouldn't be too hard. If you know the circle and the angle you can find a point quite easily.
In the painting routine, cast the Graphics to Graphics2D and you can draw(...) Shapes like Ellipse2D.Double, Rectangle2D.Double etc. See the documentation for Shape to get a list of the implementing classes.
Don't forget to set an appropriate RenderingHint to take advantage of anti-aliasing, which will improve the sub-pixel rendering at a minor cost of reduced sharpness (hardly noticeable except if you're really looking for it).
That way, you're not limited to integer displacements.
Originally Posted by mahoomerry
Thanks for the responses. I had a feeling initially trig was going to be involved but thought I'd managed to think of a work around, obviously not!