Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Block Breaker collison detection
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jrblast




PostPosted: Wed Jan 18, 2006 10:10 pm   Post subject: 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)

code:

    for i : 1 .. blocks
        if ballY - ballRad <= blockY (i) + blockHeight and ballY + ballRad >= blockY (i) and ballX - ballRad <= blockX (i) + leighway 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) + blockHeight and ballY + ballRad >= blockY (i) and ballX - ballRad <= blockX (i) + leighway + blockWidth 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 ballY + ballRad >= blockY (i) + blockHeight - leighway and ballX + ballRad >= blockX (i) and ballX - ballRad <= blockX (i) + blockWidth and ballDY < 0 then %check for collision with top 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

        elsif ballY + ballRad <= blockY (i) + leighway and ballY + ballRad >= blockY (i) - leighway and ballX + ballRad >= blockX (i) and ballX - ballRad <= blockX (i) + blockWidth and ballDY > 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...[end bad joke]..Oh, and i only dont open tags before closing them when they're not real tags Rolling Eyes

ballDX and ballDY: The number of pixels the ball moves each frame, sometimes has a negative value.

blockX and blockY: the arrays in which each blocks x and y value (Bottom right) are stored

ballX and ballY: The variabls that hold the CENTER of the ball

ballRad: ball radius

And my old collision detection (shorter....by far)

code:
    for i : 1 .. blocks
        if ballY - ballRad <= blockY (i) + blockHeight and ballY + ballRad >= blockY (i) and ballX - ballRad <= blockX (i) + blockWidth 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



block breaker 1024x768.t
 Description:

Download
 Filename:  block breaker 1024x768.t
 Filesize:  8.58 KB
 Downloaded:  129 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Wed Jan 18, 2006 11:05 pm   Post subject: (No subject)

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:
psuedo:

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




PostPosted: Wed Jan 18, 2006 11:16 pm   Post subject: (No subject)

Delos wrote:
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:
psuedo:

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 Embarassed
Drakain Zeil




PostPosted: Thu Jan 19, 2006 6:19 pm   Post subject: (No subject)

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




PostPosted: Thu Jan 19, 2006 8:39 pm   Post subject: (No subject)

Drakain Zeil wrote:
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 Razz still working on my skills...Which sadly, compared to my friends who actualy have the course, are totaly 1337
Rasta Fella




PostPosted: Thu Jan 19, 2006 10:47 pm   Post subject: (No subject)

jrblast only two more post untill you are a "programmer"
sylvester-27




PostPosted: Fri Jan 20, 2006 11:31 am   Post subject: (No subject)

Quote:
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




PostPosted: Fri Jan 20, 2006 11:17 pm   Post subject: (No subject)

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...
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: