Reputation: 5193
Okay, so say I got a rectangle (This is all 2d) made from Thing A's x,y,width and height. How would I calculate it's normal?
Upvotes: 1
Views: 7023
Reputation: 5136
Cyan,
You are NOT looking for the normal as defined by the cross product or 3 dimensions. One minute and I will explain..
EDIT:
From this answer, it is obvious that what you are looking for is simply a vector perpendicular to a line. Not a vector perpendicular to a plane.
To mathematically evaluate
R = A - 2<A, N> N
You must first have a firm understanding of a Euclidean Vector.
Given a vector A (your angle of incidence):
A = <ax, ay>
Given the vector B (which represents a vector of the wall being bounced off of):
B = <bx, by>
The normal (perpendicular) to this vector is simply rotated 90 degrees. Mathematically:
N = <nx, ny> = <-bx, by>
Therefore R =
R = A - 2<A, N> N = ...
Lets first evaluate the Dot Product
<A, N> = ax*nx + ay*ny = ax*(-bx) + ay*by = ay*by - ax*bx
Then:
R = <ax, ay> - 2*(ay*by - ax*bx) * N
= <ax, by> - <2*(ay*by - ax*bx)*nx, 2*(ay*by - ax*bx)*ny>
= <ax, by> - <2*(ay*by - ax*bx)*(-bx), 2*(ay*by - ax*bx)*(by)>
= < ax + 2*bx*(ay*by - ax*bx), ay - 2*by*(ay*by - ax*bx) >
So all you need to do is determine a vector representing the wall you are bouncing off of (which is B), and your incident Vector (which is A).
EDIT (because of comment):
You really ought to spend time reviewing the link I posted to Euclidean vectors...
The basic idea is that you define an arbitrary mathematical origin. (Say for example, and the bottom of your wall). A vector representing your wall is then just an arrow, from the top to the bottom (or the bottom to the top). With the origin described at the base, this arrow will point 0 units in the x direction, but 100 units in the y direction. Therefore your vector for the wall (B) is just:
B = < 0, 100 >
(Note that the width of your wall is unimportant - it would bounce the same with a wall 1px thick, 50 px thick, or 100px thick).
But you'll want to normalize this vector so it has unit magnitude (length of 1). So the vector becomes:
B = <0, 1>
This follows from:
Vector length = sqrt( bx^2 + by^2 ) = sqrt( 0^2 + 1^2 ) = 1
N is then:
N = <1, 0> // for the left hand side wall
N = <-1, 0> // for the right hand side wall
Upvotes: 4
Reputation: 18276
A normal in 2D is the vector that did 90 degrees in the object, in the opposite direction of the object that should hit it comes.
There's fixed values for those normals, and those are:
West (1, 0); East (-1, 0); North (0, -1); South (0, 1);
Upvotes: 0
Reputation: 61026
Be careful. A rectangle has two possible normals:
In the plane it has 4 normals:
Upvotes: 1
Reputation: 103
What cyanprime is looking for is a normal of 1 line in 2D space.
That normal has to fulfill the following condition:
m_line * m_normal = -1
whereas m_line is the magnitude of the line and m_normal is the magnitude of the normal.
=> m_normal = -1 / m_line
Obviously produces errors if m_line = 0. So you will want to treat that case specially.
If m_line is not 0, you get your 2D-Vector
normal_vector = (1, m_normal)
Upvotes: 0
Reputation: 16007
The normal to the whole rectangle will be perpendicular to the plane of the rectangle (along the third dimension).
If you mean normal to a side of the rectangle (but in the same plane as the rectangle), then you can calculate the slopes of the sides, and the slope of the normal will be negative reciprocal of the slope of the side to which it is normal. (Or undefined if the slope of the side is zero.) If you want to put this normal on the rectangle, the midpoint of the side is a good place for it.
Upvotes: 0
Reputation: 5136
Go google 'Cross Product'. (http://en.wikipedia.org/wiki/Cross_product)
Take the vectors that define the edges of your rectangle as the vectors you are trying to cross.
Upvotes: 1
Reputation: 67380
Well if your rectangle is on the XY plane, then a normal is (0,0,1). No need for algebra!
Upvotes: 0
Reputation: 210455
If by "normal" you mean a perpendicular vector, take a look at the cross product: for the vectors
<a1, a2, a3>
and
<b1, b2, b3>
the cross product is
<a2 * b3 - b2 * a3, a1 * b3 - b1 * a3, a1 * b2 - b1 * a2>
... but "normal" in pure 2D doesn't make much sense.
Upvotes: 2