Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
[Tutorial] Collision Detection
Author Message
Dan

Posted: Tue Sep 24, 2002 3:06 pm   Post subject: [Tutorial] Collision Detection

Collision Detection

Ok I am show you how to use a common method of collision detection which is some times called rectangle collision detection. What it dose is uses if statements to make a theoretical rectangles arrowed the objects and then dose some tings if the rectangles hit.

Well here is an example of how to do this. This example uses two ovals which we wont to bounce off everything. (sorry for the bad spelling if I get time I will spell check it )

 code: var x, y : int %var for the 1st ovals quadents var x2, y2 : int %var for the 2nd ovals quadents var nx, ny, nx2, ny2 : int := 1 %vars for the chage in qroadents var s : int := 10 %var for the size of the ovals x := Rand.Int (10, 100) %set the x quadent for the 1st oval y := Rand.Int (10, 100) %set the y quadent for the 1st oval x2 := Rand.Int (10, 100) %set the x quadent for the 2nd oval y2 := Rand.Int (10, 100) %set the y quadent for the 2nd oval %main loop loop     Draw.Oval (x, y, s, s, red) %draws the 1st oval     Draw.Oval (x2, y2, s, s, green) %draws the 2nd one     %check to see if oval gose off the screen     %for oval 1         if x <= 0 then %check to see if it gose off the bottom         nx := 1 %chages the direction     elsif y <= 0 then %check to see if it gose off to the left         ny := 1 %chages the direction     end if     if x >= maxx then %check to see if it gose off the top         nx := - 1 %chages the direction     elsif y >= maxy then %check to see if it gose off to the right         ny := - 1 %chages the direction     end if     %check to see if oval gose off the screen     %for oval 2         if x2 <= 0 then %check to see if it gose off the bottom         nx2 := 1 %chages the direction     elsif y2 <= 0 then %check to see if it gose off to the left         ny2 := 1 %chages the direction     end if     if x2 >= maxx then %check to see if it gose off the top         nx2 := - 1 %chages the direction     elsif y2 >= maxy then %check to see if it gose off to the right         ny2 := - 1 %chages the direction     end if     %this if checks to see if the ovals have hit each other     if x + s > x2 - s and x - s < x2 + s and y + s > y2 - s and y - s < y2 +             s then                 put "the balls hit"         delay (500)         %chages the direction of all the ovals         nx := nx * - 1         ny := ny * - 1         nx2 := nx2 * - 1         ny2 := ny2 * - 1         end if     %this part adds the chage in qroadents to the     %quradents and makes the ovals move     x += nx     y += ny     x2 += nx2     y2 += ny2     delay (10)     cls end loop

Also i think tony has some stuff to add to this about other methods of doing this.

P.S. if there is anything you dont understand (or cant read becuse of my spelling) plz feal free to post and let us know.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor
Sponsor

Tony

Posted: Wed Sep 25, 2002 12:28 pm   Post subject: (No subject)

[tutorial] Oval Collision Detection

Well Dan explained how to use rectangle collision Detection... sord off...

Anyway, that is good enough for some cases, but in sertain games oval detection might prove better. Especially if you deal with circles

The idea behind it is to find the distance between the centers of the circles and see if its less then sum of radiuses. If so, they have collided.

To make a collision Detection function, you need to know pythagorian theorem (C^2 = A^2 + B^2)

Here's how you use it:

 code: distance = ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))**0.5 %you take the square of differences of X coordinates and add %a square of differences of Y coordinates then take a square %root of the sum %%%%%% %I did (x)*(x) instead of x**2 because exponents is a function %and it takes up more resourses then *, thus its faster %Also **0.5 is the same as sqrt() - learn your math guys if distance < radious1 + radious2 %might be a good idea to save radious1+radious2 as const/var then collision = true end if

I hope this little tutorial helped. If something is still unclear, or you want to know how to do other types of detections, please post.
Tony's programming blog. DWITE - a programming contest.
R.W.Z

Posted: Fri Oct 04, 2002 5:05 pm   Post subject: Try This

This would be a good tutorial but I didn't label much but this is almost the same thing as the other one but with this you can change the number of bouncing balls by changing BALLCOUNT but only the first two balls say if they collided with each other.

 code: const BALLCOUNT : int := 2 var ballX : array 1 .. BALLCOUNT of int var ballY : array 1 .. BALLCOUNT of int var directionX : array 1 .. BALLCOUNT of int var directionY : array 1 .. BALLCOUNT of int const RADIUS : int := 20 % Set random values for the x and y of each ball also the % x and y direction for each ball for i : 1 .. BALLCOUNT     randint (ballX (i), RADIUS * 2, maxx - RADIUS * 2)     randint (ballY (i), RADIUS * 2, maxy - RADIUS * 2)     loop         randint (directionX (i), -3, 3)         randint (directionY (i), -3, 3)         exit when directionX (i) not= 0 and directionY (i) not= 0     end loop end for loop     for i : 1 .. BALLCOUNT         drawfilloval (ballX (i), ballY (i), RADIUS, RADIUS, 0)         ballX (i) := ballX (i) + directionX (i)         ballY (i) := ballY (i) + directionY (i)         if ballX (i) < RADIUS or ballX (i) > maxx - RADIUS then             directionX (i) := -directionX (i)         end if         if ballY (i) < RADIUS or ballY (i) > maxy - RADIUS then             directionY (i) := -directionY (i)         end if         drawfilloval (ballX (i), ballY (i), RADIUS, RADIUS, 12)     end for     if ballX (1) + RADIUS > ballX (2) - RADIUS and ballX (1) - RADIUS < ballX (2) + RADIUS and ballY (1) + RADIUS > ballY (2) - RADIUS and ballY (1) - RADIUS < ballY (2) + RADIUS then         locatexy (maxx div 2, maxy div 2)         put "Balls Hit"         delay(100)         cls     end if     delay (10) end loop

R.W.Z
Martin was here. Use code tags
JayLo

Posted: Tue Nov 26, 2002 11:46 pm   Post subject: for the rectangle collision...

I'm trying out a pool program... thanks for the rectangle collision program... However, I would like to make the collision more realistic as in a "real" pool bounce... I tried out the oval collision, but it seems as if the program only tells when it hits! I would like to find out if the balls can realistically bounce off each other in a real way. Please post any sort of help.
Tony

Posted: Wed Nov 27, 2002 12:35 am   Post subject: (No subject)

ya, you can do that... you need to apply some physics and trig for it though... you should have no problem with grade11 knowledge of math and physics.

I will shortly post a new tutorial on this topic. Check out tutorial section for [trig]
Tony's programming blog. DWITE - a programming contest.
void

Posted: Mon Apr 07, 2003 8:00 pm   Post subject: collison detection

when i click on a certain spot i want it to check if there is something within a certain radius of the point, say i click on (x,y)...i want to check if there is something within a 40 pixel radius of the spot ...any ideas are welcome....can u try to explain it to me because i dont wanna just see code and copy it (im lazy so if its there, i'll copy it ), if you hafta tho ..i dont mind... thanks alot.....this site is really helpful...and its a good oppurtunity for me to test my own skills by debuggin and helping others and receive help from you crazzy programmmers.......if i had a job i might give you pplz money for such an amazing site...its one of the very few on the net that have a point to them...alongside google and shockwave.com GOOD JOB!!!! i know its commentss like this that make your day j/k
im out...pz
Blade

Posted: Mon Apr 07, 2003 8:05 pm   Post subject: (No subject)

use an if statement... lets say if you have a ball.... xy will be the mouse... so you make the forign object enter the radius of the mouse click.... then use an if statement to see if its there.. .such as
if forignobject < xy of ball + 40 then
the object has entered the radius

understand?
void

Posted: Mon Apr 07, 2003 8:18 pm   Post subject: (No subject)

thanks alot ....i think i get it...but the thing is...what if its at like a 45 degree angle from the center (using a horizontal plane as 0 degreees and 180)...then how do you check it? lets say its a line headed at a circle at a 45 degree angle from the center of the circle....how would you check that?...because the xy only check the horizontal and vertical values does it not? the diagonals are completely ignored
Sponsor
Sponsor

Blade

Posted: Mon Apr 07, 2003 8:38 pm   Post subject: (No subject)

what happens if you add 40 to x, and y, then find the distance between the both then get the midpoint from that.... would that work?
Catalyst

Posted: Tue Apr 08, 2003 6:08 am   Post subject: (No subject)

use this function to find out if its in the circle

 code: function distance (x1, y1, x2, y2 : real) : real     result ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 end distance
void

Posted: Tue Apr 08, 2003 6:45 am   Post subject: (No subject)

thanks man...that helped alot...im just wondering how come everyone uses **0.5 instead of sqrt...is it betta proggramming? or is it just kinda what ur used ta doing?
Catalyst

Posted: Tue Apr 08, 2003 7:25 am   Post subject: (No subject)

might me a little faster since its not a function like sqrt, but i mainly just use by habit and that i can do cube roots and such by doing **0.3333,etc.
void

Posted: Tue Apr 08, 2003 6:33 pm   Post subject: (No subject)

 code: var x, y, button : int function distance (x1, y1, x2, y2 : real) : real     result ( (x1 - x2) ** 2 - ( y1 - y1) ** 2) ** 0.5 end distance loop     Mouse.Where (x, y, button)     locate (13, 13)     put distance (6, 4, x, y)     delay (5) end loop

okay....heres what im attempting to do....i wanna see what the distance is....by radius from the mouse and the point that i randomly set in there.....but the thing is....i get a value of 0 (which should mean im directly over the point) far too many...how many different (6,4)'s can there be.....i've tried switching the (x,y) to x1 and y1 positions istead of their current x2,y2 possition...but the result is still the same.....
Catalyst

Posted: Tue Apr 08, 2003 9:14 pm   Post subject: (No subject)

u messed the code up..

Quote:

result ( (x1 - x2) ** 2 - ( y1 - y1) ** 2) ** 0.5

should be

Quote:

result ( (x1 - x2) ** 2 - ( y1 - y2 )** 2) ** 0.5
void

Posted: Wed Apr 09, 2003 6:56 am   Post subject: (No subject)

hehehehehehe ooopsssiiies......dont i feel like an idiot....
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 3  [ 34 Posts ]
Goto page 1, 2, 3  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: