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 08-07-2011, 11:30 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: IL
Posts: 9
JJJS is on a distinguished road
Default Good circular collision detection algorithm?

Hello, I am an extremely new programmer who is looking to make a simple game where there is a DrawView with 2 circles bouncing around and if you poke them they change colors. Right now I have the circles bouncing off the sides, which was the easy part, but I am looking to make it realistic by making the circles bounce off each other. I tried using CGRectIntersectsRect but that gave a pretty unrealistic effect. I also tried using "if (ball.origin < ball.size.width + ball2.size.width)" but that was also a bad idea. I would like the angles that they bounce off at to be realistic as well, and I know this may take some trig. Anyone have any ideas?
JJJS is offline   Reply With Quote
Old 08-09-2011, 03:38 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 22
Tinyted is on a distinguished road
Default

Quote:
Originally Posted by JJJS View Post
Hello, I am an extremely new programmer who is looking to make a simple game where there is a DrawView with 2 circles bouncing around and if you poke them they change colors. Right now I have the circles bouncing off the sides, which was the easy part, but I am looking to make it realistic by making the circles bounce off each other. I tried using CGRectIntersectsRect but that gave a pretty unrealistic effect. I also tried using "if (ball.origin < ball.size.width + ball2.size.width)" but that was also a bad idea. I would like the angles that they bounce off at to be realistic as well, and I know this may take some trig. Anyone have any ideas?
I'm going to give a shot and trying to help.
Your if statement seems to make no sense. Especially with ball.origin is a struct.

First, detect collision for circular objects. What's special about circles? The radius. It's the same distance every direction. (Pretty sure you know, this it's basic geometry)
Main crucial formula would be to use x^2+y^2=distance^2. Which is essentially Pythagorean theorem. (Wikipedia it if you forgot/dunno what it is)

Now this would allow you to create accurate circle collision detection as long as both are perfect circles.

Next, the angle which they bounce of. That would involve some physics, but the main idea is trigonometry. You would have to use trigs, such as law of sine or cosine, to calculate the angle. Using concepts of physics calculate the angle it bounces off and what not.
I won't go in depth with trig, cause that's rather complicated, unless you really have no clue. I'm assuming you know trigonometry which is pretty fundamental stuff.

I'll discuss more in depth about the trig depending on what you know/don't know.
Tinyted is offline   Reply With Quote
Old 08-17-2011, 01:04 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: IL
Posts: 9
JJJS is on a distinguished road
Default

Quote:
Originally Posted by Tinyted View Post
Next, the angle which they bounce of. That would involve some physics, but the main idea is trigonometry. You would have to use trigs, such as law of sine or cosine, to calculate the angle. Using concepts of physics calculate the angle it bounces off and what not.
I won't go in depth with trig, cause that's rather complicated, unless you really have no clue. I'm assuming you know trigonometry which is pretty fundamental stuff.

I'll discuss more in depth about the trig depending on what you know/don't know.
It woud be great if you could give me some of the trig needed for circular collision reactions. I understand trig fairly well, i just wouldn't have an idea of where to start in this problem and apply it.
JJJS is offline   Reply With Quote
Old 08-17-2011, 02:11 PM   #4 (permalink)
Registered Member
 
headkaze's Avatar
 
Join Date: Feb 2010
Posts: 359
headkaze is on a distinguished road
Default

Here's some basic code

Code:
void checkCollision(Ball* pSprite1, Ball* pSprite2)
{
	if (((pSprite1->X >= pSprite2->X) && (pSprite1->X <= pSprite2->X+BALLSIZE)) && ((pSprite1->Y >= pSprite2->Y) && (pSprite1->Y <= pSprite2->Y+BALLSIZE)))
	{
		int xDist = pSprite2->X - pSprite1->X;
		int yDist = pSprite2->Y - pSprite1->Y;
		int dist = sqrt(xDist * xDist + yDist * yDist);
		float angle = atan2(yDist, xDist);
	 
		if (dist < BALLSIZE) // Collision detected
		{
			// Use the following two lines if you want the balls to repell each other instead of ball1 pushing ball2 away
			//pSprite1->X = pSprite2->X - (BALLSIZE * cos(angle));
			//pSprite1->Y = pSprite2->Y - (BALLSIZE * sin(angle));
		
			pSprite2->X = pSprite1->X + (BALLSIZE * cos(angle));
			pSprite2->Y = pSprite1->Y + (BALLSIZE * sin(angle));
		}
	}
}
__________________
Headsoft | Jungool
headkaze is offline   Reply With Quote
Reply

Bookmarks

Tags
algorithm, collision, detection, game

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: 412
10 members and 402 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,923
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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