【问题标题】:Continuous object rotation around point围绕点的连续对象旋转
【发布时间】:2016-03-07 07:13:39
【问题描述】:

我想做一个类似于rotateAroundInternalPoint()的函数。到目前为止,我的解决方案是这样的:

import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;

addEventListener( Event.ENTER_FRAME, onFrame );

function onFrame(e:Event):void
{
    var m:Matrix = item.transform.matrix.clone();
    var point:Point = new Point( 50, 50 ); // The object's width and height are 100px, so 50 is the center
    point = m.transformPoint( point );
    m.translate( -point.x, -point.y );

    m.rotate( 5 * ( Math.PI / 180 ) );
    m.translate( point.x, point.y );

    item.transform.matrix = m;
}

然而,这段代码有一个根本性的缺陷——每次迭代都会变得越来越不精确。

有人能指出造成这种情况的原因以及解决方案是什么吗?

【问题讨论】:

  • 我仍然会接受一个更好的答案,它不依赖于参考矩阵,而是依赖于更好的数学。

标签: actionscript-3 matrix geometry computational-geometry haxe


【解决方案1】:

我已经通过引入一个不会改变的参考矩阵解决了这个问题,因此初始迭代中的错误将不存在。

下面是实现:

import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;

var referenceMatrix:Matrix = item.transform.matrix.clone();

addEventListener( Event.ENTER_FRAME, onFrame );

var i:Number = 0; // you'll need this because the referenceMatrix rotation will only go one step, so instead you need to increase the rotation

function onFrame(e:Event):void
{
    var m:Matrix = referenceMatrix.clone();
    var point:Point = new Point( 100, 100 ); // pivot point local to the object's coordinates

    point = m.transformPoint( point );
    m.translate( -point.x, -point.y );
    m.rotate( i * ( Math.PI / 180 ) );
    m.translate( point.x, point.y );
    item.transform.matrix = m;

    i += 1.2; // rotation step
}

请注意,此代码是在框架中编写的,并未针对实际使用进行过优化,而是说明了算法。

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2015-07-14
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2017-02-10
    相关资源
    最近更新 更多