AS3:用鼠标旋转 DisplayObject

发布于 2024-09-13 06:40:03 字数 1786 浏览 4 评论 0原文

我基本上试图创建一个显示对象转换管理器,它允许我缩放/旋转对象。我目前正在尝试弄清楚如何旋转对象,使其角点跟随鼠标当前的 x 和 y 方向。

示例 1 示例 2 示例 3

我总是对这类事情的数学感到困惑。我知道如何监听事件和所有内容,我只是不确定如何计算要应用的轮换量。我可能会通过矩阵旋转,这样我就可以围绕其中心而不是其左上角旋转对象。有谁对这方面的数学更熟练可以帮助我吗?

回应特罗巴杜尔

你的回答太精彩了,我根本无法理解。让我解释一下我能掌握什么和不能掌握什么。以下是我理解/不理解的内容的演练:

首先,保存图像中间的内容,即所谓的注册点,我们将围绕该注册点旋转所有内容:

var box:Sprite = new BeautifulSprite();
box.width = box.height = 100;
/* ... */
var registrationPoint:Point = new Point(box.x + box.width / 2, 
        box.y + box.height / 2);

到目前为止我正确吗?如果是这样,我会继续。

其次,表示原始鼠标按下的位置:

var mouseDownPoint:Point = new Point(box.mouseX, box.mouseY);

第三,存储“向量”。问题是,我不确定你的意思是向量。我熟悉 Java 和 AS3 中的 Vector 类型,因为它们存储特定类型的值列表。除此之外,我迷路了。

var vector:* = WTF.forReals();

接下来,保存 registrationPointmouseDownPoint 之间的距离。我记得在高中时就学过计算两点之间的距离,所以我确信我可以找出两个二维点之间的距离的公式。

var distance:Number = calculateDistance(registrationPoint, mouseDownPoint);

我知道我已经很接近了!接下来,为了确定约束比例,我们获取当前鼠标位置,确定其到 registrationPoint 的距离,并将其除以 distance

var constrainedScale:Number = calculateDistance(registrationPoint, new Point(mouseX, mouseY)) / distance;

我的问题是:当我不希望它们受到约束时,如何获取它们,例如scaleX和scaleY?

现在,为了获得围绕注册点的实际旋转,我完全迷失了。您能使用我上面定义的变量来帮助我吗?

I am basically trying to create a display object transformation manager which will allow me to scale/rotate objects. I am currently trying to figure out how to rotate an object so its corner follows the current x and y of the mouse.

Example 1

Example 2

Example 3

I always get confused on the math of things like this. I know how to listen for the events and everything, I just am unsure of how to calculate the amount of rotation to apply. I will probably be rotating via a Matrix so I can rotate the object around its center, rather than the upper-left corner of it. Can anyone who is more skilled with the math of this help me out?

IN RESPONSE TO TROBADOUR

Your answer is so brilliant that I can't understand it at all. Let me explain what I can grasp and what I can't. Here's a walkthrough of what I do/don't understand:

First, save what will be the middle of the image, the so-called registration point around which we will rotate everything:

var box:Sprite = new BeautifulSprite();
box.width = box.height = 100;
/* ... */
var registrationPoint:Point = new Point(box.x + box.width / 2, 
        box.y + box.height / 2);

Am I correct so far? If so, I'll continue.

Second, denote the original mouse-down position:

var mouseDownPoint:Point = new Point(box.mouseX, box.mouseY);

Third, store the "vector." Problem is, I'm not sure of what you mean vector. I am familiar with Vector types in Java and AS3, in that they store a list of values of a certain type. Beyond that, I'm lost.

var vector:* = WTF.forReals();

Next, save the distance between registrationPoint and mouseDownPoint. I remember learning about calculating distance between two points way back in high school, so I'm sure I could dig up the formula for distance between two 2d points.

var distance:Number = calculateDistance(registrationPoint, mouseDownPoint);

I know I'm getting close! Next, to determine the constrained scale, we get the current mouse location, determine its distance to registrationPoint, and divide that by distance.

var constrainedScale:Number = calculateDistance(registrationPoint, new Point(mouseX, mouseY)) / distance;

My question here is: how do I get values when I don't want them constrained, like scaleX and scaleY?

Now, to get the actual rotation around the registration point, I am completely lost. Could you help me out, using the variables I have defined above?

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

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

发布评论

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

评论(1

牛↙奶布丁 2024-09-20 06:40:03

将缩放/旋转中心的鼠标坐标表示为 (x0,y0),将处于未缩放、未旋转状态时角点的鼠标坐标表示为 (x1,y1)。存储点 (x0,y0) 并缓存向量以及 (x0,y0) 和 (x1,y1) 之间的距离。用 v0 表示缓存的向量,用 d0 表示距离。

要获得比例因子,只需计算当前鼠标坐标到 (x0,y0) 的距离并将其除以 d0。

要获取旋转角度,首先计算 (x0,y0) 和当前鼠标坐标之间的向量。 计算角度

用 v 表示。然后使用点积公式v.v0 = |v| 。 d0 cos(theta)

将为您提供 0 到 pi 之间的值。要进入正确的象限,只需检查 v 和 v0 叉积的符号并进行相应调整。

Denote the mouse co-ords of the scale/rotation centre as (x0,y0) and the mouse co-ords of the corner when in an unscaled, unrotated state as (x1,y1). Store the point (x0,y0) and cache both the vector and the distance between (x0,y0) and (x1,y1). Denote the cached vector by v0 and the distance by d0.

To get the scale factor just compute the current mouse co-ords distance to (x0,y0) and divide that by d0.

To get the rotation angle first calculate the vector between (x0,y0) and the current mouse co-ords. Denote this by v. Then calculate the angle by using the dot product formula

v.v0 = |v| d0 cos(theta)

which will give you something between 0 and pi. To get into the correct quadrant just examine the sign of the cross product of v and v0 and adjust accordingly.

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