**0**

# 2D reflection

Started by Oct 06 2008 04:17 AM

,
6 replies to this topic

###
#1
Members - Reputation: **104**

Posted 06 October 2008 - 04:17 AM

Believe me.... I've googled this. But I don't have a math background. I'm trying to understand how Vector2.Reflect works (in XNA to be specific). What is a normal, in plain (plane lol) english and how do you apply it?
If an object is approaching a tile, let's say. Its CollisionBox will be overlapping the tile next frame. How do I calculate the bounce?

###
#3
Members - Reputation: **778**

Posted 06 October 2008 - 06:44 AM

Wikipedia has got something to say about reflections -- including a nice picture:

From the name

From the name

*Vector2*I assume you're asking about reflection in two dimensions. Your "mirror" in this case is the surface you're reflecting from.###
#4
Crossbones+ - Reputation: **7022**

Posted 06 October 2008 - 06:52 AM

A normal is a vector that is perpendicular to another vector or surface. A normalized vector is a vector with length 1. The terms are a little confusing, but they are very different. Most normal vectors are normalized, just because it makes calculations easier.

This Wolfram article helps explain reflection vectors. This Wolfram article explains normal vectors. This Wikipedia page explains a normal vector (a.k.a. surface normal) in relatively clear English terms.

Basically if you have a vector v, which represents the object's velocity, and a normalized normal vector n, which is perpendicular to the surface with which the object collides, then the new velocity v' is given by the equation:

v' = 2 * (v . n) * n - v;

Where '*' is the scalar multiplication operator, '.' is the dot product of two vectors, and '-' is the subtraction operator for two vectors. v is reflected off of the surface, and gives a reflection vector v' which is used as the new velocity of the object.

This Wolfram article helps explain reflection vectors. This Wolfram article explains normal vectors. This Wikipedia page explains a normal vector (a.k.a. surface normal) in relatively clear English terms.

Basically if you have a vector v, which represents the object's velocity, and a normalized normal vector n, which is perpendicular to the surface with which the object collides, then the new velocity v' is given by the equation:

v' = 2 * (v . n) * n - v;

Where '*' is the scalar multiplication operator, '.' is the dot product of two vectors, and '-' is the subtraction operator for two vectors. v is reflected off of the surface, and gives a reflection vector v' which is used as the new velocity of the object.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

###
#5
Members - Reputation: **104**

Posted 06 October 2008 - 09:20 AM

Quote:

Original post by MikeTacular

A normal is a vector that is perpendicular to another vector or surface. A normalized vector is a vector with length 1. The terms are a little confusing, but they are very different. Most normal vectors are normalized, just because it makes calculations easier.

This Wolfram article helps explain reflection vectors. This Wolfram article explains normal vectors. This Wikipedia page explains a normal vector (a.k.a. surface normal) in relatively clear English terms.

Basically if you have a vector v, which represents the object's velocity, and a normalized normal vector n, which is perpendicular to the surface with which the object collides, then the new velocity v' is given by the equation:

v' = 2 * (v . n) * n - v;

Where '*' is the scalar multiplication operator, '.' is the dot product of two vectors, and '-' is the subtraction operator for two vectors. v is reflected off of the surface, and gives a reflection vector v' which is used as the new velocity of the object.

how do you find n? let's say the wall is at an angle of 87 degrees (in vector form).

###
#6
Crossbones+ - Reputation: **7022**

Posted 06 October 2008 - 10:22 AM

There are two ways to find a normal:

1) If you have the angle the normal makes with the horizontal (like the picture below), the normal vector N can be found using a little trig.

N.x = -sin(87); // be sure to convert to radians though!

N.y = cos(87);

Using this method, N will already be normalized (have a length of 1) because of the properties of sin and cos.

2) If you don't have the angle, but instead two points that define a line segment, you can use another method. If a line segment is defined by points A and B, then the direction of the line segment is the line going from point A to point B, or in other words, the direction of the line segment can be found by doing B - A. Let's define vector v = B - A. The vector which is perpendicular to v will also be perpendicular to line segment AB. In 2D, it is very easy to find a vector perpendicular to another vector. The formula is:

// P stands for Perpendicular Vector, and is perpendicular to vector V

P.x = -v.y;

P.y = v.x;

// Since N is perpendicular to AB, and so is P, we can say that N = P, or in other words:

N.x = -v.y;

N.y = v.x;

Now we just need to resize vector N so it has a length of 1. To do this, we divide each component of vector N by N's total length. This is called normalizing the vector.

float length = std::sqrt(N.x * N.x + N.y * N.y);

N.x /= length;

N.y /= length;

And done! Now you have a normal vector N perpendicular to line segment AB.

If you notice, the equation for the coordinates of a circle are:

x = cos(theta);

y = sin(theta);

This is the equation I used in method 1). Notice that in method 2), I say that a perpendicular vector can be found by swapping the components of a vector and multiplying x by -1. See something similar in both methods?

Remember this is for 2D, but the same principles can easily be extended to 3D.

1) If you have the angle the normal makes with the horizontal (like the picture below), the normal vector N can be found using a little trig.

N.x = -sin(87); // be sure to convert to radians though!

N.y = cos(87);

Using this method, N will already be normalized (have a length of 1) because of the properties of sin and cos.

2) If you don't have the angle, but instead two points that define a line segment, you can use another method. If a line segment is defined by points A and B, then the direction of the line segment is the line going from point A to point B, or in other words, the direction of the line segment can be found by doing B - A. Let's define vector v = B - A. The vector which is perpendicular to v will also be perpendicular to line segment AB. In 2D, it is very easy to find a vector perpendicular to another vector. The formula is:

// P stands for Perpendicular Vector, and is perpendicular to vector V

P.x = -v.y;

P.y = v.x;

// Since N is perpendicular to AB, and so is P, we can say that N = P, or in other words:

N.x = -v.y;

N.y = v.x;

Now we just need to resize vector N so it has a length of 1. To do this, we divide each component of vector N by N's total length. This is called normalizing the vector.

float length = std::sqrt(N.x * N.x + N.y * N.y);

N.x /= length;

N.y /= length;

And done! Now you have a normal vector N perpendicular to line segment AB.

If you notice, the equation for the coordinates of a circle are:

x = cos(theta);

y = sin(theta);

This is the equation I used in method 1). Notice that in method 2), I say that a perpendicular vector can be found by swapping the components of a vector and multiplying x by -1. See something similar in both methods?

Remember this is for 2D, but the same principles can easily be extended to 3D.