Saturday, May 12, 2012

[Action Script] Rotate matrix by mouse position May,2012

Hey guys I have been banging my head against this problem for the past few hours. I have a rectangle object created, I want the rectangle's rotation to follow my mouse cursor. ( I am easily able to do this). Where things get hard is that I want the rectangle to rotate around it's bottom right corner, to do this I've found that I need to use a matrix transformation. At this point I am able to have my rectangle rotate around the bottom right point, but I cannot figure out how to have it follow my mouse cursor as well while rotating. The reason being that with a matrix you can only tell it a amount of radians to rotate (matrix.rotate) rather than just being able to set its current rotation.... Here is my code

ActionScript Code: var point:Point=new Point(myRectangle.x+myRectangle.width/2, myRectangle.y+myRectangle.height/2); function rotateObject( event:Event ):void{          var m:Matrix=myRectangle.transform.matrix;                              m.tx -= point.x;                              m.ty -= point.y;                              m.rotate (45*(Math.PI/180));                              m.tx += point.x;                              m.ty += point.y;                              myRectangle.transform.matrix=m; } addEventListener( Event.ENTER_FRAME, rotateObject);
So with the current code it is constantly rotating around the bottom right corner, I have tried changing the "45" to "Math.atan(mouseX/mouseY), but again it seems that would only work if i could set the rotation of the matrix to that rather than telling it how much to rotate by....

Any thoughts or ideas would be much appreciated, thanks!
Rotate matrix by mouse position

Related Post



0 comments: