
-----------------------------------
jrblast
Wed Jan 18, 2006 10:10 pm

Block Breaker collison detection
-----------------------------------
Okay, so I made a block breaker game with collision detection...(Duh!)...And, well I had it so if it ever hits the block it bounces...Down...Even if it hits a side and not top or bottom...Which makes it act weird...So I tried making it check for collision with each walll...Heres the collision detection (see attatchment for full game and variable explanations)


    for i : 1 .. blocks
        if ballY - ballRad = blockY (i) and ballX - ballRad = blockX (i) - leighway and ballDX > 0 then % check for collison with left wall of block
            score += 100       %increases score if block is hit
            ballDX := -ballDX   %moves ball out of the way a bit
            blockX (i) += maxx      %moves block offscreen where it cant be hit
            fork hitBlockSound

        elsif ballY - ballRad = blockY (i) and ballX - ballRad = blockX (i) - leighway + blockWidth and ballDX < 0 then % check for collison with right wall of block
            score += 100       %increases score if block is hit
            ballDX := -ballDX   %moves ball out of the way a bit
            blockX (i) += maxx      %moves block offscreen where it cant be hit
            fork hitBlockSound

        elsif ballY + ballRad = blockY (i) + blockHeight - leighway and ballX + ballRad >= blockX (i) and ballX - ballRad = blockX (i) and ballX - ballRad  0 then % check for collision with bottom wall of block
            score += 100       %increases score if block is hit
            ballDY := -ballDY   %moves ball out of the way a bit
            ballY += ballDY
            blockY (i) += maxy      %moves block offscreen where it cant be hit
            fork hitBlockSound

        end if
    end for


Ill explain the variables at best I can...Leaving ones that are quite self explanatory out of this...for self explanatory reasons...    for i : 1 .. blocks
        if ballY - ballRad = blockY (i) and ballX - ballRad = blockX (i) then
            score += 100       %increases score if block is hit
            ballY -= ballDY   %makes ball bounce when block is hit
            ballDY := -ballDY   %moves ball out of the way a bit
            blockY (i) += maxy      %moves block offscreen where it cant be hit
 
        end if

    end for


-----------------------------------
Delos
Wed Jan 18, 2006 11:05 pm


-----------------------------------
You seem to have the right idea down.  I find in situations like these (where there are a lot of conditions to check, any many are similar in all) to instead of doing one big line of ands and ors, to break it down into a lot of smaller ones.
So:

if ball moving up then
   if ball hits block on left then
   ...
   end if
elsif ball moving down then
...
end if

This makes debuggin a lot easier.

Also, you may want to have the collision detection based on the ball's motion rather than which part of the block it hits (so basically which part of the ball was hit).  Probably just a matter of preference, and if you can get your method to work instead go for it.

-----------------------------------
jrblast
Wed Jan 18, 2006 11:16 pm


-----------------------------------
You seem to have the right idea down.  I find in situations like these (where there are a lot of conditions to check, any many are similar in all) to instead of doing one big line of ands and ors, to break it down into a lot of smaller ones.
So:

if ball moving up then
   if ball hits block on left then
   ...
   end if
elsif ball moving down then
...
end if

This makes debuggin a lot easier.

Also, you may want to have the collision detection based on the ball's motion rather than which part of the block it hits (so basically which part of the ball was hit).  Probably just a matter of preference, and if you can get your method to work instead go for it.

Okay, cool, thanks, I gotta sleep now, but I'll try it tommorow. I'll also try to troubleshoot it the way i mentioned in the crummy tut i just posted :oops:

-----------------------------------
Drakain Zeil
Thu Jan 19, 2006 6:19 pm


-----------------------------------
Recently I've found myself looking for all the shortcuts that I can get to do things. It cuts down my code by quite a lot. 

Anyway, unless my eyes are fooling me, all of the code inside of your IF statements are the exact same thing, cut it down to one if tree branch, instead of the huge ammount you have. I would also look at how to fix up those if statements, they look entirely longer than needed... but I'm not going to look into them, simply because they are way too long =/

-----------------------------------
jrblast
Thu Jan 19, 2006 8:39 pm


-----------------------------------
Recently I've found myself looking for all the shortcuts that I can get to do things. It cuts down my code by quite a lot. 

Anyway, unless my eyes are fooling me, all of the code inside of your IF statements are the exact same thing, cut it down to one if tree branch, instead of the huge ammount you have. I would also look at how to fix up those if statements, they look entirely longer than needed... but I'm not going to look into them, simply because they are way too long =/

Yah, but if you look at my post count title (Newbe programmer), Well, I find it quite accurate :P still working on my skills...Which sadly, compared to my friends who actualy have the course, are totaly 1337

-----------------------------------
Rasta Fella
Thu Jan 19, 2006 10:47 pm


-----------------------------------
jrblast only two more post untill you are a "programmer"

-----------------------------------
sylvester-27
Fri Jan 20, 2006 11:31 am


-----------------------------------
jrblast only two more post untill you are a "programmer"

i don't think that is really helping him and is a waste of a post. jrblast, i might be wrong but if you use whatdotcolor. That way when the ball hits a certain color, you could make your block blow up.

-----------------------------------
jrblast
Fri Jan 20, 2006 11:17 pm


-----------------------------------
Well, as I learned after about 5 minutes on this forum when my friend told me bout it, whatdotcolour is frowned upon...because it seems to suck lol, I think i got it working mostly, just a couple bugs....that and my comps messed up :O i'll fix it later...
