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

Username:   Password: 
 RegisterRegister   
 Circular Collision Detection- HELP!
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
adamlover




PostPosted: Thu May 30, 2013 7:12 pm   Post subject: Circular Collision Detection- HELP!

Hi all, this is my first ever question, so bare with me.

For my ISU, I'm making a game where the user moves around the screen, collecting stars and avoiding enemies. These circular enemies will kill the circular user. My issue is with detecting collision.

The enemies are not random, but they do move across the screen using "for" statements; the user moves with the keyboard arrows. I'm using processes and the distance formula and radii, but for some reason, the distance between the two circles is not recognized, so the program won't "GAME OVER". Any help/advice/suggestions are beyond greatly appreciated. Thanks-


code:


var answer : string (1)
var down : string := chr (208)
var up : string := chr (200)
var left : string := chr (203)
var right : string := chr (205)

var userxlocation : int := maxx div 2
var userylocation : int := maxy div 2

var foo : int := 0


process user
    loop
        drawfilloval (userxlocation, userylocation, 20, 20, 48)     %draw user
        getch (answer)
        if answer = up then     %arrow key moves up
            userylocation := userylocation + 18
            if userylocation > maxy - 18 then
                userylocation := maxy - 18
            end if
        elsif answer = down then     %arrow key moves down
            userylocation := userylocation - 18
            if userylocation < 18 then
                userylocation := 18
            end if
        elsif answer = left then     %arrow key moves left
            userxlocation := userxlocation - 18
            if userxlocation < 18 then
                userxlocation := 18
            end if
        elsif answer = right then     %arrow key moves right
            userxlocation := userxlocation + 18
            if userxlocation > maxx - 18 then
                userxlocation := maxx - 18
            end if
        end if
        delay (10)
        cls
        drawfilloval (userxlocation, userylocation, 20, 20, 48)     %redraw
    end loop
end user


process enemy1
    for i : 1 .. maxx
        drawfilloval (100 + i, 100, 60, 60, blue)
        delay (1)
        drawfilloval (100 + i, 100, 60, 60, black)
    end for
    cls
    for decreasing i : maxx .. 1
        drawfilloval (100 + i, 100, 60, 60, blue)
        delay (1)
        drawfilloval (100 + i, 100, 60, 60, black)
        foo := i
    end for
end enemy1


setscreen ("graphics:max;max,nobuttonbar")
View.Set ("position:0;0")

colourback (black)
cls
View.Update


loop
    cls
    View.Update
    fork user
    delay (1020)
    fork enemy1
    delay (1060)
    fork user
    delay (1750)
    View.Update
     
    %40 is the radii of both circles added together
    if sqrt ((userxlocation - (100 + foo)) ** 2 + (userylocation - 100) ** 2) <= 40 then
        cls
        put "GAME OVER"
        exit
    end if
   
end loop
put "IT IS ENDED"

Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Thu May 30, 2013 8:24 pm   Post subject: Re: Circular Collision Detection- HELP!

First and foremost I strongly recommend that you do not use processes. I understand it's tempting, but it will likely cause you more grief than good.

Why are you calling fork over and over again? You don't need multiple user and enemy1 processes, one will suffice (note that I think you would be better off not using processes).

As for why your game is not exiting, you check for collision once every 1.83 seconds and when you do foo is probably 1.


Instead of using processes, try keeping track of where your objects are, where they are going, then run a loop to update where they are on the screen and check for collision every time.
evildaddy911




PostPosted: Fri May 31, 2013 6:24 pm   Post subject: RE:Circular Collision Detection- HELP!

the biggest 2 things wrong with it are:
1. you should have put your "fork" statements OUTSIDE the main loop if you are using processes (which should NOT be used in Turing) because then you start a new process every few seconds, which generally just blows up your program
2. when you exit the main loop because the player has collided with the ball of death, you don't end the processes. that is why processes are bad. they don't end until either they run out of code to execute or the program ends. as far as i know, there is only 1 way to end a process prematurely: by putting "exit when done" statement ONLY inside the biggest loop of the process.

What i suggest doing is incorporating the processes into 1 big main loop. have player movement, then ball-of-death movement. then have your "am i dead?" check, exiting if neccesary. No processes.

1 more thing: when you use setscreen and View.Set, you aren't switching the drawing mode to "offscreenonly" so View.Update is doing nothing. if you don't understand what this means, check out the Turing Walkthrough
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: