Quote:
Originally Posted by JJJS
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.