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

## Recommended Posts

Alright, so I got two angles. One is the joystick's angle, and the other is the camera to player angle. The camera's angle. Now I want it so when I press up on the joystick it moves the player away from the camera. How would I do this? And is there a easy way to do it in Java or Ardor3d?

##### Share on other sites
Think of it in terms of vectors, not angles. A common problem with people just learning game mathematics is that they know too much trigonometry and not enough linear algebra.

Your camera has a forward-vector and a right-vector. Project both of those onto the ground plane and normalize, and you are left with camera-relative movement vectors for the horizontal and vertical joystick axes. No angles are directly involved.

##### Share on other sites
"Project both of those onto the ground plane and normalize"

Umm, how would I do that?

##### Share on other sites
To normalize a vector, just divide each of its components by its magnitude. To project a vector onto a plane, subtract the dot product of it and the plane normal, multiplied by the plane normal. (That is, V projected onto a plane with normal N is V-(V·N)N.) For the ground plane there's a more straightforward way to do it, but puzzling through the more general equation will be a good exercise for you.

##### Share on other sites
So... I can't wrap my head around this...

Is there a way I can convert it into a matrix and like...multiply them to make it work?

##### Share on other sites
Is your game world 3D? If it's not, then ignore the entire rest of this post.

You can get some vectors out of the camera's view matrix. Those are the 'Right', 'Up', 'Forward', and 'Translation' vectors. Some APIs don't have explicit ways to get them, but it's not that tricky. Say you have a DirectX style matrix like this:

| xx xy xz xw |    X row "Right" (or Left)| yx yy yz yw |    Y row "Up"| zx zy zz zw |    Z row "Forward"| wx wy wz ww |    Translate row (in some 3D transform matrices, this is on the last column instead)

If you treat (xx, xy, xz) as a vector, it makes the "X" vector (right/left).

What that means is that if you drew a line out from the camera in that direction, it would be to the "right" from the camera's perspective.

The same thing is true for the Y and Z rows - they represent the "up" and "forward" directions from the camera's viewpoint.

What I would do is:

- Get the camera view matrix' X row and make a vector out of it.

- The "Up" part of that vector (usually Y) should be nearly zero. If it's not, set it to zero.

- Normalize the vector by dividing it by its own length (length = sqrt(x*x+y*y+z*z)). (Dividing a vector by a number simply divides the X, Y and Z parts by the same number)

- This will give you a vector to the "right" which is aligned with the 'ground plane' (assuming the ground plane is a flat plane that extends in the X and Z axes and has a constant value in the Y axis)

- You can make the "forward" vector: (-right.Z, 0, right.X) NOTE: You could also "project the camera forward vector onto the ground plane", but if your camera has an adjustable tilt, this can cause either the 'forward' or 'up' vector to be nearly zero when you project it, so I prefer to construct this vector by manually making one perpendicular to the 'right' vector instead) The same problem may occur if your camera can roll, but that's pretty rare except for in flight simulators.

Now, just multiply your joystick X and Y in:

moveVector.X = Joystick.X * right
moveVector.Z = Joystick.Y * forward

You might have to swap signs depending on coordinate system and such.

1. 1
2. 2
Rutin
26
3. 3
4. 4
JoeJ
18
5. 5
gaxio
11

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631763
• Total Posts
3002197
×