BIT-101 [2003-2017]

Matrix3D bug? or me confused?


I assume the latter…

Anyway, trying to wrap my head around AS3’s Matrix3d class. In particular, the pointAt method. This supposedly, “Rotates the display object so that it faces a specified position.”

Here’s the class I’m using for testing:

[as]package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Vector3D;

[SWF(width=800, height=800)]
public class MatrixStuff extends Sprite
{
[Embed (source=“Compass.jpg”)]
private var Compass:Class;

private var sprite:Sprite;

public function MatrixStuff()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

var compass:Bitmap = new Compass();
compass.x = -compass.width / 2;
compass.y = -compass.height / 2;
sprite = new Sprite();
sprite.addChild(compass);
sprite.x = 400;
sprite.y = 400;
sprite.z = 100;
addChild(sprite);

addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):void
{
sprite.transform.matrix3D.pointAt(new Vector3D(mouseX, mouseY, 0));
}
}
}[/as]

This should make the image “face” the mouse. Here’s the result:

[kml_flashembed publishmethod=“static” fversion=“8.0.0″ movie=“https://www.bit-101.com/2003/wp-content/uploads/2009/08/MatrixStuff.swf” width=“800″ height=“800″ targetclass=“flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

As you can see, the bottom of the object is pointing at the mouse, rather than the front of the object. Per the documentation, this is controlled by the “at” parameter:

at:Vector3D (default = null) — The object-relative vector that defines where the display object is pointing. Object-relative defines the object’s transformation relative to the object space, the object’s own frame of reference and coordinate system. Default value is the -z axis (0,0,-1).

But this looks like it’s using a (0, 1, 0) vector. In fact, we can test this by passing in such a vector to the second parameter:

[as]sprite.transform.matrix3D.pointAt(new Vector3D(mouseX, mouseY, 0), new Vector3D(0, 1, 0));[/as]

Lo and behold, exact same behavior. Also, notice that it’s flipped on the y-axis. Not sure about that.

In fact, by using (0, -1, 0) I can get the “north” point to point at the mouse, and by using (1, 0, 0) and (-1, 0, 0), I can get the east and west points to point at the mouse. So far so good. Now, the documentation says the default is (0, 0, -1), so I try that, and the whole thing breaks. Doesn’t rotate at all. Same with (0, 0, 1).

Anyone else able to see this behavior? Anything I’m doing obviously wrong?

« Previous Post
Next Post »