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

Username:   Password: 
 RegisterRegister   
 collision detection/eating another bubble help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Turing_Noob




PostPosted: Sun May 10, 2009 9:48 am   Post subject: collision detection/eating another bubble help

I have an assignment due 2morrow that involves a yellow circle eating some other smaller circles.
<Answer Here>


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.


Turing:


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
>
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Sun May 10, 2009 12:23 pm   Post subject: 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
Turing:

if (ball_2_x - ball_1_x) ** 2 + (ball_2_y - ball_1_y) ** 2 <= (ball_1_radius + ball_2_radius) ** 2 then %this is the equation for circle collisions
    collision_detected %whatever you want to happen if you see a collision.
end if

if you use that formula (OUTSIDE of your if counter > 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




PostPosted: Sun May 10, 2009 12:43 pm   Post subject: Re: collision detection/eating another bubble help

Edit: The solution up top was much better.
Turing_Noob




PostPosted: Sun May 10, 2009 12:55 pm   Post subject: RE:collision detection/eating another bubble help

TheGuardian001, your solution doesn't work. I still need help
OneTwo




PostPosted: Sun May 10, 2009 3:03 pm   Post subject: Re: collision detection/eating another bubble help

TheGuardian001's solution is correct, here's his solution:

Turing:


...

    collision_with_red_cookie := sqrt ((x - red_cookiex) ** 2 + (y - red_cookiey) ** 2)
    if collision_with_red_cookie <= (sizeX + 5) then %this is the equation for circle collisions
        sizeX += 50
        sizeY += 50
        Draw.Oval(red_cookiex, red_cookiey, 5, 5, 16)
        red_cookiex := 0
        red_cookiey := 0
    end if
   
    counter := counter + 1
    View.Update
end loop


Explanation
Your original code had the collision detection inside an if statement which in-turn doens't work because it only checks when new dots are drawn not when your yellow oval moves. To fix that problem, you have to move the collision detection code outside of the if statement like so:


code:

loop
<CODE FOR THE MOVEMENT OF THE YELLOW OVAL>

if counter > 50 then
     ...
end if

<COLLISION DETECTION HERE>
end loop



Every single time the oval moves, it checks for collisions. As for the other code:

code:
Draw.Oval(red_cookiex, red_cookiey, 5, 5, 16)
red_cookiex := 0
red_cookiey := 0


The Draw.Oval draws a black oval on the red cookie when the yellow oval touches it. Also, it sets the x, y co-ordinates of the red cookie to 0 or else you would see a black dot over your yellow oval as you pass over the cookie.
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  [ 5 Posts ]
Jump to:   


Style:  
Search: