Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

To achieve something similar to the image below using HTML5 canvas one can simply write the following:

var TO_RADIANS = 0.0174532925;
context.translate(30, 20);
context.rotate(30 * TO_RADIANS);
context.drawImage(img, 0, 0);

enter image description here

My problem comes when I want to convert the coordinates from inside the translated/rotated context back to the main context. For example, if a missile were shot from 0,0 inside the translated/rotated context along the X axis, what formula would you use to then convert its position to the outer context?

share|improve this question

2 Answers 2

You basically found out about transforms and spaces in 2D!

If you enter this "new missile space" via translation followed by a rotation then you have to do the same thing in reverse, multiplied by -1.

missile.rotate(-30 * TO_RADIANS);
missile.translate(-30, -20);

Now your missile is in "world space".

share|improve this answer
    
Sadly I don't have access to the rotate/translate functions for where I need to calculate the position. Those functions are only available when drawing the graphics, in this scenario I am purely obtaining the position. Ideally I need to do what the rotate/translation function would do but in JavaScript –  jskidd3 Jul 8 at 23:14
    
Do you have access to the coordinates of the "missile space" inside the world space? –  Brotcrunsher Jul 8 at 23:22
    
I only have access to the coordinates displayed in the image. There is the main context which is the outer one, and then the rotated context inside of that. Both have an x, y axis. –  jskidd3 Jul 8 at 23:25
    
Okay and do you know by how many degrees you have to rotate? –  Brotcrunsher Jul 8 at 23:32
    
For this example we can just say 30 degrees like the image :-) –  jskidd3 Jul 8 at 23:33

Note the following important identity for augmented isometric affine matrices:

enter image description here

where the isometric condition is recognized from: enter image description here and

enter image description here

is the translation in world coordinates of your original transformation.

In vector terms, if you think of your original transformation as being

enter image description here

then the inverse transformation, to return to World Coordinates is simply

enter image description here
where because of isometry the inverse of R is simply the transpose of R, as noted above.

In vector form the initial identity is just

enter image description here enter image description here enter image description here = enter image description here

which is readily proved by multiplying out to get first

enter image description here enter image description here = enter image description here

and then reducing the left hand side as

enter image description here == enter image description here == enter image description here

Q.E.D.

Note also that the vector form of the identity is not specific to only a 2D space, but applies to any number of spatial dimensions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.