Guys here is how you calculate a registration point that is on the top
left corner.. to the center.. Aka: Object handles to Box2d or
something like that..
first you want to find the center of the rectangle..
use this getCenter function (below) and pass it your rectangle...which
you can get by knowing the x y width and height of your shape..
getCenter(new Rectangle(x,y,width,height))
Then use this rotation Matrix function to find out how much x and y
have changed since you rotated it... (you don't need to do this second
part if your rectangle didn't rotate... Make sure all the rotation
info is in radians.. so you pass x y width height and radians that
have rotated to rotationMatrix..
So implementing it would probably look something like this:
var cp:Point = getCenter(new Rectangle(x,y,width,height));
if(Number(rotation) != 0)
{
cp.x = rotationMatrix(x,y,width,height,Number(rotation)).x;
cp.y = rotationMatrix(x,y,width,height,Number(rotation)).y;
}
then you will have a properly translated registration point.
Heres the functions:
private function
rotationMatrix(x:Number,y:Number,w:Number,h:Number,rads:Number):Point
{
//rotation matrix formula;
//opposite direction because of flash
var startingPointX:Number = 0 - (w / 2);
var startingPointY:Number = 0 + (h / 2);
//the big guns.. The Rotation Matrix formula for opposite direction
var x1:Number = Math.cos(rads) * startingPointX + Math.sin(rads) *
startingPointY;
var y1:Number = -Math.sin(rads) * startingPointX + Math.cos(rads) *
startingPointY;
var endX:Number = x - x1;
var endY:Number = y + y1;
return new Point(endX, endY);
}
private function getCenter(rect:Rectangle):Point
{
var topleft:Point = rect.topLeft;
var bottomright:Point = rect.bottomRight;
//midpoint formula
var px:Number = (topleft.x + bottomright.x) / 2;
var py:Number = (topleft.y + bottomright.y) / 2;
return new Point(px,py);
}
get Center uses the midpoint formula.
Let me know how this works for you.
I used the opposite Rotation Matrix formula since flash's coordinate
system is different than most.. where rotation is clockwise instead of
counter clockwise. and 0,0 is in the top left corner.
On Jul 22, 11:26 am, Jacob Schatz <
playpianolikew...@gmail.com> wrote:
> I will figure out this calculation and then post it. So far I have it from
> all your really great posts. Steven I think your solution may work but it
> may be a little too verbose.
> What I have so far:
> Return the rectangle object. Get topleft and bottomright.
> Use the midpoint formula to get the midpoint. Now we have the center of the
> rectangle. Now only one more thing has to happen. We have to take into
> account the changes that happened to the topleft x&y while rotating. Will
> post the code once I figure it out completely.
> Best,
> Jacob.
> Sent from my iPhone
>
> On Jul 22, 2010, at 9:20 AM, Marc Hughes <
>
>
flexcompone...@rogue-development.com> wrote:
>
> I might be missing something, but I think localToGlobal method is all you
> need.
>
> If you have a display object that's 50x50 the center is 25,25 in local
> coordinates. Pass that into localToGlobal and you'll get the global coords.
>
> globalPoint = handle.localToGlobal( new Point(25,25) );
>
>
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/d...
> > On Wed, Jul 21, 2010 at 6:05 PM, Jake <
playpianolikew...@gmail.com> wrote:
>
> >> Hi all,
> >> Is there a way to get the real x,y location of the handles. something
> >> like:
> >> oh.topLeftHandle.x... If I could get the real x,y coordinates (not
> >> just local).. then I could get the center point of the Rectangle..
> >> So here is my important question:
> >> Is it possible to set the registration point to the center? Or is
> >> there an easy way to find that center point?
> >> I am thinking of all types of Trig.. but I can't seem to figure out
> >> that center point unless I have the x.y of those handles. I am trying
> >> to send this data over to box2d but of course that works with
> >> registration points in the center.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Flex ObjectHandles" group.
> >> To post to this group, send email to
object...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >>
objecthandle...@googlegroups.com<
objecthandles%2Bunsu...@googlegroups.com>
> >> .
> >
objecthandle...@googlegroups.com<
objecthandles%2Bunsu...@googlegroups.com>
> > .