简体   繁体   English

AS3:用鼠标旋转DisplayObject

[英]AS3: Rotate a DisplayObject with the Mouse

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. 我目前正在尝试弄清楚如何旋转对象,以便其角跟随鼠标的当前x和y。

例子1 例子2 例子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 对TRABADOUR的回应

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. 我熟悉Java和AS3中的Vector类型,因为它们存储某种类型的值的列表。 Beyond that, I'm lost. 除此之外,我迷路了。

var vector:* = WTF.forReals();

Next, save the distance between registrationPoint and mouseDownPoint . 接下来,保存registrationPointmouseDownPoint之间的距离。 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. 我记得在高中时曾学习过计算两个点之间的距离的知识,因此,我确定我可以挖掘出两个2d点之间的距离的公式。

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 . 接下来,要确定约束比例,我们获取当前鼠标的位置,确定其与registrationPoint距离,然后将其除以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? 我的问题是: 当我不希望它们受到约束时如何获得值,例如scaleX和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? 使用上面定义的变量,您能帮帮我吗?

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). 将缩放/旋转中心的鼠标坐标表示为(x0,y0),将处于未缩放,未旋转状态的鼠标角表示为(x1,y1)。 Store the point (x0,y0) and cache both the vector and the distance between (x0,y0) and (x1,y1). 存储点(x0,y0),并缓存矢量和(x0,y0)与(x1,y1)之间的距离。 Denote the cached vector by v0 and the distance by d0. 用v0表示缓存的矢量,用d0表示距离。

To get the scale factor just compute the current mouse co-ords distance to (x0,y0) and divide that by d0. 要获得比例因子,只需计算当前鼠标坐标到(x0,y0)的距离,然后将其除以d0。

To get the rotation angle first calculate the vector between (x0,y0) and the current mouse co-ords. 要获取旋转角度,请首先计算(x0,y0)与当前鼠标坐标之间的向量。 Denote this by v. Then calculate the angle by using the dot product formula 用v表示。然后使用点积公式计算角度

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

which will give you something between 0 and pi. 这会给你0到pi之间的值。 To get into the correct quadrant just examine the sign of the cross product of v and v0 and adjust accordingly. 要进入正确的象限,只需检查v和v0的叉积的符号并相应地进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM