
-----------------------------------
dffhkhksg
Sun Jan 01, 2012 5:59 pm

Help with this Collision Detection
-----------------------------------
What is it you are trying to achieve?
I want to create a brick breaker game, with a moving ball and column/rows for the bricks


What is the problem you are having?
Various glitches where the ball passes through a portion of the brick before detecting a collision, but the main problem i need fixed is that on certain events (ie ball hits a brick while it is moving up and left) which cause the wrong brick to be drawn (well, erased)


Describe what you have tried to solve this problem
Tweaking the parameters on the draw command, but i think I need some if-statements to truly fix the problem


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
I basically have 9 checks around the ball and use whatdotcolour, an example of this would be


procedure erasebrick (column, row : int) %, above : string)
    if movey  0 then %Ball is moving up
        if movex  0 then %Right
            drawfillbox (column * 20 + 1, row * 10 + 3, (column + 1) * 20, (row + 1) * 10 + 2, white)
            bricksleft += -1
        end if
    end if
    locate (1, 1)
    put bricksleft
end erasebrick

%

elsif (whatdotcolour (round (ballx) + 20, round (bally) - 21) not= white) then     %Hits a brick below the ball
                erasebrick ((ballx + 20) div 20, (bally - 21) div 10)




Please specify what version of Turing you are using
4.1

-----------------------------------
Dreadnought
Sun Jan 01, 2012 8:57 pm

Re: Help with this Collision Detection
-----------------------------------
I believe that your problem comes from the fact that while the center of the ball may be in one column/row its edges can collide with rectangles in other rows.

I see what you're trying to do with the columns and rows, but I think you've complicated this more than its needs to be.

You say that you check the colour of 9 points for collision. Well if a dot is not white then that single point must be in a rectangle. So the coordinates of that point can tell you the rectangle you hit. Now you just have to figure out which way to make the ball bounce afterwards.

Good luck.

-----------------------------------
Tony
Sun Jan 01, 2012 9:49 pm

RE:Help with this Collision Detection
-----------------------------------
- presumably a ball has more than 9 points to it, so some angles don't check for collision, until the ball is far enough into the block for other points to also "collide"
- if the ball is travelling "fast" (anything greater than 1pixel-per-frame really), it can make it up to (velocity - 1) pixels into the block before you do the check for "is the ball colliding right now"

-----------------------------------
chipanpriest
Mon Jan 02, 2012 12:32 pm

Re: Help with this Collision Detection
-----------------------------------
I'm making a space invaders game and for the enemies, i just use a boolean variable to decide if theyre alive or not and all the enemies are in an array. Example:
for i : 1 .. 10 % 10 enemies
    if enemy (i) = true then % if theyre alive or not
        if By + 20 >= E1y1 - 20 and Bx + 5 >= E1x (i) - 20 and Bx - 5 