• | **Help! 2D vector rotate.**
on Apr 22, 2009 at 10:22:34 pm |

Hello.

I'm writing an expression for a 2D vector rotate and it's not working. It's driving me nuts!

I know it's not the simplest way to rotate a point around another one, but this is part of a bigger expression problem I'm trying to solve, writing a lookat function that includes an 'up' vector.

I broke the problem down to this really simple thing, and now find that I'm getting a kind of Star Trek badge instead of a circle.

Here's a picture of the shape it describes...

...and here's the code.

function doRotate(p, a) { //rotates a point p, angle a around the origin [0,0]

x = p[0];

y = p[1];

x = x*Math.cos(a) - y*Math.sin(a);

y = x*Math.sin(a) + y*Math.cos(a);

return([x,y]);

}

myPos = [300,100]; // starting position

tCentre = [300,300]; //centre of rotation

myAngle = degreesToRadians(effect("angle")("Slider")); //picks up the slider value and converts to rads.

a = doRotate(myPos-tCentre, myAngle); // call rotate function.

a = a + tCentre; //adjust back to centre of rotation.

What am I doing wrong? I was sure the code was right.

Ben.

http://www.benrollason.com

• | **Re: Help! 2D vector rotate.** on Apr 22, 2009 at 11:09:31 pm |

I haven't tried it, but it looks like you might be changing too soon. Try your function this way:

function doRotate(p, a) { //rotates a point p, angle a around the origin [0,0]

x0 = p[0];

y0 = p[1];

x = x0*Math.cos(a) - y0*Math.sin(a);

y = x0*Math.sin(a) + y0*Math.cos(a);

return([x,y]);

}

Dan

• | **Re: Help! 2D vector rotate.** on Apr 23, 2009 at 9:07:31 am |

That was just the ticket.

Thanks for your help.

Ben.

http://www.benrollason.com

• | **Re: 2D vector rotate.** on Oct 17, 2018 at 11:00:54 am |

Hi Dan, thanks for all your support!

Do you know the most efficient way to rotate the point around a different origin eg. [10,100] not [0,0] ?

Also if I have a shape made of 4 random points, does all of this function need to be recalculated per point or can it be more efficient?

Really appreciate your support!

• | **Re: 2D vector rotate.** on Oct 17, 2018 at 4:19:37 pm |

Like this maybe:

myPoint = [200,200];

origin = [10,100];

angle = 30;

v = myPoint - origin;

a = degreesToRadians(angle);

c = Math.cos(a);

s = Math.sin(a);

vNew = [v[0]*c - v[1]*s,v[0]*s + v[1]*c];

origin + vNew

I don't understand the second part of your question.

Dan

• | **Re: 2D vector rotate.** on Oct 17, 2018 at 4:51:46 pm |

Hey thanks Dan that works!

To clarify the other part, if I have an array of points that create a shape :

[0,0],[200,0],[200,100],[0,100]

Is the only way of rotating the shape by using a for loop and applying the function to each point? Or is there a more efficient way to rotate an array of points?

Cheers!

• | **Re: 2D vector rotate.** on Oct 17, 2018 at 5:16:56 pm |

I'm guessing you'd have to rotate each point separately.

Dan

• | **Re: 2D vector rotate.** on Oct 17, 2018 at 6:00:42 pm |

Thanks Dan, got it working and now much faster when only calculating the Math.cos and Math.sin once!

Wish AE would allowed us to create plugins that can calculate and output data for pick whipping. Heavy expressions just become so slow.

AE 2019 is apparently 5-6 times faster at evaluating expressions, fingers crossed this will improve things.

Best wishes!