开发者

Problem with rotating object

I've been having this problem for a while, and it would be great if someone can help. :)

http://img31.imageshack.us/i/problemsc.jpg/ (illustration of the problem)

So, I have this object with registration point at middle bottom. I want this object to follow the mouse, but to remain on the center of the screen while rotating. Here is what I used :

dx = mouseX - this.x ;
dy = mouseY - this.y ;

radians = Math.atan2(dy, dx);
angle= radians * 180/Math.PI;

this.rotation = angle + 90;
this.y = mouseY + this.height; 

The problem with this code is that when you move the mouse away from the center,e.g. the angle is not 90 degrees, there is an offset between the 开发者_运维问答mouse cursor and the object. By tracing, I found out that it follows the y position of the mouse as it should, but by rotating the object, it's height and width is changing, so the object is not pointing right on the cursor.

Is there a way to compensate for this offset? Or maybe another approach? Thanks in advance.

UPDATE: Ok, it seems that I didn't explain the problem right, sorry, English is not my native language. I need the object to follow the cursor, but the front end of the object to be on the same position as the cursor and the back end of the object to be in the middle of the screen, just on the center x axis. So when you move the mouse, front end of the object will always pointing at and will be on the mouse position. Back end should stay on same position, but rotated.


The following works for me (it's pretty much identical to yours)

import flash.events.MouseEvent;

const RAD_TO_DEG:Number = 180/Math.PI;
const OFFSET:Number = 90;

var angle:Number;
var radians:Number;

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveIt, false, 0, true);

function moveIt(e:MouseEvent):void {
    radians = Math.atan2(mouseY - mc.y, mouseX - mc.x);
    angle = radians * RAD_TO_DEG;

    mc.rotation = angle + OFFSET;
}

Just ensure you have the registration point set correctly (see image)

Problem with rotating object

I have also uploaded a SWF

http://megaswf.com/serve/1054550


You don't need to move the object, if you have the correct registration point. You should just rotate it according to the center of the stage. Use this:

this.rotation = (Math.atan2(stage.mouseY - stage.stageHeight/2, stage.mouseX - stage.stageWidth/2) / Math.PI) * 180

You can download a sample fla from here: http://rapidshare.com/files/457051556/rotation_mouse.fla

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜