Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-16-2010, 12:04 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 65
MrPenguin9 is on a distinguished road
Default I need help with circle to circle collision response

I need help with circle to circle collision response. I have two circles, both moving. I know how to find if they are colliding (something like this)
Code:
float dist = sqrt(((circle1.pos.x - circle2.pos.x) * (circle1.pos.x - circle2.pos.x)) + ((circle1.pos.y - circle2.pos.y) * (circle1.pos.y - circle2.pos.y)) );
if(dist < 64){
//do collision
}
But I have no idea how to do the response to the collision. All I know is the position of the circles, (circle1.pos.x, circle1.pos.y, circle2.pos.x, circle2.pos.y) velocity of the circles, (circle1.vel.x, circle1.vel.y, circle2.vel.x, circle2.vel.y) and the diameter of both of the circles is 64. (radius is 32)

Thanks!
MrPenguin9 is offline   Reply With Quote
Old 02-16-2010, 12:40 AM   #2 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

I googled "Elastic Collision Game Programming" and came up with several references on how to handle the collision, as long as you are assuming the circles do not deform during the collision.
JasonR is offline   Reply With Quote
Old 02-16-2010, 04:42 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 6
ClarityGames is on a distinguished road
Default

Hi Mr. Penguin. I've done something like this before and have a couple of pointers.

1. You can do away with the square root function.
Square root functions are notoriously slow and cpu intensive, and should be avoided. On the face of it, though, it's an essential last step in calculating the distance between two points. But if you take advantage of the fact that if

a = squrt(b)

then

a squared = b

you can transform your CD calculation to

float dist = ((circle1.pos.x - circle2.pos.x) * (circle1.pos.x - circle2.pos.x)) + ((circle1.pos.y - circle2.pos.y) * (circle1.pos.y - circle2.pos.y));
if(dist < 4096){
//do collision
}


4096 being 64 squared. Try it, it works!

2. The collision response for two circles colliding can be based on the vector between the two centers. Basically the vector created between circle1 and circle2 represents the line of kinetic force that circle1 applies to circle2, so the negative of it would represent the reactive 'push' of the collision.

e.g. if circle1 - circle2 = (4,4) then applying a force on circle1 of (-4,-4) would be a realistic response.

Of course the 'applying the force' bit is the tricky part of the deal and I'm a little hazy on that side of things, but I'm pretty sure that the whole process can be handled by vector arithmetic.

You could try something like this as a simple start:-

Circle1VecX = (circle2.pos.x - circle1.pos.x)/64

and then the same for Y to see the circles respond with the correct lines of force away from each other. This won't give you the correct direction or speed of response but it'll at least show the basis of a proper 'reaction', i.e. the two circles should always respond by heading away from each other in exactly opposite directions. After that it's just a matter of applying the correct magnitude to the reactive force and adding it to the original vector of the circle and volia! Realistic response!
ClarityGames is offline   Reply With Quote
Old 02-16-2010, 12:44 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 65
MrPenguin9 is on a distinguished road
Default

Quote:
Originally Posted by ClarityGames View Post
Hi Mr. Penguin. I've done something like this before and have a couple of pointers.

1. You can do away with the square root function.
Square root functions are notoriously slow and cpu intensive, and should be avoided. On the face of it, though, it's an essential last step in calculating the distance between two points. But if you take advantage of the fact that if

a = squrt(b)

then

a squared = b

you can transform your CD calculation to

float dist = ((circle1.pos.x - circle2.pos.x) * (circle1.pos.x - circle2.pos.x)) + ((circle1.pos.y - circle2.pos.y) * (circle1.pos.y - circle2.pos.y));
if(dist < 4096){
//do collision
}


4096 being 64 squared. Try it, it works!

2. The collision response for two circles colliding can be based on the vector between the two centers. Basically the vector created between circle1 and circle2 represents the line of kinetic force that circle1 applies to circle2, so the negative of it would represent the reactive 'push' of the collision.

e.g. if circle1 - circle2 = (4,4) then applying a force on circle1 of (-4,-4) would be a realistic response.

Of course the 'applying the force' bit is the tricky part of the deal and I'm a little hazy on that side of things, but I'm pretty sure that the whole process can be handled by vector arithmetic.

You could try something like this as a simple start:-

Circle1VecX = (circle2.pos.x - circle1.pos.x)/64

and then the same for Y to see the circles respond with the correct lines of force away from each other. This won't give you the correct direction or speed of response but it'll at least show the basis of a proper 'reaction', i.e. the two circles should always respond by heading away from each other in exactly opposite directions. After that it's just a matter of applying the correct magnitude to the reactive force and adding it to the original vector of the circle and volia! Realistic response!
Great idea! Thank you!

Quote:
Originally Posted by JasonR View Post
I googled "Elastic Collision Game Programming" and came up with several references on how to handle the collision, as long as you are assuming the circles do not deform during the collision.
Thank you so much! I was googleing "Circle to circle collision response" and I couldn't find anything, but "Elastic Collision Game Programming" work perfectly!

Thank you both very much!
MrPenguin9 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 399
7 members and 392 guests
13dario13, iOS.Lover, jeroenkeij, Leslie80, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:44 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0