Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: 2D Game Physics, how do I calculate reflections?  (Read 4819 times)

noodle0117

  • Bay Watcher
  • I wonder what would happen if I pull it.
    • View Profile
2D Game Physics, how do I calculate reflections?
« on: February 25, 2014, 09:09:55 pm »

Good day bay12 forums,

I'm trying to figure out a seemingly simple problem here, but somehow I'm getting mentally stuck.


Given an x-velocity and y-velocity of an object, and the angular orientation of the object that we are reflecting stuff off of, what will be the resulting x-velocity and y-velocity of the object? (assume zero loss in speed).
Logged

Shadowlord

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #1 on: February 25, 2014, 10:03:25 pm »

A quick google search for 'pool table physics' and skimming the first two results reveals that it should bounce off at the same angle with which it approaches the wall (or whatever that line in your picture is).

See 'angular deflection' in http://library.thinkquest.org/TQ0013321/thescience.html
The other page was http://www.real-world-physics-problems.com/physics-of-billiards.html (but it was about balls colliding with each other or the cue, rather than the walls)

Good luck!
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive

gomez

  • Bay Watcher
  • [Hysterical and Useless]
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #2 on: February 25, 2014, 10:07:44 pm »

here is a clue, what is the difference between the angle of the object and theta?

Also I may be wrong here but if you google "vector" you might get another way of working this out which is easier to program, assuming this is for a program and not homework  ;D
Logged
Quote
"They accused us of suppressing freedom of expression. This was a lie and we could not let them publish it."
- Nelba Blandon (as Director of Censorship, Nicaragua)

lemon10

  • Bay Watcher
  • Citrus Master
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #3 on: February 25, 2014, 10:11:34 pm »

I'm sure there is a far simpler way to do it, but this what I came up with.

Notes: Deflection angle=angle from v1/v2 to the black line.
First off, the magnitude of (x,y) and (x2,y2) is the same (or v1 and v2 as I will now call them).
Secondly, the angle from the black line is equal for both v1 and v2.

The first thing to do then is find the angle of both of them from 0 degrees. You can find the angle of the first from 0 with a simple (tan-1(y/x)). The angle of the second is (180+deflection angle+v1 angle from 0)=angle2, although its possible to simplify it.

Now that you have angles, you need to find the magnitude, which can be done with a simple sqrt(x1^2+y1^2).
Finally:
cos(angle2)*magnitude=x2
sin(angle2)*magnitude=y2
Logged
And with a mighty leap, the evil Conservative flies through the window, escaping our heroes once again!
Because the solution to not being able to control your dakka is MOAR DAKKA.

That's it. We've finally crossed over and become the nation of Da Orky Boyz.

noodle0117

  • Bay Watcher
  • I wonder what would happen if I pull it.
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #4 on: February 25, 2014, 11:48:02 pm »

A quick google search for 'pool table physics' and skimming the first two results reveals that it should bounce off at the same angle with which it approaches the wall (or whatever that line in your picture is).

See 'angular deflection' in http://library.thinkquest.org/TQ0013321/thescience.html
The other page was http://www.real-world-physics-problems.com/physics-of-billiards.html (but it was about balls colliding with each other or the cue, rather than the walls)

Good luck!
I know about the angle 1 equals angle 2 part.
I'm just looking to see if there's some formally defined formula where I can plug in the different values and get the correct x/y result.
aka it's easy for me to calculate the angle for any individual case. I'm just trying to find a way to generalize this.
Logged

lemon10

  • Bay Watcher
  • Citrus Master
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #5 on: February 25, 2014, 11:57:04 pm »

But thats pretty much what I just gave you in my previous post.  :-\
Well then, here it is in two easy equations:
x2=cos(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
y2=sin(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))

x2=cos(180+deflection angle+tan-1(y1/x1))*sqrt(x1^2+y1^2)
y2=sin(180+deflection angle+tan-1(y1/x1))*sqrt(x1^2+y1^2)
« Last Edit: February 27, 2014, 09:18:24 pm by lemon10 »
Logged
And with a mighty leap, the evil Conservative flies through the window, escaping our heroes once again!
Because the solution to not being able to control your dakka is MOAR DAKKA.

That's it. We've finally crossed over and become the nation of Da Orky Boyz.

noodle0117

  • Bay Watcher
  • I wonder what would happen if I pull it.
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #6 on: February 26, 2014, 02:04:50 am »

But thats pretty much what I just gave you in my previous post.  :-\
Well then, here it is in two easy equations:
x2=cos(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
y2=sin(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
:D
Thanks, I'll try em out.
Logged

Sirian

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #7 on: February 26, 2014, 02:32:15 am »

I think than I would use some sort of vector and renorm the incoming vector so that the point of intersection is 0,0 and rotate the plane so that the line we are bouncing on coincides with the y axis. At this point your vector is something like (x,y -> 0,0) and you turn it into (0,0 -> x,-y). Then you reverse the rotation and renorm back and you should have a valid outgoing vector ?

edit : wait... actually if you're only using velocities... then you only have to do a rotation to align the line you are bouncing on with the y axis, then your x becomes -x, you undo the rotation and you're done. I'm sure you can get reliable info on google about rotating your coordinates.
« Last Edit: February 26, 2014, 02:49:29 am by Sirian »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #8 on: February 26, 2014, 02:51:01 am »

remember all these solution assume the colliding object is a point and doesn't carry neither friction nor momentum  :P
Logged

ank

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #9 on: February 26, 2014, 02:57:24 am »

Don't we just need to rotate the vector by twice the angle between the vector and the reflecting line?
Logged

lemon10

  • Bay Watcher
  • Citrus Master
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #10 on: February 26, 2014, 02:14:14 pm »

Derp, I messed up the parentheses on my previous equations.
I put it as:
x2=cos(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
y2=sin(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
But it should be:
x2=cos(180+deflection angle+tan(y1/x1))*sqrt(x1^2+y1^2)
y2=sin(180+deflection angle+tan(y1/x1))*sqrt(x1^2+y1^2)


Also, keep in mind that you get small rounding errors using the basic sqrt distance equations, so if accuracy is particularly important it might be worth it to import a function that makes sure that there isn't any rounding errors.
Logged
And with a mighty leap, the evil Conservative flies through the window, escaping our heroes once again!
Because the solution to not being able to control your dakka is MOAR DAKKA.

That's it. We've finally crossed over and become the nation of Da Orky Boyz.

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #11 on: February 27, 2014, 02:45:22 am »

you get more error for the tan with x close to zero aaand a crash when x hits 0
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #12 on: February 27, 2014, 03:55:28 am »

you get more error for the tan with x close to zero aaand a crash when x hits 0
This is why you should be using vectors instead. If we have v being the velocity vector and n being the surface normal at the point of contact, then the new velocity vector is equal to v+2*(v-(v dot n)*v), as (v dot n)*v is equal to the portion of the velocity going parallel to the surface. I can draw a diagram to clarify this if you want.
Logged

noodle0117

  • Bay Watcher
  • I wonder what would happen if I pull it.
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #13 on: February 27, 2014, 06:03:59 pm »

Derp, I messed up the parentheses on my previous equations.
I put it as:
x2=cos(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
y2=sin(180+deflection angle+tan(y1/x1)*sqrt(x1^2+y1^2))
But it should be:
x2=cos(180+deflection angle+tan(y1/x1))*sqrt(x1^2+y1^2)
y2=sin(180+deflection angle+tan(y1/x1))*sqrt(x1^2+y1^2)


Also, keep in mind that you get small rounding errors using the basic sqrt distance equations, so if accuracy is particularly important it might be worth it to import a function that makes sure that there isn't any rounding errors.

Pretty sure it should be atan rather than tan.
This formula looks a great deal like what I'm currently using for calculating my reflections.
I remember reading somewhere that the answer can be calculated with less trigonometry somehow by using the dot product, but I can't quite recall how to do so
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #14 on: February 27, 2014, 06:14:41 pm »

I remember reading somewhere that the answer can be calculated with less trigonometry somehow by using the dot product, but I can't quite recall how to do so
This is why you should be using vectors instead. If we have v being the velocity vector and n being the surface normal at the point of contact, then the new velocity vector is equal to v+2*(v-(v dot n)*v), as (v dot n)*v is equal to the portion of the velocity going parallel to the surface. I can draw a diagram to clarify this if you want.
Was this what you were thinking of?
Logged
Pages: [1] 2