如何计算正常值?

发布于 2024-10-27 16:32:43 字数 57 浏览 1 评论 0原文

好吧,假设我有一个由物体 A 的 x、y、宽度和高度制成的矩形(这都是 2d)。怎么计算才算正常呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蘑菇王子 2024-11-03 16:32:43

青色,

您不是在寻找由叉积或 3 维定义的法线。一分钟,我会解释..

编辑:

来自

要进行数学评估,

R = A - 2<A, N> N

您必须首先对欧几里得向量有深入的了解。

给定向量 A(入射角):

A = <ax, ay> 

给定向量 B(代表被弹回的墙壁的向量):

B = <bx, by>

该向量的法线(垂直)只需旋转 90 度。从数学上来说:

N = <nx, ny> = <-bx, by>

因此 R =

R = A - 2<A, N> N = ...

让我们首先评估点积

<A, N> = ax*nx + ay*ny = ax*(-bx) + ay*by = ay*by - ax*bx

然后:

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) >

因此,您需要做的就是确定一个代表您要反弹的墙壁的向量(即 B)和您的入射向量(即 A)。

编辑(因为评论):

您确实应该花时间查看我发布到欧几里得向量的链接...

基本思想是定义任意数学原点。 (例如,你的墙的底部)。代表你的墙的向量只是一个从上到下(或从下到上)的箭头。由于原点位于底部,该箭头将指向 x 方向 0 个单位,但指向 y 方向 100 个单位。因此,墙壁 (B) 的矢量就是:(

B = < 0, 100 >

请注意,墙壁的宽度并不重要 - 它与 1 像素厚、50 像素厚或 100 像素厚的墙壁的反射效果相同)。

但您需要对该向量进行归一化,使其具有单位大小(长度为 1)。因此向量变为:

B = <0, 1>

这可从:

Vector length = sqrt( bx^2 + by^2 ) = sqrt( 0^2 + 1^2 ) = 1

N 则为:

N = <1, 0>  // for the left hand side wall
N = <-1, 0> // for the right hand side wall

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
琉璃梦幻 2024-11-03 16:32:43

如果“法线”指的是垂直向量,请查看叉积:向量

<a1, a2, a3>

<b1, b2, b3>

叉积是

<a2 * b3 - b2 * a3, a1 * b3 - b1 * a3, a1 * b2 - b1 * a2>

......但是纯二维中的“正常”没有多大意义。

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.

十年不长 2024-11-03 16:32:43

去谷歌搜索“叉积”。 (http://en.wikipedia.org/wiki/Cross_product)

获取定义矩形的边缘作为您试图穿过的向量。

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.

执着的年纪 2024-11-03 16:32:43

当心。矩形有两个可能的法线:

在此处输入图像描述

在平面中它有 4 个法线:

在此处输入图像描述

Be careful. A rectangle has two possible normals:

enter image description here

In the plane it has 4 normals:

enter image description here

挽清梦 2024-11-03 16:32:43

如果矩形位于 XY 平面上,则法线为 (0,0,1)。不需要代数!

Well if your rectangle is on the XY plane, then a normal is (0,0,1). No need for algebra!

江南烟雨〆相思醉 2024-11-03 16:32:43

整个矩形的法线将垂直于矩形的平面(沿第三维)。

如果您的意思是垂直于矩形的(但与矩形位于同一平面),那么您可以计算边的斜率,并且法线的斜率将是法线一侧的斜率。 (如果边的斜率为零,则未定义。)如果您想将此法线放在矩形上,边的中点是一个不错的位置。

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.

慈悲佛祖 2024-11-03 16:32:43

cyyprime 正在寻找的是 2D 空间中 1 条线的法线。

该法线必须满足以下条件:

m_line * m_normal = -1

,而 m_line 是线的大小,m_normal 是法线的大小。

=> m_normal = -1 / m_line

如果 m_line = 0,显然会产生错误。因此您需要特殊对待这种情况。

如果 m_line 不为 0,您将得到 2D 向量

normal_vector = (1, m_normal)

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)

向地狱狂奔 2024-11-03 16:32:43

2D 中的法线是在对象中形成 90 度的向量,与应该撞击它的对象的方向相反。

这些法线有固定值,它们是:

West (1, 0);
东 (-1, 0);
北 (0, -1);
南 (0, 1);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文