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 4820 times)

lemon10

  • Bay Watcher
  • Citrus Master
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #15 on: February 27, 2014, 06:28:07 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
It should indeed be arctan, it just didn't copy properly when I took it from my last post.

EDIT: Not sure how you missed both Ruler's post (detailing dot products) and my first post (which didn't have any errors despite being slightly longer then post with just the equations) on though.
« Last Edit: February 27, 2014, 06:30:25 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.

ejseto

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #16 on: February 27, 2014, 07:40:28 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))

This isn't right. You can easily show it doesn't work for a vertical surface, in which case (180+deflection angle+tan(y1/x1)) = 270. Then x2 = 0 which makes no sense. In general, once you have the angle of incidence (angle between incoming ray and the normal), you just run the coordinates through a rotation matrix with an angle twice that. From the form of the rotation matrix it should be clear your solution cannot work, since in general x2 depends on both x1 AND y1. You also completely ignored the orientation of the surface.

Anyway, Ruler's vector solution is correct and also avoids all sorts of divide-by-zero silliness. I'd avoid the rotation matrix altogether, but just in case:

[[x2],      [[ cos(a), -sin(a)],     [[x1],
 [y2]]  =   [ sin(a), cos(a)]]   *  [y1]]

for angle of rotation 'a.'

Or equivalently, since idk if you can read that matrix equation typed out like that,
x2 = x1cos(a) - y1sin(a)
y2 = x1sin(a) + y1cos(a)

You can also just "unrotate" the system by its orientation angle (thus aligning the normal with the x-axis, assuming 0 radians = x-axis), set y2=-y1, and rotate it back, as someone else suggested.
Logged

lemon10

  • Bay Watcher
  • Citrus Master
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #17 on: February 27, 2014, 09:16:44 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))

This isn't right. You can easily show it doesn't work for a vertical surface, in which case (180+deflection angle+tan(y1/x1)) = 270. Then x2 = 0 which makes no sense. In general, once you have the angle of incidence (angle between incoming ray and the normal), you just run the coordinates through a rotation matrix with an angle twice that. From the form of the rotation matrix it should be clear your solution cannot work, since in general x2 depends on both x1 AND y1. You also completely ignored the orientation of the surface.

Anyway, Ruler's vector solution is correct and also avoids all sorts of divide-by-zero silliness. I'd avoid the rotation matrix altogether, but just in case:

[[x2],      [[ cos(a), -sin(a)],     [[x1],
 [y2]]  =   [ sin(a), cos(a)]]   *  [y1]]

for angle of rotation 'a.'

Or equivalently, since idk if you can read that matrix equation typed out like that,
x2 = x1cos(a) - y1sin(a)
y2 = x1sin(a) + y1cos(a)

You can also just "unrotate" the system by its orientation angle (thus aligning the normal with the x-axis, assuming 0 radians = x-axis), set y2=-y1, and rotate it back, as someone else suggested.
You can also easily show that you didn't read my post just before yours.
It should indeed be arctan, it just didn't copy properly when I took it from my last post.
So yeah, tan doesn't work. arctan does.

EDIT: I will also note that its typically better to use the arctan2(y,x) function instead of arctan(y/x) in order to avoid the problems of dividing by 0. But yeah, doing it the dot product way is probably better.
« Last Edit: February 27, 2014, 09:21:56 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.

ejseto

  • Bay Watcher
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #18 on: February 27, 2014, 11:57:07 pm »


You can also easily show that you didn't read my post just before yours.


Actually I did. I'm not taking issue with whether it's arctan or not. I assumed that's what you meant, which actually is implied by what I posted. It's still not right. Just from my example you can see your equations don't work. If a ray is incident on a vertical surface (e.g. y-axis), say, at positive angle theta with respect to the normal (x-axis in this case), it should reflect off at angle -theta, meaning x2=x1 and y2=-y1.

You defined "deflection angle" as the complement of theta, i.e. pi/2 - theta
arctan(y1/x1) = theta (in this example)
pi + deflection angle + arctan(y1/x1) = 3pi/2
cos(3pi/2) = 0

Therefore, x2 = 0 with your method.
Logged

LoSboccacc

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

can't we just use an engine?  :P

aaanyway
for a ball moving on velocity v bouncing to a plane that has a normal of n

u = (v · n / n · n) n  (velocity parallel to the normal)
w = v − u              (remainder)

· is a dot product, all other are the usual vector operations

v′ = w − u (velocity parallel to the normal is reversed by the collision, the other component remains the same)
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 #20 on: February 28, 2014, 01:29:12 pm »

But using an engine means you miss out half the fun...
Anyways I'll try implementing the dot product variation and see if I can get stuff to work.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #21 on: February 28, 2014, 10:22:30 pm »

can't we just use an engine?  :P

aaanyway
for a ball moving on velocity v bouncing to a plane that has a normal of n

u = (v · n / n · n) n  (velocity parallel to the normal)
w = v − u              (remainder)

· is a dot product, all other are the usual vector operations

v′ = w − u (velocity parallel to the normal is reversed by the collision, the other component remains the same)
Or to finish the equations:
v' = v - 2 ((v · n) / (n · n)) * n
Which would probably take about as much time to compute as a single trig function.
And in general, if you're using trig functions for a game engine physics system outside of an initial conversion, you're probably doing it wrong. http://www.iquilezles.org/www/articles/noacos/noacos.htm
Logged

LoSboccacc

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

If you are fixing trigonometry moving to vector you better go to quaternion directly, or werid surprises will await you on the animation road
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: 2D Game Physics, how do I calculate reflections?
« Reply #23 on: March 04, 2014, 01:56:40 am »

If you are fixing trigonometry moving to vector you better go to quaternion directly, or werid surprises will await you on the animation road
And if you're feeling extra special, go for Dual Quaternions; the fun numbers where positions meet rotations at the corner of dual numbers (n^2 = 0, n != 0) and imaginary numbers (n^2 = -1) to make everything as simple as middleschool algebra.
Logged
Pages: 1 [2]