Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / How do I do 2D circle collision?

Author
Message
Null Freq
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location:
Posted: 6th Jan 2004 09:55 Edited at: 6th Jan 2004 09:58
Hello,

I have been trying to figure this out for the past hour.. I want to make the "player" circle stop at the "radius" of the other circle on the screen but still be able to move around the other circle.. I know this is probly very easy to do but I've only been using DBC for about 4 months now and Its one big learning experince!

Thanks to all who help!


~Falling Down~
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Jan 2004 10:37
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 6th Jan 2004 10:45 Edited at: 6th Jan 2004 10:46
Use the distance formula to check their radii.

The dist. formula is:

dist = Sqrt((y2 - y1)^2 + (x2 - x1)^2)

where x1,y1 is the center of one of the circles, and x2,y2 is the center of the other circle.

Then check the distance.
If it is greater than r1 + r2 ( r1 and r1 being the radii of the circles ) Then no collision has occured.
If it is less then or equal to r1 + r2 then collision is occuring and you need to adjust it appropriatly.

a function might do:


Then, to use it, in the loop, put:



But, replace the x1,y1,r1,x2,y2,r2 with the appropriate values.

Hope I Helped...

[EDIT] Whoops, Phaelax beet me to it [/EDIT]


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
Null Freq
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location:
Posted: 6th Jan 2004 11:42 Edited at: 6th Jan 2004 11:43
Thank you for your help but I am not very sure on how to use functions yet.

Is there a simpler way to do this? I didnt make my code above so it could be easily readable or useable thats about as advanced as my current DBC knowledge is.

Thank you again

~Falling Down~
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 6th Jan 2004 12:00
Yeah, there's an easier way...

Just use a gosub.:



And just before you call the Gosub, set the values, ie, x1 = whatever etc. etc.

Then, after the gosub check to see if inzone = 1, ie, If inzone = 1 Then whatever.

Hope I Helped...


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy

Login to post a reply

Server time is: 2025-05-22 09:08:40
Your offset time is: 2025-05-22 09:08:40