Find Right Angle to a Vector
BlitzMax Forums/BlitzMax Programming/Find Right Angle to a Vector
| ||
Hiya, so if I have a vector like [dx,dy] and I want to work out the vector which is at a right angle to it (or a left angle), basically 90 degrees left or right, is there a quick way to work this out as at the moment all I can think of is: Use pythagoras to get the radius from origin. Work out angle of line from 0 degress. Subtract or add 90 degress to the angle. Use standard dx = r*cos(angle) and dy=r*sin(angle). Just seems a bit long-winded. I'm pretty sure there must be a better options using quadrants or something. Any ideas? Thanks. |
| ||
rotate through any degree:[dx,dy] x [ cos a, -sin a] [ sin a, cos a] plug 90` into the rotation matrix and you get the rotation matrix [ 0 , -1 ] [ 1, 0 ] and simplify the result: [dy,-dx] |
| ||
You could have figured that out by drawing a line, and then turning the paper 90 degrees. |
| ||
You could have figured that out by drawing a line, and then turning the paper 90 degrees. Ghetto coder! |
| ||
Ghetto coder! Thats a good thing, right? |
| ||
skidracer: Thanks but regretably I've forgotton how to matrix transformations in the last ooh 15 years or so (since I last programmed 3D lol). So what am I supposed to do with the [dy,-dx] result? Is my original vector a matrix like this [dx,dy]? and am I supposed add the matrices or multiply them? Hmm, let's work this out. Say dx = 0 and dy = 1 (it should point down/South on the screen (or up on a graph)), 90 degrees clockwise would be pointing left/West. So dx will end up being -1 and dy will end up being 0. Hmm, I don't get how that links to the [dy,-dx] matrix. Help please experts! |
| ||
oh OK I think I get it. newdx = -dy and newdy=dx for clockwise, and newdx = dy and newdy = -dx for anti-clockwise. thanks. Teach me to work this out at 1am with the flu. |
| ||
Pen. Paper. Draw. Turn. HomeBoy <----- Ghetto Talk Teach me to work this out at 1am with the flu You are working yourself into the ground Grey |
| ||
The simple way to get a perpendicular vector is to remember that the dot product must be zero. Starting with vector (x,y) you just swap the two numbers and make one of them negative. One possible result is (y,-x). The dot product with (x,y) is x*y + y*(-x) = x*y - x*y = 0. So (y,-x) is perpendicular to (x,y). Of course, (-y,x) works as well. Which is a right turn and which is left depends on the coordinate system used. Just try them both and see what they do. |
| ||
Thanks Floyd. It's just been a while since I did all this stuff but it's coming back to me. I seem to be using quite a bit of maths for this project, but it's pleasing to work it all out and see a result. |
| ||
Haha dude, you must have really been out of it :) *prod* Hope you'fe feeling better now mate! :) I had a problem a week or so back- I needed to create a TFormPoint function in 2D but I just couldn't figure it out despite knowing it wasn't difficult. I got it working- but I actually don't know why its working right now. I should post it in the archives. :) |
| ||
yeah. Well I don't do 3D you see - not since I had an C64 and Amiga, well actually I made a 3D screen saver in Delphi in about 1996. But yeah I was feeling pretty crappy and the wiki pages full of formulas seemed liked weird creatures crawing over the page that I couldn't comprehend... Anyway it works fine now. I was making particles fly off a pinwheel in the direction of the spin. |
| ||
You are working yourself into the ground Grey Actually I hope you're not right. Hope you've not been doing the 3am bedtime thing jake- remember when I started talking to myself? |
| ||
lol. It's OK. It was more a case of watch some TV (yes I've got a TV license again for the first time in 18 months, but it was to stop the kids fighting when their dinner is being cooked), then just before bed, double check the forum post I made earlier (you've all done that right?) and then go off on some crazy wiki research about the topic. That's all ;-) |