Computer Science Canada

Collision Detection and Bounces

Author:  JumpingLimaBean [ Thu Jun 09, 2011 12:12 pm ]
Post subject:  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

Author:  Raknarg [ Thu Jun 09, 2011 3:31 pm ]
Post subject:  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.

Author:  JumpingLimaBean [ Thu Jun 09, 2011 7:05 pm ]
Post subject:  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

Author:  Zren [ Thu Jun 09, 2011 7:28 pm ]
Post subject:  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

Author:  JumpingLimaBean [ Thu Jun 09, 2011 7:57 pm ]
Post subject:  RE:Collision Detection and Bounces

how would i get the number to stay in the cirlce tho


By the way thanks for the tip

Author:  2F_Zelda [ Thu Jun 09, 2011 8:06 pm ]
Post subject:  Re: Collision Detection and Bounces

You would have the location of the number dependent on the location of the shape.

Author:  JumpingLimaBean [ Thu Jun 09, 2011 9:47 pm ]
Post subject:  RE:Collision Detection and Bounces

how would i do that

Author:  Tony [ Thu Jun 09, 2011 10:16 pm ]
Post subject:  RE:Collision Detection and Bounces

You know where to draw the shape/circle; draw the number at the same location. Read the documentation on Font.Draw

Author:  JumpingLimaBean [ Thu Jun 09, 2011 11:02 pm ]
Post subject:  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

Author:  JumpingLimaBean [ Thu Jun 09, 2011 11:37 pm ]
Post subject:  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.


Turing:


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 <= circlerad1 + circlerad2 then
        put " Nice Kyle you mastered movable collision!"
        exit
        end if
       
        delay(20) 
end loop


Author:  JumpingLimaBean [ Thu Jun 09, 2011 11:41 pm ]
Post subject:  RE:Collision Detection and Bounces

I will try to do the numbers in the circles tomorrow or something.

Author:  Zren [ Thu Jun 09, 2011 11:57 pm ]
Post subject:  RE:Collision Detection and Bounces

Quote:
distance := sqrt (circlex2 - circlex1) ** 2 + (circley2 - circley1) ** 2

Pythagorean Therum...

c^2 = a^2 + b^2
c = sqrt(a^2 + b^2)
dist = sqrt((x1-x2)^2 + (y1-y2)^2)

You missed the brackets foo.

Author:  JumpingLimaBean [ Fri Jun 10, 2011 1:17 am ]
Post subject:  RE:Collision Detection and Bounces

Thanks i missed those. I tried adding bracket at end of 2 and it didnt work so i took it out. I was missing the bracket infront of sqrt xD


It now works.



Now how do i get a number 1 to stay in the circle and follow it O.o

I know i gotta place it in with the circle or something. Do i need to do some if statements

Author:  2F_Zelda [ Fri Jun 10, 2011 2:25 am ]
Post subject:  Re: Collision Detection and Bounces

No, you want to draw it at the same co-ordinates as the circle.

Author:  Raknarg [ Fri Jun 10, 2011 9:16 am ]
Post subject:  RE:Collision Detection and Bounces

So let me get this straight, are you trying to get something to rotate around a circle?

Author:  JumpingLimaBean [ Fri Jun 10, 2011 11:29 am ]
Post subject:  RE:Collision Detection and Bounces

I am trying to place number 1 into the first circle and follow it.


Then place the number 2 into the second circle and follow it

Author:  Zren [ Fri Jun 10, 2011 2:31 pm ]
Post subject:  RE:Collision Detection and Bounces

What would you do if you wanted to draw another circle on top of the original circle? You'd use the same coordinates. Since the coordinates are variables, you'd use the same variables.

Draw.FillOval(x,y,r,r,red)
Draw.Oval(x,y,r,r,black)

Instead of another circle, it's rendered text.

Centering the text on the circle is secondary though...

Author:  JumpingLimaBean [ Fri Jun 10, 2011 3:11 pm ]
Post subject:  RE:Collision Detection and Bounces

Thanks it worked. It was so easy, dont know why i didn't think of that myself.


: