Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection? How do I do it?

This is over my head, can someone explain it to me better? http://mathworld.wolfram.com/Reflection.html

I'm making a 2d breakout fighting game, so I need the ball to be able to reflect when it hits a wall, paddle, or enemy (or a enemy hits it).

all their formula's are like: x_1^'-x_0=v-2(v·n^^)n^^.

And I can't fallow that. (What does ' mean or x_0? or ^^?)

like image 232
CyanPrime Avatar asked Nov 27 '22 12:11

CyanPrime


1 Answers

The formula for reflection is easier to understand if you think to the geometric meaning of the operation of "dot product".

The dot product between two 3d vectors is mathematically defined as

<a, b> = ax*bx + ay*by + az*bz

but it has a nice geometric interpretation

The dot product between a and b is the length of the projection of a over b taken with a negative sign if the two vectors are pointing in opposite directions, multiplied by the length of b.

dot product geometric interpretation

Something that is immediately obvious using this definition and that it's not evident if you only look at the formula is for example that the dot product of two vectors doesn't change if the coordinate system is rotated, that the dot product of two perpendicular vectors is 0 (the length of the projection is clearly zero in this case) or that the dot product of a vector by itself is the square of its length.

Something that is instead less obvious using the geometric interpretation is that the dot product is commutative, i.e. that <a, b> = <b, a> (fact that is clear considering the formula).

An important point to consider is also that if the length of b is 1 then the dot product <a, b> is simply the length of the projection of a over b (taken with the proper sign).

Given this interpretation the formula for computing the reflection over a plane is quite easy to understand:

vector reflection

To compute the reflected vector r, given a vector a and a plane with normal n you just need to use the formula:

r = a - 2<a, n> n

the height h in the figure is in this case just <a, n> (note that n is assumed to be of unit length) and so it should be clear that you need to move twice that height in the direction of the normal.

If you consider the proper dot product signs you should see that the formula applies also when the incident vector a and the plane normal n are facing in the same direction.

like image 74
6502 Avatar answered Dec 09 '22 21:12

6502