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.

I have a vector (8,5). It's origin is at (10,9)

I can calculate a perpendicular like so:

MyPerpendicularVector.x = -MyVector.y
MyPerpendicularVector.y = MyVector.x

Given that I now know these two axis, how do I make these axis (made up of my new vectors) now my "local" axis? for example I want to plot a vector (2,3) on the "local" axis but I have no idea how to calculate it in relation to the "global" axis

Image of the problem here:

http://i.stack.imgur.com/GNISp.jpg

share|improve this question

2 Answers 2

I'll start by saying that your vector cannot technically have an 'origin.' A vector is only a representation of a direction.

It's a bit unclear which coordinate system your vector (2,3) is defined in. I'm going to assume from your image that it is defined in your local coordinate system and you're trying to rotate it into your global coordinate system.

For a vector, all that you have to do is rotate it by the angle difference between your local and global coordinate systems. You can calculate this by taking the inverse cosine of the dot product of your two x-axes:

angle = cos^-1((2,3) * (1,0))

Then to rotate the vector you would use a simple 2D rotation matrix, which involves sines and cosines:

rotatedv = (x*cos(angle) - y*sin(angle), x*sin(angle) + y*cos(angle))

And this will be your vector in your global coordinates.

I should also mention that if your local coordinate system is centered at a different point than your global coordinate system (as it seems to be in your image), then you will need to take that translation into account only for points, not vectors.

share|improve this answer
    
cos^-1 only has a range from 0 to Pi. You want Atan2/atanfull –  NeuroFuzzy Feb 11 '12 at 2:10
    
The sign of the angle isn't necessarily needed. –  ktodisco Feb 11 '12 at 2:12

What ktodisco says is right, but the way I'd do it would be with matrices. Essentially, your coordinate system is expressed as xI+yJ+K. In the case of the origin/standard system, that represents the matrix equation:

http://mathurl.com/7hoegjb

likewise, for your transformed coordinate system with origin (x0,y0) and direction (a,b), the matrix would correspond to:

http://mathurl.com/6n3vevw

So to convert between systems, set both of the matrices equal to each other. All you need to calculate is A^(-1)*Vector and you're done.

The same thing can be done in three dimensions.

Also note that with this way, each unit in the alternate coordinate system would be the length of (8,3) in the normal coordinate system. To have a point at (2,3) in the alternate system be where you drew it, you'd have to normalize the vector first. Then the transform matrix.

(I'd also like to point out that in ktodisco's post, you would want the atan2 or atanfull function instead of the inverse cosine function.)

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.