Archived

This topic is now archived and is closed to further replies.

Polovitr

Collision in Arcanoid - like game

Recommended Posts

Polovitr    122
Hi, I alluded to some problem while programming my little Arcanoid – like game. Can somebody help me, please? The situation is: For simplyfying exploration let´s assume some paddle 100*1 pixels (width* height) and ball with size 1 px (point – ball). These both sprites are registred in DXSpriteEngine component and in our model paddle doesn´t move, it´s y coordinate is half of form.height (so the paddle is in the middle of screen height in FullScreenMode). The movement of the ball is automatic with variable r (y:=y+r), where r can be positive (movement down), or negative (movement up), and futhermore, let´s assume abs(r) = 5 (increment or decrement of y coordinate always 5 px). We anticipate rebound of ball in the case of collision, so when we detect collision (in teh procedure Ball.DoMove), the variable r changes sign (in the procedure Ball.DoCollision). And the problem is: Wherever we put instruction „detect collision“ (Collision) in the procedure Ball.DoMove, due to biggness of variable r (5 px) and littlenesses of objects (ball and paddle) in the direction of movement (y coord. – 1px), the collision often doesn´t take place and the ball „pass through“ the paddle, because of new possition of the ball is behind the paddle (instruction Collision behind computing y coord.), or the ball is still in front of the paddle („in front of the collision“) (instruction Collision before computing coord. y). And furthermore, when we size up objects now, e.g. from 1 px to 10 px (it means the ball is circle with diameter 10 px and the paddle has height 10 px now), if the collision take place, sometimes the ball „sink“ in the paddle before collision (its computed y coordinate + and its height (10 px) > y coordinate of the paddle – in the case of rebound in the upper plate of the paddle). When I looked at some arcanoid type games I found out, that rebound is allways precisely in upper plate (no sinking or sticking). Do you think, this problem can be solve (prefereby without assembler (asm))? Thanks for your advise. Polovitr

Share this post


Link to post
Share on other sites
Harry Hunt    542
You have to clip the ball...

Here''s some pseudo-code

DeltaY := PaddleY - BallCenterY;

if DeltaY > Increment then
Inc(BallCenterY, 5)
else
Inc(BallCenterY, DeltaY)

{ This will clip your ball to your paddle and prevents it from going right through your paddle }

Now you can do a collision detection like

if DeltaY = 0 then
BallDirectionY := BallDirectionY * -1;


Notice that this is pseudo-code and it needs to be modified to work with your code...but I guess you get the idea...
First clip the ball, then check for collisions

Share this post


Link to post
Share on other sites