Jump to content
  • Advertisement
Sign in to follow this  
realworld666

Can I get a reflection vector from this information

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two points in space. The start point and end point of a moving ball. The ball has come to a sstandstill at the end point and I want to set it off in a direction that looks realistic which I assume would be some kind of reflection of this vector. The only information I have though is the initial vector and the normal of the polygon the ball is currently on.

Share this post


Link to post
Share on other sites
Advertisement
r := -2 n ( n . i ) + i
where
r is the reflected vector (pointing away from the surface),
n is the surface normal,
i is the incoming vector (pointing towards the surface).

The sign of i has to be negated, of course, if you want it also point away from the surface.

[Edited by - haegarr on June 17, 2008 5:17:22 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by realworld666
That seems to be repulsing the ball but back in the direction it came from. Maybe its just a mistake on my part...

In that case
r == -i
must hold, so that
n ( n . i ) == i
must be concluded. In other words, the ball hits the surface perpendicularly (i is a multiple of n), and then it is absolutely okay if it is reflected exactly back. So I assume there's a mistake in the implementation. Perhaps you can show us some code snippet?

Share this post


Link to post
Share on other sites
Quote:
Original post by haegarr
Quote:
Original post by realworld666
That seems to be repulsing the ball but back in the direction it came from. Maybe its just a mistake on my part...

In that case
r == -i
must hold, so that
n ( n . i ) == i
must be concluded. In other words, the ball hits the surface perpendicularly (i is a multiple of n), and then it is absolutely okay if it is reflected exactly back. So I assume there's a mistake in the implementation. Perhaps you can show us some code snippet?

The code I have is:

Vector4 inV = (startPos - EndPos);
Vector4 N = polyNormal;
repulseV = (-2.f * N * ( N.DotProduct(inV) ) + inV);
repulseV.Normalize();

From looking at the debug output, inV and repulseV are pretty much the same.

Share this post


Link to post
Share on other sites
Sorry thats a blatant lie.
Some example inputs and outputs I have:

in 0.01, 0.00, 0.10
out 0.12, 0.00, 0.99

in -0.03, 0.00, 0.07
out -0.42, 0.02, 0.89

Share this post


Link to post
Share on other sites
For the examples you gave: I need to know the normal, too.

I also have implemented it now for test purposes:

ModelSpace::Vector_t incoming(1,-2,0);
incoming.print();

ModelSpace::Vector_t normal(0,1,0);
normal.print();

float projectedLength = normal.dot(incoming);
::printf("%f\n",projectedLength);

ModelSpace::Vector_t scaledNormal = normal*(-2.0f*projectedLength);
scaledNormal.print();

(scaledNormal+incoming).print();


With the simple example (as hardcoded above) I got

incoming: 1.000000 -2.000000 0.000000
normal: 0.000000 1.000000 0.000000
projectedLength: -2.000000
scaledNormal: 0.000000 4.000000 0.000000
reflected: 1.000000 2.000000 0.000000
what is absolutely okay. Can you verify this result with your code?


EDIT: A bit more complicated example:

incoming: 1.000000 -2.000000 0.000000
normal:-0.707107 0.707107 0.000000
projectedLength: -2.121320
scaledNormal: -3.000000 3.000000 0.000000
reflected: -2.000000 1.000000 0.000000
what is also absolutely okay.

Share this post


Link to post
Share on other sites
Yes. I get the same result.
OK I'll take a look at the numbers I'm passing around. It's possibly not this code that is failing but the application of the result as a force

Share this post


Link to post
Share on other sites
Maybe I'm going down the wrong road here but from what I can see this is going wrong because my ball movement is in the XZ plane and my normal is close to 0,1,0. Wouldn't that give me a reflection in the wrong axis?

Share this post


Link to post
Share on other sites
Quote:
Original post by realworld666
Maybe I'm going down the wrong road here but from what I can see this is going wrong because my ball movement is in the XZ plane and my normal is close to 0,1,0. Wouldn't that give me a reflection in the wrong axis?

The cited situation denotes the degenerated case w.r.t. reflection. A plane with the normal [ 0 1 0 ]T _is_ the XZ plane; so moving the ball in the XZ plane means that the ball is rolling. It cannot be reflected to anywhere.

Mathematically the angle between the normal and the incoming vector is 90°, and hence the dot-product returns zero. In that case the formula is reduced to
r = i
what means nothing else than that the ball keeps on moving in the same direction; in other words it is rolling. That is expected behaviour, isn't it? Perhaps I haven't understood the intention behind the OP...


EDIT: It may happen, of course, that the program suffers from numerical instability if the values gets close to the degenerated case. But gravity and collision detection should correct that. For now I don't see why it shouldn't work well.

[Edited by - haegarr on June 17, 2008 8:10:22 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!