views:

2439

answers:

6

Hi,

I am writing and opengl based iphone app and would like to allow a user to translate around a view based on the direction that they move two fingers on the screen. For one finger I know I could just calculate the vector from the start position to the current position of the users finger and then find the unit vector of this to get just the direction, but I don't know how I would do this for two fingers, I don't think adding the components of the vectors and calculating the average would work so I'm pretty much stuck...

thanks in advance

+1  A: 

"I don't think adding the components of the vectors and calculating the average would work" -- why not? (I don't see why it wouldn't)

Chad Birch
+7  A: 

Vector math works just like you think:

v3 = (v1 + v2)/2

// so:
v3.x = (v1.x + v2.x) / 2;
// same for Y and Z
Squeegy
+2  A: 

adding them and dividing by two does work

tliff
+2  A: 

Easy thought experiment: Do it for unit vectors in x and y direction. Intuitively you can picture that the "average" would be a unit vector at a 45 degree angle up and to the right. That's exactly what happens. The thought experiment suggests that you need to normalize the average by its magnitude to get a unit vector. I'd advise that you do so.

duffymo
+3  A: 

If you are only interested in the direction, You should add them and normalize the result vector,

(v1 + v2)/abs(v1 + v2)
TokenMacGuy
Division by zero!
Gareth Rees
+1  A: 

Usually it is better to multiply then divide for speed reasons when doing graphics programming so I would recommend this:

v3 = (v1 + v2) * 0.5f;