【问题标题】:How to rotate a rectangle around centroid in flex 3如何在flex 3中围绕质心旋转矩形
【发布时间】:2012-10-01 17:02:54
【问题描述】:

我正在使用 matrix.rotate 方法来旋转矩形(在我的例子中是框)。 我的旋转事件如下所示

公共函数transformObject(transformEvent:TransformEvent):void{

        var numChildrn:int = _markedObjectLayer.numChildren;
        var tempMatrix: Matrix = null;
        var tempx:Number;
        var tempy:Number;
        var tempHeight:Number;
        var tempWidth:Number;
        for(var i:int = 0; i < numChildrn; i++){
            var chld:MarkedObject = ObjectLayer.getChildAt(i)
            if (chld.selected){
                var  height:int = (BoxObject) chld.height;
                var  width:int = (BoxObject) chld.width;


                tempMatrix = chld.transform.matrix;


                tempHeight=height;  
                tempWidth=width;


                tempMatrix = MatrixTransformer.transform(tempMatrix,transformEvent.angle);


                tempMatrix.tx=tempx;
                tempMatrix.ty=tempy

                chld.transform.matrix = tempMatrix;
            }
        }

        invalidateDisplayList();
    }
} 

Matrix.transform方法调用matrix.rotate方法

公共静态函数变换(sourceMatrix:Matrix, 旋转:数字=0):矩阵 {

        sourceMatrix = MatrixTransformer.rotate(sourceMatrix, rotation, "degrees");

        return sourceMatrix;
    }



    /**
     * Rotates a matrix and returns the result. The unit parameter lets the user specify "degrees", 
     * "gradients", or "radians". 
     */
    public static function rotate(sourceMatrix:Matrix, angle:Number, unit:String = "radians"):Matrix {
        if (unit == "degrees") 
        {
            angle = Math.PI * 2 *( angle / 360);
        }

        sourceMatrix. rotate(angle)
        return sourceMatrix;
    }

问题是 x 和 y 是盒子的左核心,因此它围绕左角旋转。但是,如果我尝试将 temp.x 和 temp.y 作为质心值,它不会围绕质心旋转?

谁能建议我在这里做错了什么?

谢谢 阿克谢

【问题讨论】:

标签: actionscript-3 apache-flex matrix actionscript flex3


【解决方案1】:

如果您真的想要或必须直接使用矩阵,您可以使用内置的 flash 类更方便地做到这一点:fl.motion.MatrixTransformer:

MatrixTransformer.rotateAroundInternalPoint(matrix, centroidX, centroidY, angleInDegrees);

有关更多信息,请参阅MatrixTransformer 上的 Adob​​e docs

但是,如果您不需要使用转换矩阵,更简单的解决方案是:

  • 以 (0, 0) 为其质心的方式绘制对象
  • 使用DisplayObject 中的简单rotation 属性,以更简单的方式实现相同的目标

【讨论】:

  • 感谢 Jakub 的回复。但是 fl.motion.MatrixTransformer 是否可以在 flex 中导入?我试着看,但没有这样的图书馆可用。我应该下载一些库吗?
  • 我还没有导入 fl 库,但是物体在每次旋转后都在以螺旋运动的方式移动..我会尽快发布代码
【解决方案2】:

想通了。看来我没有在旋转前后将它们翻译成适当的坐标位置

//第一步固定矩形的坐标。我将它们添加到一个事件中,以便它们保持静态

    if (TransformEvent.X == 0 && TransformEvent.Y == 0)
        {
         TransformEvent.X = chld.x;
           TransformEvent.Y = chld.y;
         }

//下一步获取矩形的质心

       tempx = TransformEvent.X + width/2;
       tempy= TransformEvent.Y +height/2;

// 第三步:旋转前平移

        tempMatrix.translate(-1*tempx,-1*tempy);

//旋转矩形

        tempMatrix = MatrixTransformer.transform(tempMatrix,transformEvent.angle);

//旋转后平移到质心

         tempMatrix.translate(tempx,tempy);

//将矩阵分配回矩形

         chld.transform.matrix = tempMatrix;

感谢您的所有帮助。这个网站也帮助我翻译了一些东西 http://www.foxarc.com/blog/article/66.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2015-03-29
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多