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

Username:   Password: 
 RegisterRegister   
 Square and Circle collision detection not working proporly
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Offroadrider




PostPosted: Thu Mar 31, 2011 11:05 am   Post subject: Square and Circle collision detection not working proporly

Hey guys,

I am trying to make a tag program but the collision detection between the circle and the square is not working proporly.

Here is the code that is not working

Turing:


%Variables
var ballx, bally :int :=300
var boxx,boxy :int :=50
var chars :array char of boolean
var font1,font2,font3 :int
var colour1, colour2 :int :=0
var z :int

font1 := Font.New ("timesnewroman:50:bold")
font2 := Font.New ("timesnewroman:100:bold")
font3 := Font.New ("timesnewroman:15")
randint (z, 0, 1)

if z = 0 then
 colour1 := 40
 end if
if z = 1 then
 colour2 := 40
 end if
 
 
View.Set ("offscreenonly")
loop
Input.KeyDown (chars)
drawfillbox (maxx,maxy,0,0,black)
drawfilloval (ballx,bally,10,10, colour1)
drawfillbox (boxx,boxy,boxx +20,boxy +20, colour2)
View.Update

%%%%%%%%CONTROLS%%%%%%%%%%%
%%%%%%%%%%%%P1%%%%%%%%%%%%%
if chars (KEY_UP_ARROW) then
    bally +=2
    end if
if chars (KEY_DOWN_ARROW) then
    bally -=2
    end if
if chars (KEY_RIGHT_ARROW) then
    ballx +=2
    end if
if chars (KEY_LEFT_ARROW) then
    ballx -=2
    end if
%%%%%%%%%%%%P2%%%%%%%%%%%%%%
if chars ('w') then
    boxy +=2
    end if
if chars ('s') then
    boxy -=2
    end if
if chars ('a') then
    boxx -=2
    end if
if chars ('d') then
    boxx +=2
    end if
   
%%%%%%%%%%%boundries%%%%%%%%
if bally > 380 then
    bally := 380
    end if
if bally < 20 then
    bally := 20
    end if
if ballx > 620 then
    ballx := 620
    end if
if ballx < 20 then
    ballx := 20
    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
   
%%%%%%%%%taging%%%%%%%%%%%%
if colour1=40 and ballx -20 < boxx and bally -20 > boxy and ballx < boxx +20 and bally > boxy +20 then
    colour2 := 40
    colour1 := 0
    end if
   
if colour2=40 and boxx +20 > ballx and boxy < bally and boxy + 20 > bally and boxx < ballx then
    colour1 := 40
    colour2 := 0
    end if
     
if chars (' ') then
    exit
    end if

end loop



This is in turing 4.1.1

1 more thing,
this is not the entire code
it would be way too big for this space.
this is just the pieces of code that have to do with the collision detection

Thanks for your help
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Mar 31, 2011 11:16 am   Post subject: RE:Square and Circle collision detection not working proporly

Quote:
bally -20 > boxy

Quote:
bally > boxy +20


What's different about these two statements?
Offroadrider




PostPosted: Thu Mar 31, 2011 11:29 am   Post subject: Re: Square and Circle collision detection not working proporly

Nothing, it's just that when i wrote the ballx-20, it finnaly worked on the some what on y-axis if I put boxx +20 > ballx like on the other line, it didn't work
Offroadrider




PostPosted: Thu Mar 31, 2011 1:02 pm   Post subject: Re: Square and Circle collision detection not working proporly

feel free to copy the code into a turing file to help find out what's not working
Zren




PostPosted: Thu Mar 31, 2011 2:07 pm   Post subject: RE:Square and Circle collision detection not working proporly

Box:
The bottom left is (boxx, boxy).
The top right is (boxx+20, boxy+20)

Circle:
The middle is (ballx, bally)

Circle and rectangular collision is done with if a point is within the range of the circle or rectangle. You seem to be trying to compare two ranges. And also treating the ball's center point like one of the rectangles control points (it's in the middle not a corner).

Make sure your code follows this pattern, where p is the middle of your ball.

Rectangle:
box.x <= p.x <= box.x + box.width
box.y <= p.y <= box.y + box.height

The way collision detection works, is that you can check circle against circle, or rectangle against rectangle, or if a point is within either. You can't easily check collision between a circle and a rectangle. You can however treat a circle as a rectangle since the circle can fit inside a rectangle (easier and more accurate than putting the rectangle inside the circle).

This post on collision detection should help:
http://compsci.ca/v3/viewtopic.php?t=13661
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: