Rotating a 2D vector around an arbitrary point.

Hope this is the best place for this question, sorry if its not. I'm having trouble figuring out how to rotate a vector around an arbitrary point.

The below snippet rotates the 2dvector whos points are ax and ay around the origin(0x,0y), but say I wanted this vector to rotate around a new origin(10x, 40y), how would I go about this?

ax' = ax*Math.cos(theta) - ay*Math.sin(theta) ;

ay' = ay*Math.cos(theta) + ax*Math.sin(theta) ;

[459 byte] By [mkel25a] at [2007-9-28 5:42:24]
# 1
Translate your affine space so that the centre of rotation was at the origin. Rotate. Invert the original translation.
YATArchivista at 2007-7-9 16:53:18 > top of Java-index,Other Topics,Java Game Development...
# 2

> Translate your affine space so that the centre of

> rotation was at the origin. Rotate. Invert the

> original translation.

I'm only just starting out with vectors so please go easy :)

I think what your describing relates to something I've read but putting it into practice has been a bit of a bummer. I read that you must subtract the new origin from the point, then perform the rotation, then add back the new origin? (I wouldn't be surprised if this information describes something else) Trying to run before I can walk has always been a forte of mine, I've only been reading about vectors for a day or two and my maths knowledge is shoddy to say the least. Can't say I'm looking forward to the endless chapters on matrices.

mkel25a at 2007-7-9 16:53:18 > top of Java-index,Other Topics,Java Game Development...
# 3

> bummer. I read that you must subtract the new origin

> from the point, then perform the rotation, then add

> back the new origin? (I wouldn't be surprised if this

Yeah, this is what the previous poster is refering to. If you don't do this, then what you get is your object rotating around the origin - which is great for doing a solar system simulation, but not useful for Asteroids.

Hope this helps, Cheers!

deathya at 2007-7-9 16:53:18 > top of Java-index,Other Topics,Java Game Development...
# 4

I found this code snippet below that apparently does what I was talking about in my above post, but I needed to add some logic to the process to get it to work how I wanted.

//ox,oy = origin

ax' = ax*Math.cos(theta) - ay*Math.sin(theta) + ox*(1-Math.cos(theta)) + oy*Math.sin(theta);

ay' = ax*Math.sin(theta) + ay*Math.cos(theta) + oy*(1-Math.cos(theta)) - ox*Math.sin(theta);

Say for example every time I clicked in an applet the mouse down x and y represented the new origin, I had to go through this to get the desired results:

public boolean mouseDown(Event evt, int x, int y)

{

ax = 50; //redefine the original points

ay = 0;

ox = x ; //define the new origin

oy = y;

if(ax>ox)ax = ox+ax; else ax = ox-ax;

if(ay>oy)ay = oy+ay; else ay = oy-ay;

if(bx>ox)bx = ox+bx; else bx = ox-bx;

if(by>oy)by = oy+by; else by = oy-by;

{

Then after the mouse down I could use the above rotation code to rotate the point around the new origin, without the logic the points lost their value.

So the question is what am I doing wrong here? I thought the above rotation code would be fine for this task on its own, also if I draw a line from the new origin to the point (-ax,-ay) rather than getting a mirror of the original point the new point still rotates around (0,0)? BTW, I'm not using the point class, just doubles to represent the x and y and drawLine() to err, draw the lines.

I've obviously misunderstood something here, but what? Also were can I get hold of a pointy hat with a big D on it.

Thanks in advance.

mkel25a at 2007-7-9 16:53:18 > top of Java-index,Other Topics,Java Game Development...
# 5

Sorry, but it's easiest to use matrices to do this, and a concept called "homogenous vectors". The translation of (ox, oy) to the origin, rotation anti-clockwise by theta, and inverse translation, applied to (ax, ay) in homogenous co-ordinates, comes down to the following equation: [ax' w][1 0 ox] [c -s 0] [1 0 -ox] [ax]

[ay' w] = [0 1 oy] [sc 0] [0 1 -oy] [ay]

[ w ][0 01] [00 1] [0 01] [ 1]where c = cos(theta) and s = sin(theta). Going one matrix multiplication at a time, we get [ax' w][1 0 ox] [c -s 0] [ax - ox]

[ay' w] = [0 1 oy] [sc 0] [ay - oy]

[ w ][0 01] [00 1] [1][ax' w][1 0 ox] [c(ax - ox) - s(ay - oy)]

[ay' w] = [0 1 oy] [s(ax - ox) + c(ay - oy)]

[ w ][0 01] [1][ax' w][c(ax - ox) - s(ay - oy) + ox]

[ay' w] = [s(ax - ox) + c(ay - oy) + oy]

[ w ][ 1 ]Thus we conclude that ax' = c(ax - ox) - s(ay - oy) + ox

ay' = s(ax - ox) + c(ay - oy) + oyThis agrees with the equations you give. However, it shows you the intermediate steps. I suggest breaking your calculation down into those steps and looking at the intermediate values to make sure they make sense.

YATArchivista at 2007-7-9 16:53:18 > top of Java-index,Other Topics,Java Game Development...
# 6

Thanks, I think the rotation code in my last post uses the homogenous point during the 齩x*(1-Math.cos(theta))?stage, the 1 being the homogenous point/vector (divide the real points by the homogenous points, right?). What you have posted sort of makes sense, I think I just need to spend more time reading up on vectors and matrices. It's like when I started to read about java, the whole thing seemed alien and unruly but it soon kicks in. The only reason I齰e came back to java programming as a hobby is to hopefully understand vectors and matrices, they look like a lot of fun once mastered. Your suggestion of breaking down the calculations and looking at them makes a lot of sense.

I think the main problem I've had with understanding matrices is to do with how they are defined (some in rows some in columns, just to make it more awkward), I think I'm getting there, I just need a slap on the head from time to time.

Thanks.

mkel25a at 2007-7-9 16:53:18 > top of Java-index,Other Topics,Java Game Development...