Be a Supporter!

Dynamic center of rotation?

  • 704 Views
  • 7 Replies
New Topic Respond to this Topic
Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Dynamic center of rotation? 2009-03-24 07:51:54 Reply

is there any way (using as2) to create the center of rotation for a clip your spawning of the libary (the center of rotation that i want is at point 0,0)


AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 08:02:28 Reply

At 3/24/09 07:51 AM, Yambanshee wrote: is there any way (using as2) to create the center of rotation for a clip your spawning of the libary (the center of rotation that i want is at point 0,0)

No, don't think so. Probably the best simulation is to createEmptyMovieClip first, attach the library MC inside the created MC, adjust the _x and _y, then rotate the created MC rather than the attached one


- - Flash - Music - Images - -

BBS Signature
Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 08:11:31 Reply

At 3/24/09 08:02 AM, Denvish wrote: No, don't think so. Probably the best simulation is to createEmptyMovieClip first, attach the library MC inside the created MC, adjust the _x and _y, then rotate the created MC rather than the attached one

thanx, ill give it a try


AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 08:21:05 Reply

It is perfectly possible with AS: Here's a sample code with a clip on the stage called 'clip'

Note, that i am not directly manipulating or using the _x and _y of the clip as these values are rounded and will give innacurate (horribly innacurate) results:

Here, i am defining an axis to rotate about in the clip's coordinate system, in the test flash i had a square that was 100x100 with the origin at it's centre, so by defining ax and ay as 50, -50 i am rotating the clip about it's top right corner.

//coordinate in the clip's coordinate system to rotate about
var ax:Number = 50;
var ay:Number = -50;

var cx:Number = clip._x;
var cy:Number = clip._y;
var cr:Number = clip._rotation;

function onEnterFrame():Void
{
	var ang:Number = 1;
	var angr:Number = ang*Math.PI/180;
	
	var pangr:Number = cr*Math.PI/180;
	var px:Number = cx+ax*Math.cos(pangr)-ay*Math.sin(pangr);
	var py:Number = cy+ay*Math.cos(pangr)+ax*Math.sin(pangr);

	var dx:Number = cx-px;
	var dy:Number = cy-py;

	cx = px+dx*Math.cos(angr)-dy*Math.sin(angr);
	cy = py+dy*Math.cos(angr)+dx*Math.sin(angr);
	cr += ang;	

	clip._x = cx;
	clip._y = cy;
	clip._rotation = cr;
}

This is never going to be a bottleneck, but if it were you could use temporary variables and have the cos and sine of the angles calculated once and used twice, rather than having two calls to Math.sin and Math.cos for each angle


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 08:25:11 Reply

Also just realised alot of that code was redundant, since it's roating about the axis, the axis isn't going to move :P

Let the axis of rotation be (px,py) (no longer in clip's coordinate system) to rotate by an angle 'ang' around that point:

//coordinate in the clip's coordinate system to rotate about
var px:Number = clip._x + 50;
var py:Number = clip._y - 50;
var ang:Number = 1; //1 degree per frame

var angr:Number = ang*Math.PI/180;
var cx:Number = clip._x;
var cy:Number = clip._y;
var cr:Number = clip._rotation;

function onEnterFrame():Void
{
	var dx:Number = cx-px;
	var dy:Number = cy-py;

	cx = px+dx*Math.cos(angr)-dy*Math.sin(angr);
	cy = py+dy*Math.cos(angr)+dx*Math.sin(angr);
	cr += ang;	

	clip._x = cx;
	clip._y = cy;
	clip._rotation = cr;
}

using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 08:31:02 Reply

At 3/24/09 08:21 AM, dELtaluca wrote: It is perfectly possible with AS

I stand corrected. Smartarse ;)


- - Flash - Music - Images - -

BBS Signature
Cojones893
Cojones893
  • Member since: Mar. 9, 2003
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 10:15:10 Reply

It's also doable in AS3 with the rotateAroundExternalPoint or rotateAroundInternalPoint.

MatrixTransformer.rotateAroundExternalPoint(mat,X,Y, 10);

Where mat is your matrix for the object you want to rotate, X and Y form the point you want to rotate around and 10 is how many degrees to move.

Then you just have to reapply mat to the original matrix.

Rotate Around External Point

Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Dynamic center of rotation? 2009-03-24 10:50:49 Reply

At 3/24/09 08:25 AM, dELtaluca wrote:
Let the axis of rotation be (px,py) (no longer in clip's coordinate system) to rotate by an angle 'ang' around that point:

ughh, thanx :)
no idea what that is having very limited knoledge of math, as ive only just started learning trig in school...

I shall study and try understand!


AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature