
-----------------------------------
JumpingLimaBean
Thu Jun 09, 2011 12:12 pm

Collision Detection and Bounces
-----------------------------------
Im looking to make a game of tag.


There is a circle and a square.

You start at opposite corners and you cant go outside of the window.
Whoever is "red" color has to chase after the other "your it" and when you
tag them you get 1 point. When you tag them, they then become "red", "it"
and they chase after you. First to 10 points wins







Turing
----------
%%%%%%%%%%%boundries%%%%%%%%
if bally > 380 then
    bally := 380
    boxY := 370
    end if
if bally < 20 then
    bally := 20
    boxY :=10
    end if
if ballx > 620 then
    ballx := 620
    boxX := 610
    end if
if ballx < 20 then
    ballx := 20
    boxX := 10
    end if
    
if boxy > 360 then
    boxy := 360
    end if
if boxy < 0 then
    boxy := 0
    end if
if boxx > 600 then
    boxx := 600
    end if
if boxx < 0 then
    boxx := 0
    end if



That is used in a program to create boundaries between a circle and a square. When they touch eachother a point is then sent.  
How is this used? I dont understand what the 360 and that means. Help please xD

-----------------------------------
Raknarg
Thu Jun 09, 2011 3:31 pm

RE:Collision Detection and Bounces
-----------------------------------
This is just used to make sure that the shape can't escape the boundaries. If it's past the boundaries, it puts it back on the edge.

-----------------------------------
JumpingLimaBean
Thu Jun 09, 2011 7:05 pm

RE:Collision Detection and Bounces
-----------------------------------
I dont get how this detects if the players touch
eachother and when they do, they reset to again starter positions can someone explain in simple form?

var ballx, bally :int :=300
var boxX,boxY :int :=290
var boxx,boxy :int :=50





Input.KeyDown (chars)
drawfillbox (maxx,maxy,0,0,black)
drawfillbox (boxX,boxY,boxX +20,boxY +20,black)
delay (3)
drawfilloval (ballx,bally,10,10,colour1)
delay (3)
drawfillbox (boxx,boxy,boxx +20,boxy +20,colour2)
delay (3)
t-=1
exit when t =0
View.Update





if colour1=40 and boxx < boxX +20 and boxx +20 > boxX and boxy < boxY +20 and boxy +20 > boxY then %?
    colour1 :=0
    colour2 :=40
    boxx :=100
    boxy :=100
    score1 +=1
end if   
   
if colour2=40 and boxX < boxx +20 and boxX +20 > boxx and boxY < boxy +20 and boxY +20 > boxy then %?
    colour2 :=0
    colour1 :=40
    boxX :=290
    boxY :=290
    ballx :=300
    bally :=300
    score2 +=1
end if

-----------------------------------
Zren
Thu Jun 09, 2011 7:28 pm

RE:Collision Detection and Bounces
-----------------------------------
Combining circle and rectangular collision detection is annoying (possible but annoying). Why not just make both of them circles but different colours? Maybe even draw a number inside them with Font.Draw?

Then read up on Collision detection here: http://compsci.ca/v3/viewtopic.php?t=13661

-----------------------------------
JumpingLimaBean
Thu Jun 09, 2011 7:57 pm

RE:Collision Detection and Bounces
-----------------------------------
how would i get the number to stay in the cirlce tho


By the way thanks for the tip

-----------------------------------
2F_Zelda
Thu Jun 09, 2011 8:06 pm

Re: Collision Detection and Bounces
-----------------------------------
You would have the location of the number dependent on the location of the shape.

-----------------------------------
JumpingLimaBean
Thu Jun 09, 2011 9:47 pm

RE:Collision Detection and Bounces
-----------------------------------
how would i do that

-----------------------------------
Tony
Thu Jun 09, 2011 10:16 pm

RE:Collision Detection and Bounces
-----------------------------------
You know where to draw the shape/circle; draw the number at the same location. Read the documentation on [tdoc]Font.Draw[/tdoc]

-----------------------------------
JumpingLimaBean
Thu Jun 09, 2011 11:02 pm

RE:Collision Detection and Bounces
-----------------------------------
I successfully drew the "1" in the same location of the circle. But how do i get it to follow with it

-----------------------------------
JumpingLimaBean
Thu Jun 09, 2011 11:37 pm

RE:Collision Detection and Bounces
-----------------------------------
Okay i ended up doing this prgram.

I got collision to work, but the problem is
that im going to use it to make TAG. If i
move past the circle, it says negative value
passed to Sqrt. I need it not to do this.
And sometimes it goes into the circle and not
stopping so its detecting it late or something.
Please test out and help me make it better 
so i can turn it into TAG.




var x := 320
var y := 200
var circlex1, circley1, circlerad1 : int
var circlex2, circley2, circlerad2 : int
var distance : real
var number : int


circlex1 := 100
circley1 := 100
circlerad1 := 10
circlex2 := 250
circley2 := 220
circlerad2 := 30
const INCREMENT : int := 5

%this variable is needed to use Input.Keydown
var chars : array char of boolean 

loop
        cls
        Draw.FillOval (circlex1, circley1, circlerad1, circlerad1, black)
        Draw.FillOval (circlex2,circley2,circlerad2,circlerad2, 9)
        Input.KeyDown (chars)  %tell computer to listen for keypresses
        
        %check arrow keys
        %Look up Keyboard in the Turing reference to find all special 
        %key codes
        if chars (KEY_LEFT_ARROW) and chars(KEY_UP_ARROW) then
                circlex1 := circlex1 - INCREMENT
                circley1 := circley1 + INCREMENT
        elsif chars (KEY_RIGHT_ARROW) and chars(KEY_UP_ARROW) then
                circlex1 := circlex1 + INCREMENT
                circley1 := circley1 + INCREMENT
        elsif chars (KEY_LEFT_ARROW) and chars(KEY_DOWN_ARROW) then
                circley1 := circley1 - INCREMENT
                circlex1 := circlex1 - INCREMENT
        elsif chars (KEY_RIGHT_ARROW) and chars(KEY_DOWN_ARROW) then
                circley1 := circley1 - INCREMENT
                circlex1 := circlex1 + INCREMENT
        elsif chars (KEY_LEFT_ARROW) then
                circlex1 := circlex1 - INCREMENT
        elsif chars (KEY_RIGHT_ARROW) then
                circlex1 := circlex1 + INCREMENT
        elsif chars (KEY_UP_ARROW) then
                circley1 := circley1 + INCREMENT
        elsif chars (KEY_DOWN_ARROW) then
                circley1 := circley1 - INCREMENT
        end if
        
        
        
        %check asdf keys
        if chars ('a') then
                circlex2 := circlex2 - INCREMENT
        elsif chars ('d') then
                circlex2 := circlex2 + INCREMENT
        elsif chars ('w') then
                circley2 := circley2 + INCREMENT
        elsif chars ('s') then
                circley2 := circley2 - INCREMENT
        end if
        distance := sqrt (circlex2 - circlex1) ** 2 + (circley2 - circley1) ** 2
        
        if distance 