3

How can I make a camera that I can move around and rotate around its own axis in processing 2+?

I have a camera that I can move around in the world space and have some kind of rotation:

frustum(-10,10,-10,10,10,2000);
translate(camX,camY,camZ);//I move around by adding to these values when a button is pressed
rotate(angleX,1,0,0);//same here...
rotate(angleY,0,1,0);
rotate(angleZ,0,0,1);

Bu the problem with this is that the rotation is centered in the scene, meaning that I get very strange rotations when moving further away from the scene's center coordinates. Why does that happen when I have translated before rotating?

Patrick Dahlin
  • 286
  • 2
  • 5
  • 19
  • Are you trying to learn the movement and rotation principles in 3D or are you trying to implement it for something else? For implementation, take a look at PeasyCam for Processing. Also for the sake of learning, you can check out their source code. – Nico Apr 18 '14 at 21:41
  • Well, I would like to know the basics for 3d camera movement. Though as far as what I have seen on the web I do not completely understand it. – Patrick Dahlin Apr 19 '14 at 08:46
  • What I understand about translation and moving objects in 3d space is not working as expected in this example and I'd really like to know why so I can understand it better. – Patrick Dahlin Apr 19 '14 at 09:47
  • 1
    I'll be straight with you. Understanding rotation and translation in 3D is a b****. I recently created a 3D scene but unfortunately I can't share much from it or about it. However, this guy's videos and his code in C++ that he provides with them helped a lot: https://www.youtube.com/playlist?list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My – Nico Apr 19 '14 at 23:15

1 Answers1

3

Thanks to Nicolás Carlo and his suggestion to watch This Youtube playlist made by Jorge Rodriguez I was able to fix what I almost made right the first time.

All I had to do was really just to do some simple trigonometric calculations to get the forward-vector from the angles I got, and then just add the camera position to that for the centerX,centerY,centerZ values in camera();

Ex. where camPos is the current position 3D-Vector and vecForward is the calculated 3D-forward vector that I needed.

vecForward.x = cos(yaw)*cos(pitch);
vecForward.y = sin(pitch);
vecForward.z = sin(yaw)*cos(pitch);

camera(camPos.x,camPos.y,camPos.z, camPos.x+vecForward.x,camPos.y+vecForward.y,camPos.z+vecForward.z, 0,1,0);

If some of you do not know, Pitch is the up-down angle and Yaw is left-right angle of the camera.

And as a last note here,I highly suggest watching Rodriguez "Math for game developers" that Carlo suggested since every video explains at least one of the most important/oftenly used mathematical solutions at a time to different problems and then giving an example in the end.

Patrick Dahlin
  • 286
  • 2
  • 5
  • 19