# Vector Math help

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Sorry to be an annoying git, but I freakin suck at math, I've been struggling with this simple vector problem for about an hour now, so I just said fuck it and decided to post begging for help. Basically, I have 2 2d vectors. Each is described by an x and y coordinate. Let's call them Vec1 and Vec2. I have to extract a component vector from Vec2 that is perpendicular to Vec1. Here is a picture to illustrate: Sounds pretty simple but I can't seem to pull it off. Can somebody explain in really simple terms how to do this? It would be even better if you could write it in pseudocode.

##### Share on other sites
blue = black + pink
pink = blue - black

my lame guess. Could be wrong.

##### Share on other sites
ORTHOGONAL(black,blue)  Project blue onto black  real factor = (dot blue black) / |black|   vector project = factor black   Substract the projection  return blue - project

##### Share on other sites
Quote:
 Original post by RivieraKidblue = black + pinkpink = blue - blackmy lame guess. Could be wrong.

Unfortunately, this doesn't work for all vectors of varying magnitude that point in the same direction. It would work if the |v1|2 - |v2|2 = |v3|2, but we don't know if that is the case.

ToohrVyk has it right.

##### Share on other sites
Tossing this over to the Math & Physics forum.

##### Share on other sites
Quote:
 Original post by ToohrVykORTHOGONAL(black,blue) Project blue onto black real factor = (dot blue black) / |black| vector project = factor black Substract the projection return blue - project

Thanks. I translated your code into a c++ function. I'm not sure that it's done right though.

void Ortho(double vec1x,double vec1y,double vec2x,double vec2y) {double factor = (vec1x*vec2x+vec1y*vec2y)/sqrt(vec1x*vec1x+vec1y*vec1y);double projx=factor*vec1x;double projy=factor*vec1y;resultx=vec2x-projx;resulty=vec2y-projy;}

##### Share on other sites
Everyone sucks at math, but be patient and work with her :)

##### Share on other sites
I think I identified the problem. I just had to take out the sqrt sign and the thing gives the right output.

void Ortho(double vec1x,double vec1y,double vec2x,double vec2y) {double factor = (vec1x*vec2x+vec1y*vec2y)/sqrt(vec1x*vec1x+vec1y*vec1y);double projx=factor*vec1x;double projy=factor*vec1y;resultx=vec2x-projx;resulty=vec2y-projy;}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!