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 want to move the camera sideways (strafe). I had the following in mind, but it doesn't look like there are standard methods to achieve this in Libgdx.

If I want to move the camera sideways by x, I think I need to do the following:

  1. Create a Matrix4 mat
  2. Determine the orthogonal vector v between camera.direction and camera.up
  3. Translate mat by v*x
  4. Multiply camera.position by mat

Will this approach do what I think it does, and is it a good way to do it? And how can I do this in libgdx? I get "stuck" at step 2, as I have not found any standard method in Libgdx to calculate an orthogonal vector.

EDIT: I think I can use camera.direction.crs(camera.up) to find v. I'll try this approach tonight and see if it works.

EDIT2: I got it working and didn't need the matrix after all:

Vector3 right = camera.direction.cpy().crs(camera.up).nor();
camera.position.add(right.mul(x));
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

It'll do what you think it does. If you check the Camera class source code, you'll see that they're doing exactly what you suggest, plus they're normalizing the result:

93 final Vector3 right = new Vector3();
94
95 public void normalizeUp () {
96     right.set(direction).crs(up).nor();
97     (...)
98 }

The "standard" method of finding a vector orthogonal to two others is indeed to compute their cross product (in libgdx or anywhere).

There's an additional benefit of knowing this: if you're sure that normalizeUp() gets called in your case, you can just use camera.right directly, e.g.:

camera.position += camera.right * someFactor;
share|improve this answer
    
Thanks, i got it working, i came to the same conclusion a few moments ago, that i didn't really need the matrix if i had the right vector. I have to create my own right though, as it is private field. I've updated my question with the code i'm using now. –  Bubblewrap Apr 2 '12 at 17:19
1  
Have you thought about using TweenEngine to move cameras? It can let you do some really cool effects with the camera. aurelienribon.com/blog/2011/04/java-universal-tween-engine –  Rubber Mallet Apr 2 '12 at 17:22
add comment

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.