Rotation Around Center

Did you set flatReservedTxt.rotation = 45?

@player_03 sure I did

this is my code

flatReservedTxt.x = (575 - flatReservedTxt.width) / 2;
flatReservedTxt.y = (415 - flatReservedTxt.height) / 2;
			
var oldX = flatReservedTxt.x; 
var oldY = flatReservedTxt.y;
			
//
flatReservedTxt.rotation = 45;
var hypotenuse:Float = Math.sqrt(flatReservedTxt.width / 2 * flatReservedTxt.width / 2 + flatReservedTxt.height / 2 * flatReservedTxt.height / 2);
			
var newX = hypotenuse * Math.cos((flatReservedTxt.rotation + 45) * Math.PI / 180);
var newY = hypotenuse * Math.sin((flatReservedTxt.rotation + 45) * Math.PI / 180);
flatReservedTxt.x += oldX - newX;
flatReservedTxt.y += oldY - newY;

oldX = newX;
oldY = newY; 

what I am missing?

thanks

For one thing, shouldn’t that line be at the end? Either that or you shouldn’t add 45 inside the sin() and cos() functions.

If that isn’t enough, please post screenshots of what’s wrong.

@player_03 this worked for me

private function rotateDysplayObj(image:DisplayObject, degrees:Float):Void {
		
		var radians:Float = degrees * (Math.PI / 180.0);
		var offsetWidth:Float = image.width/2.0;
		var offsetHeight:Float =  image.height/2.0;

		// Perform rotation
		var matrix:Matrix = new Matrix();
		
		matrix.translate(-offsetWidth, -offsetHeight);
		matrix.rotate(radians);
		matrix.translate(offsetWidth, offsetHeight);
		matrix.concat(image.transform.matrix);
		image.transform.matrix = matrix;
} 

but can’t figure out why not worked sin & cos solution ((((
can you paste complete working example?
Thanks

Now that I look at it, that’s honestly a much better formula. The one Mallario posted doesn’t work unless you store the value of hypotenuse.

Here’s another option. It can re-calculate the value of hypotenuse, but instead you have to store the pivot point:

private function rotateObjectAround(target:DisplayObject, rotation:Float, pivotX:Float, pivotY:Float):Void {
	var hypotenuse:Float = Math.sqrt((target.x - pivotX) * (target.x - pivotX) + (target.y - pivotY) * (target.y - pivotY));
	target.x = pivotX - hypotenuse * Math.cos(rotation * Math.PI / 180);
	target.y = pivotY - hypotenuse * Math.sin(rotation * Math.PI / 180);
	
	target.rotation = rotation;
}
1 Like