Reputation: 4784
i'm developing a simple game, in which there are bricks , ball and a paddle where you should hit the bricks with the ball (you all know this game).
My problem is a bit complicated, the ball is moving in constant speed, let's assume 200px per second, and let's assume i have 30 frames per second. this means that each frame the ball moves about 7 pixels.
i have 2 things i do each frame : 1. draw the game on the screen. 2. move the ball, collision detection etc..
the problem is that in frame X the ball didn't hit anything yet but in frame X+1 he moves 7 pixels and then he is partially inside a brick (or partially inside two adjacent bricks which is much worse).
this happens because the movement is discrete and not continuous.
I don't really know how should i deal with it, i got this problem in every game i developed (not only for android but also in PC). probably i'm missing something crucial in my understanding of game development.
thanks in advance.
Upvotes: 1
Views: 522
Reputation: 13914
It's a bit hefty on the maths, but there's a nice article at
http://www.gamedev.net/page/resources/_/technical/math-and-physics/a-verlet-based-approach-for-2d-game-physics-r2714?
(Due to the _ in the URL it seems to upset the markup for SO…)
Essentially, you're pretty much doing it right — of course, There Is More Than One Way To Do It™. Just make sure you have old(x,y)
and new(x,y)
available while you're looking for collisions, and consider the movement path "tentative" until after you've done the collision-detection. (perhaps present(x,y)
and destination(x,y)
are better terms)
In the case of the Breakout brick bouncing game, you'll be looking for where the line (or vector) of present (x,y) + movement (x,y) → destination (x,y)
intersects with the edge of the brick, and create an angle of reflection with its vertex at the point of intersection. You can quick handily skim over the entire second half of that article about rigid body responses.
Upvotes: 3
Reputation: 751
The simplest solution would be to use some type of physics engine. Box2d is a great 2D physics engine with many wrappers for it written in several languages.
By using a physics engine, you won't have to worry about collision detection. All of that is taken care of for you so that you can focus more on developing the core features of your game.
Upvotes: 0