
-----------------------------------
Turing_Noob
Sun May 10, 2009 9:48 am

collision detection/eating another bubble help
-----------------------------------
I have an assignment due 2morrow that involves a yellow circle eating some other smaller circles.



So far, i've been able to make the circles poped up and make the yellow circle move but not i need to find out how to make the yellow bubble grow by 5 pixals if i get the little red bubble. This is my code so far so can some please tell me how to make the yellow bubble eat the red bubble and then having the yellow bubble incrase by 5 pixals every time it eats the red bubble thx.




var chars : array char of boolean
var x, y : int                              %variable for the location of yellow ball
var sizeX : int                             %variable for the horizontal radius of yellow ball
var sizeY : int                             %variable for the vertical radius of yellow ball
var red_cookiex : int
var red_cookiey : int
var green_cookiex : int
var green_cookiey : int
var orange_cookiex : int
var orange_cookiey : int
var cyan_cookiex : int
var cyan_cookiey : int
var magenta_cookiex : int
var magenta_cookiey : int
var counter : int := 1                      %used to delay the appearance of the cookies
var is_red_cookie : boolean := false        %makes the red cookie appear on the screen
var is_green_cookie : boolean := false      %makes the green cookie appear on the screen
var is_orange_cookie : boolean := false     %makes the orange cookie appear on the screen
var is_cyan_cookie : boolean := false       %makes the cyan cookie appear on the screen
var is_magenta_cookie : boolean := false    %makes the magenta cookie appear on the screen
var collision_with_red_cookie : real

x := 320
y := 220
sizeX := 15
sizeY := 15

View.Set ("graphics,offscreenonly, nobuttonbar")
colourback (16)
cls


loop
    Input.KeyDown (chars)
    drawfilloval (x, y, sizeX, sizeY, 16)
    delay (30)
    if chars (KEY_LEFT_ARROW) and x > sizeX then
        %move left and creates a border on the left side
        x := x - 5
    end if
    if chars (KEY_RIGHT_ARROW) and x < maxx - sizeX then
        %move right and creates a border on the right side
        x := x + 5
    end if
    if chars (KEY_UP_ARROW) and y < maxy - sizeX then
        %move up and creates a border on the top side
        y := y + 5
    end if
    if chars (KEY_DOWN_ARROW) and y > sizeX then
        %move down and creates a border on the bottom side
        y := y - 5
    end if

    drawfilloval (x, y, sizeX, sizeY, 14) %draw yellow ball

    if counter > 50 then

        if is_red_cookie then
            drawfilloval (red_cookiex, red_cookiey, 5, 5, 16)
            is_red_cookie := false
        else
            randint (red_cookiex, 5, maxx - 5)
            randint (red_cookiey, 5, maxy - 5)
            drawfilloval (red_cookiex, red_cookiey, 5, 5, brightred)
            is_red_cookie := true
        end if

        if is_green_cookie then
            drawfilloval (green_cookiex, green_cookiey, 5, 5, 16)
            is_green_cookie := false
        else
            randint (green_cookiex, 5, maxx - 5)
            randint (green_cookiey, 5, maxy - 5)
            drawfilloval (green_cookiex, green_cookiey, 5, 5, green)
            is_green_cookie := true
        end if

        if is_orange_cookie then
            drawfilloval (orange_cookiex, orange_cookiey, 5, 5, 16)
            is_orange_cookie := false
        else
            randint (orange_cookiex, 5, maxx - 5)
            randint (orange_cookiey, 5, maxy - 5)
            drawfilloval (orange_cookiex, orange_cookiey, 5, 5, 43)
            is_orange_cookie := true
        end if

        if is_cyan_cookie then
            drawfilloval (cyan_cookiex, cyan_cookiey, 5, 5, 16)
            is_cyan_cookie := false
        else
            randint (cyan_cookiex, 5, maxx - 5)
            randint (cyan_cookiey, 5, maxy - 5)
            drawfilloval (cyan_cookiex, cyan_cookiey, 5, 5, cyan)
            is_cyan_cookie := true
        end if
        if is_magenta_cookie then
            drawfilloval (magenta_cookiex, magenta_cookiey, 5, 5, 16)
            is_magenta_cookie := false
        else
            randint (magenta_cookiex, 5, maxx - 5)
            randint (magenta_cookiey, 5, maxy - 5)
            drawfilloval (magenta_cookiex, magenta_cookiey, 5, 5, magenta)
            is_magenta_cookie := true
        end if
        counter := 1    collision_with_red_cookie := sqrt ((x - red_cookiex) ** 2 + (y - red_cookiey) ** 2)
    if collision_with_red_cookie = sizeX + 5 = false then
        sizeX := sizeX + 5
        sizeY := sizeY + 5
    end if
    end if

    counter := counter + 1
    View.Update
end loop>

-----------------------------------
TheGuardian001
Sun May 10, 2009 12:23 pm

Re: collision detection/eating another bubble help
-----------------------------------
Okay, having looked over your code properly this time, I can now safely say it does matter where you put your collision detection.

you currently have it inside an if statement (if counter > 50) that will only run once each time the cookie shows up. this means that unless the cookie shows up on top of them, you won't see the collision.

Next up, you seem to be going at collisions in a very roundabout way.

done properly, collisions should look something like this

if (ball_2_x - ball_1_x) ** 2 + (ball_2_y - ball_1_y) ** 2  50 statement) then you should be able to detect collisions, and do whatever you want based on that collision.

Oh, and as a note, this means that you don't need the collision detection stuff inside of the (if counter > 50) statement. Take that out entirely and use this method outside of that if statement.

-----------------------------------
OneTwo
Sun May 10, 2009 12:43 pm

Re: collision detection/eating another bubble help
-----------------------------------
Edit: The solution up top was much better.

-----------------------------------
Turing_Noob
Sun May 10, 2009 12:55 pm

RE:collision detection/eating another bubble help
-----------------------------------
TheGuardian001, your solution doesn't work. I still need help

-----------------------------------
OneTwo
Sun May 10, 2009 3:03 pm

Re: collision detection/eating another bubble help
-----------------------------------
TheGuardian001's solution is correct, here's his solution:



...

    collision_with_red_cookie := sqrt ((x - red_cookiex) ** 2 + (y - red_cookiey) ** 2)
    if collision_with_red_cookie 