Ball Bouncing in a circle
I am working on a puzzle game for the iphone, I am stuck at figuring out how to bounce a ball within a circle.
any ideas?
any ideas?
if the small ball has position P and radius r, the big circle that the small ball is in has position P' and radius r' (r' > r) then as long as the center P of the small circle is within r'  r of P' the small ball is inside.. otherwise it's outside.
Here's pseudocode for that:
If you find from one frame to the next the ball went from inside to outside, You would want to apply a force to the ball so that it bounces back in.. That force would be the 'opposite and equal' one (thanks Newton!). To calculate that force (it's a direction and magniture aka a vector) you can reflect the current velocity of the ball around the line from the point of contact to the center of the bigger circle.
Here's pseudocode for that:
Code:
(define (withindistance d p1 p2)
(< (distance p1 p2) d))
(define (distance p1 p2)
(sqrt (+ (square ( (pointx p1) (pointx p2)))
(square ( (pointy p1) (pointy p2)))))) ;; easily extended to 3D or more
If you find from one frame to the next the ball went from inside to outside, You would want to apply a force to the ball so that it bounces back in.. That force would be the 'opposite and equal' one (thanks Newton!). To calculate that force (it's a direction and magniture aka a vector) you can reflect the current velocity of the ball around the line from the point of contact to the center of the bigger circle.
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
The force (impulse) applied to the ball to make it bounce back should be in the direction that goes from the ball's position to the center of the sphere, and it's strength proportional to the dot product between the velocity vector and the normalized vector from the ball to the center (for elastic collisions twice this amount)
Â©hâ‚¬ck Ã¸ut Âµy stuÆ’Æ’ Ã¥t ragdollsoft.com
New game in development Rubber Ninjas  Mac Games Downloads
Guys, thanks for the replies. Only thing is I did physics a long time back & I am trying to catch up. It would be awesome if we can break down the steps a little more.
Onto the problem:
The bigger circle is stationary. The smaller circle(ball) is the one moving inside the Bigger circle
here's a picture of what I am thinking.
http://www.flickr.com/photos/60395315@N00/3621325296/
(p1, p2) is the point I want to find.
Do I need to find point (c,d)?
I am thinking I need to solve for the equation between the line (a,b) & (m,n) and the equation of the circle to find (c,d), so I can get the equation of the tangent.
Is that the only way to do that, or is there an easier alternative way to find (c,d)
> To calculate that force (it's a direction and magniture aka a vector) you can
> reflect the current velocity of the ball around the line from the point of
> contact to the center of the bigger circle.
Can you help me with the equations, so I can reflect the velocity. This is where I was stuck.
>The force (impulse) applied to the ball to make it bounce back should be in
>the direction that goes from the ball's position to the center of the sphere
>and it's strength proportional to the dot product between the velocity vector
> and the normalized vector from the ball to the center (for elastic collisions
> twice this amount)
Onto the problem:
The bigger circle is stationary. The smaller circle(ball) is the one moving inside the Bigger circle
here's a picture of what I am thinking.
http://www.flickr.com/photos/60395315@N00/3621325296/
(p1, p2) is the point I want to find.
Do I need to find point (c,d)?
I am thinking I need to solve for the equation between the line (a,b) & (m,n) and the equation of the circle to find (c,d), so I can get the equation of the tangent.
Is that the only way to do that, or is there an easier alternative way to find (c,d)
> To calculate that force (it's a direction and magniture aka a vector) you can
> reflect the current velocity of the ball around the line from the point of
> contact to the center of the bigger circle.
Can you help me with the equations, so I can reflect the velocity. This is where I was stuck.
>The force (impulse) applied to the ball to make it bounce back should be in
>the direction that goes from the ball's position to the center of the sphere
>and it's strength proportional to the dot product between the velocity vector
> and the normalized vector from the ball to the center (for elastic collisions
> twice this amount)
Najdorf Wrote:The force (impulse) applied to the ball to make it bounce back should be in the direction that goes from the ball's position to the center of the sphere, and it's strength proportional to the dot product between the velocity vector and the normalized vector from the ball to the center (for elastic collisions twice this amount)
Can you clarify where the position of the ball is, when I take the normalized vector from the ball to the center.
Is the ball at the boundary where its gonna bounce, or is it at a position outside the boundary?
strikerjax Wrote:Can you clarify where the position of the ball is, when I take the normalized vector from the ball to the center.
Is the ball at the boundary where its gonna bounce, or is it at a position outside the boundary?
It's at the point of bounce.
Here's one way to compute:
1: Calculate where the ball is going to hit the boundary next if it continues along its current vector (i.e. direction of movement.)
2: Calculate distance from current location to bounce location.
3: If distancetohit is greater than the distance the ball will travel this frame, then the ball isn't going to hit yet. Move the ball along it's vector the right distance. You are done.
4: Otherwise, move the ball to the collision point.
5. Reflect the original vector of travel around the axis formed by the line between the large circle's center and the point of collision.
6. Calculate how much further the ball will move this frame. Loop over the entire algorithm again until the ball has no more distance to travel.
Note: if you are already computing your motions in terms of forces and masses then you will want to handle this as suggested with force impulses. Didn't sound like you were, so for step 5 I suggested just calculating the reflected vector yourself. You don't even need vector math if you are uncomfortable with that; just use high school trigonometry.
Measure twice, cut once, curse three or four times.
Thanks for the detailed steps Matt.
Going by your steps, I understand how to solve for the direction of the resulting vector.
How do I find the magnitude of the vector?
There is a normal vector (N) that is pointing towards the center of the bigger circle from the point of collision. What is the magnitude of this vector?
Is magnitude of N equal to V cos (a), where a is the angle between the Velocity V & the Vector N?
am I thinking this right?
I am sorry about these simple physics questions. Its been a long while since had gone through these calculations, and I am trying to catch up. Most of the online tutorials use the weight of the object to calculate the normal force. Here I am not using the weight, so this got me a little confused, along with the vector calculations. I did find some flash code, but I have to learn flash to find the logic behind it, i think its easier to learn the physics part.
Going by your steps, I understand how to solve for the direction of the resulting vector.
How do I find the magnitude of the vector?
There is a normal vector (N) that is pointing towards the center of the bigger circle from the point of collision. What is the magnitude of this vector?
Is magnitude of N equal to V cos (a), where a is the angle between the Velocity V & the Vector N?
am I thinking this right?
I am sorry about these simple physics questions. Its been a long while since had gone through these calculations, and I am trying to catch up. Most of the online tutorials use the weight of the object to calculate the normal force. Here I am not using the weight, so this got me a little confused, along with the vector calculations. I did find some flash code, but I have to learn flash to find the logic behind it, i think its easier to learn the physics part.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Ball and chain  markhula  9  12,526 
Apr 18, 2011 10:39 PM Last Post: haqu 

When one circle is touching  Miglu  5  3,369 
Aug 29, 2010 08:31 AM Last Post: AnotherJake 

Bouncing around the screen  Taxxodium  5  4,017 
Jun 25, 2009 02:10 PM Last Post: johncmurphy 

2D reflecting a point off a circle  Joseph Duchesne  1  3,218 
Oct 23, 2004 08:05 PM Last Post: MattDiamond 