# 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.

## 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 on other sites
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 on other sites
That seems to be repulsing the ball but back in the direction it came from. Maybe its just a mistake on my part...

##### Share on other sites
Quote:
 Original post by realworld666That 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 on other sites
Quote:
Original post by haegarr
Quote:
 Original post by realworld666That 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 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 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.000000normal: 0.000000 1.000000 0.000000projectedLength: -2.000000scaledNormal: 0.000000 4.000000 0.000000reflected: 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.000000normal:-0.707107 0.707107 0.000000projectedLength: -2.121320scaledNormal: -3.000000 3.000000 0.000000reflected: -2.000000 1.000000 0.000000
what is also absolutely okay.

##### 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 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 on other sites
Quote:
 Original post by realworld666Maybe 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]

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 15
• 13
• 14
• 75
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004345

×