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 10-06-2009, 07:37 PM   #1 (permalink)
Indie Dev
 
hm50's Avatar
 
Join Date: May 2009
Location: South Bend, Indiana
Posts: 162
hm50 is on a distinguished road
Default Redirection/Ricochet

I am trying to figure out what formula I would use to determine a redirection or ricochet of one object impacting another object. I know that the ricochet would be the same angle after the collision that it was prior to the collision. My objects in this example are solid, not water or energy absorbent. I am just unsure of how to calculate that angle.

Can anyone shed some light on this for me? Thanks!

EDIT: I should have mentioned that I am using a typical velocity.x,velocity.y movement. Basically I think I need to know the line between the two objects and then find the line perpendicular to that line so that I can find the angle of impact based on the stationary object's x,y. The angle of departure would be the same angle in an opposite velocity of either velocity.x or velocity.y.

Am I at all close? What math functions do I use to determine these angles?

In research I have found people speaking of vectors, but I have no idea what that is or how to calculate it.
__________________
iPhone 3G

Support Indie Devs!! (that goes for newbs too!)

Apps:
Spell Blocks
See Read Say
iStatus
myVIP
myMVP

Last edited by hm50; 10-06-2009 at 09:14 PM.
hm50 is offline   Reply With Quote
Old 10-06-2009, 10:10 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Get out the graph paper - I can get you started with the theory, although this isn't the fastest way to do it. It's faster to use matrices, but it's better to understand what's going on first I think.

You already have a vector - your two numbers (x and y) give you a 2d vector. The vector gives us speed and direction - we know how fast we're moving and what direction.

Bouncing off vertical or horizontal surfaces is easy - if you're bouncing off the ceiling or floor, just reverse the y coordinate of your vector. Your speed was (5,5), now you bounce off the floor and your speed is (5,-5). If you're bouncing off the sides, reverse the x so (5,5) becomes (-5,5).

Angled surfaces is harder, but start with the simple case of bouncing off the floor again. Draw this on that graph paper. The ball comes down at an angle, which you can calculate by taking arctan(velocity.x / velocity.y) , and exits at angle (0 - angle) . Then we need to turn the angle back into a vector using sin and cosine.

So we have this now:
Code:
float angle = atan(velocity.y / velocity.x);
float deflectAngle = - angle;
velocity.x = cos(deflectAngle);
velocity.y = sin(deflectAngle);
Not bad, but there's one bug. We have the new angle, but we lost the speed (the magnitude of the vector). You can prove this by plugging (5,5) and (1,1) into the code above - 5/5 and 1/1 are both "1", so they give the same angle, and the same deflection angle.

We can fix that by finding the speed before we start (the magnitude of the vector; raw speed without direction, AKA the "length" of the vector) and multiplying our new vector by that magnitude. We can get the magnitude of the vector with the Pythagorean theorem.

Code:
float speed = sqrt( velocity.x *velocity.x  + velocity.y * velocity.y );

float angle = atan(velocity.y / velocity.x);
float deflectAngle = - angle;

velocity.x = cos(deflectAngle) * speed;
velocity.y = sin(deflectAngle) * speed;
Superb. If that works, you're ready to bounce stuff off of angled surfaces - just subtract the angle of the vector from (2*surfaceAngle).

Code:
float speed = sqrt( (velocity.x *velocity.x)  + (velocity.y * velocity.y) );

 float angle = arctan(velocity.y / velocity.x);
 float deflectAngle = (2*surfaceAngle)- angle;

 velocity.x = cos(deflectAngle) * speed;
 velocity.y = sin(deflectAngle) * speed;
I have not compiled any of this, so I probably misplaced a minus sign somewhere. Run some known numbers through it and check it with your graph paper.

EDIT: Fixed deflectangle. I think this is correct now, at least for my test cases.
__________________

Free Games!

Last edited by smasher; 10-06-2009 at 11:26 PM. Reason: arctan to atan, get rid of theta, switch sin/cos
smasher is offline   Reply With Quote
Old 10-06-2009, 10:43 PM   #3 (permalink)
Indie Dev
 
hm50's Avatar
 
Join Date: May 2009
Location: South Bend, Indiana
Posts: 162
hm50 is on a distinguished road
Default

That's Great! I am getting the graph paper now.

In your post you describe the "angleOfSurface". The issue I have there is that in my (immediate) circumstance, the stationary object is circular. Do I then define the angle of the object as perpendicular to the slope of the moving object? Is that again done with "atan"?

Thanks in advance!!
__________________
iPhone 3G

Support Indie Devs!! (that goes for newbs too!)

Apps:
Spell Blocks
See Read Say
iStatus
myVIP
myMVP
hm50 is offline   Reply With Quote
Old 10-06-2009, 10:59 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

If the stationary object is round, the math actually gets (amazingly) much easier. Just swap the signs on both coordinates. If you were travelling at (5,5) your new velocity is (-5,-5). Perfect bounce back in the direction you came. If your speed was (0, -5) it's now (0,5). Draw that on graph paper and you'll see what I mean. it's just x=-x, y=-y.

EDIT: Hold on, I probably simplified too much. I assumed your bullet or ball was heading straight on. If your bullet or ball makes a glancing blow, I'm wrong. You need to figure out the tanget of the sphere at the point of impact, and reflect off of the tangent as though it were a flat surface. Use that tangent as surfaceAngle.
__________________

Free Games!

Last edited by smasher; 10-06-2009 at 11:27 PM.
smasher is offline   Reply With Quote
Old 10-06-2009, 11:18 PM   #5 (permalink)
Indie Dev
 
hm50's Avatar
 
Join Date: May 2009
Location: South Bend, Indiana
Posts: 162
hm50 is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
EDIT: Hold on, I probably simplified too much. I assumed your bullet or ball was heading straight on. If your bullet or ball makes a glancing blow, I'm wrong. You need to figure out the tanget of the sphere at the point of impact, and reflect off of the tangent as though it were a flat surface. Use that tangent as angleOfSurface.
Okay, you edited before I could explain that. the radius of my stationary object is also at play. I am adding the radius of the stationary object and the radius of my moving object in the collision detection. Do I need to take that into consideration when figuring the bounce angle also?
__________________
iPhone 3G

Support Indie Devs!! (that goes for newbs too!)

Apps:
Spell Blocks
See Read Say
iStatus
myVIP
myMVP
hm50 is offline   Reply With Quote
Old 10-06-2009, 11:36 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by hm50 View Post
Okay, you edited before I could explain that. the radius of my stationary object is also at play. I am adding the radius of the stationary object and the radius of my moving object in the collision detection. Do I need to take that into consideration when figuring the bounce angle also?
Hmmn. I haven't done that before. But if you know the two objects have collided, you could draw a line between the center of the two objects, get the angle of that line with arctan(dy/dx), add 90 degrees (I mean M_PI/2 of course) to get the angle of a perpendicular line, and bounce the moving object off of that.

I just tried it on paper - draw a pea hitting a glancing blow at the top of the big melon, draw a line from the pea to the melon's center, cross it with a perpendicular, and reflect off of it. Looks good to me.
__________________

Free Games!

Last edited by smasher; 10-06-2009 at 11:38 PM.
smasher is offline   Reply With Quote
Old 10-06-2009, 11:48 PM   #7 (permalink)
Indie Dev
 
hm50's Avatar
 
Join Date: May 2009
Location: South Bend, Indiana
Posts: 162
hm50 is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Hmmn. I haven't done that before. But if you know the two objects have collided, you could draw a line between the center of the two objects, get the angle of that line with arctan(dy/dx), add 90 degrees (I mean M_PI/2 of course) to get the angle of a perpendicular line, and bounce the moving object off of that.

I just tried it on paper - draw a pea hitting a glancing blow at the top of the big melon, draw a line from the pea to the melon's center, cross it with a perpendicular, and reflect off of it. Looks good to me.
Awesome, that's what I was wondering, so if I understand correctly. I'll use the method you described above using the "arctan(dy/dx) + M_PI/2" for the "deflectAngle". Right?

I should have described my objects more throughly in my OP, however I am quite sure I will use the deflection on a solid angled wall eventually also. I am also sure this thread will benefit many others!!! Thanks Again Smasher!
__________________
iPhone 3G

Support Indie Devs!! (that goes for newbs too!)

Apps:
Spell Blocks
See Read Say
iStatus
myVIP
myMVP
hm50 is offline   Reply With Quote
Old 10-06-2009, 11:54 PM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by hm50 View Post
Awesome, that's what I was wondering, so if I understand correctly. I'll use the method you described above using the "arctan(dy/dx) + M_PI/2" for the "deflectAngle". Right?

I should have described my objects more throughly in my OP, however I am quite sure I will use the deflection on a solid angled wall eventually also. I am also sure this thread will benefit many others!!! Thanks Again Smasher!
You'll use "arctan(dy/dx) + M_PI/2" for the surfaceAngle - that's the angle of the fake line we're bouncing off of. The deflectAngle is still depends on the direction of the ball and of the surface, so it's "(2*surfaceAngle)- angle" , which I think is finally correct.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 10-08-2009, 01:06 PM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 129
gonk is on a distinguished road
Default

I may not understand your question, but if I do, it sounds like you are looking for a bounce vector, such as is common in ray tracing. To find this, you need to know the velocity vector ("V") of your object, and the (normalized) normal vector ("N") of the point with which it is colliding. Then your bounce vector is:

B = V - 2*N*dot_product(V,N)

Do a google search on "ray tracing bounce vector" to find more info.

Sorry if that isn't what you were asking.
gonk is offline   Reply With Quote
Old 02-27-2010, 03:17 PM   #10 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 1
60Hz is on a distinguished road
Default

For 2d I use this simple algy to figure out the reflection angle.

Angle of Reflection = (2 x Wall's normal angle) - (Angle of Incidence)

Hope that helps.
60Hz 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: 397
7 members and 390 guests
13dario13, ChrisYates, fredidf, iOS.Lover, 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:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0