Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411582 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 06, 2024, 02:14:55 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Using a matrix to position a rotated-off-center sprite
Pages: [1]
Print
Author Topic: Using a matrix to position a rotated-off-center sprite  (Read 1046 times)
st33d
Guest
« on: July 11, 2010, 03:20:24 PM »

I have a sprite whose handle is at the top left, it holds a rectangular bitmap. However, in Box2D, objects are held at their center. I cannot center the bitmap in the sprite and I cannot attach the sprite to another sprite and center that sprite - I'm chucking around loads of these, a lazy solution is not an option.

I'm guessing that what I need to do is to give a matrix transform to the sprite that will push the handle of it out of the center at a rotated angle.

What the hell do I feed into the matrix class to do this?
Logged
compositeredfox
Level 0
***



View Profile WWW
« Reply #1 on: July 11, 2010, 04:11:09 PM »

You have to create the matrix, translate it to where your sprite is, substract it the pivot's position (where you want the "hinge" to be), rotate the matrix to your wanted angle (in radians), add the pivot's position. Then apply it to your sprite.

Code:
var m:Matrix = new Matrix();
m.translate( spritePosition.x, spritePosition.y ); // Not the actual x and y of the sprite you're applying the matrix to!
m.translate( -pivot.x, -pivot.y ); // x and y of the pivot sprite
m.rotate(whateverRotation); // in Radians!
m.translate( pivot.x, pivot.y);
sprite.transform.matrix = m;

Here's a FLA with an example. The star has its center in its top left, and you can move the pivot and it will rotate correctly around that:
http://dl.dropbox.com/u/64314/forums/pivotedRotation/pivoted_rotation.fla

Here's the SWF:
http://dl.dropbox.com/u/64314/forums/pivotedRotation/pivoted_rotation.swf


Although if you're working with box2d maybe it's better to always center your sprites. Why can't you do that?
Logged
st33d
Guest
« Reply #2 on: July 12, 2010, 01:41:44 AM »

I'm rendering the sprites with a combination of copyPixels when they're static and then rendering them as rotated Sprites when they are dynamic.

Thanks for this. It took me a while to realise that pivot was the real-world pivot point and not point on the sprite I was rotating.

There's also a MatrixTransformer class I've found about that does a similar task:

http://jobemakar.blogspot.com/2007/06/rotating-around-point.html

But I think I'm doubtful that it would offer any more optimisation over the code I've got already.


Code:
var pos:b2Vec2 = body.GetPosition();
var angle:Number = body.GetAngle();
matrix.identity();
matrix.tx = -width * 0.5 * Game.SCALE;
matrix.ty = -height * 0.5 * Game.SCALE;
matrix.rotate(angle);
matrix.tx += pos.x * Game.SCALE;
matrix.ty += pos.y * Game.SCALE;
mc.transform.matrix = matrix;
Logged
davidp
Level 6
*



View Profile WWW
« Reply #3 on: July 13, 2010, 05:16:28 AM »

i've been having the same problem lately - this helps a lot!

thanks guys Smiley
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic