A few years back I started experimenting with 3d in canvas. Everything went pretty well untill I had to to turn the camera, I couldn't get it done.
A few days back I just happened to stumble upon that project again and noticed I got pretty far with it, sure, the code was a mess... so I cleaned it up a little and started fixing the camera rotation... And... I still have no clue how to do it. I tried google but so far nothing worked, I just have no idea what I'm doing....
Can somebody help me with this?
The code is located here: http://johandam.com/etc/3d
The relevant pieces are (probably) located here:
http://johandam.com/etc/3d/js/obj.js < General 'entity' class, here the 3d world coordinates are translated to 2d screen coordinates, it used to adjust it's position based on the camera's rotation but then distance-checking got all messed up so I moved it to the camera class, which I think makes more sense anyway.
http://johandam.com/etc/3d/js/camera.js < the camera object. A lot of movement code and at the end I try to adjust the camera based on it's rotation.
The idea is to have a target point around which the camera rotates but I'm pretty sure that somewhere a long the line I mixed a couple of different methods in it and I have no idea what I'm doing...
Who can help me out here?
Create*
family of methods from this list to get an idea of some of the helper methods that you could create. In your case you only really need theCreateTranslation
andCreateRotation*
methods, but a few of the others would also be useful, such as theCreateLookAt
method to help move your camera around. – David Gouveia May 14 '12 at 13:02world
andview
are the conventional names for those matrices. You store aworldMatrix
in each object and aviewMatrix
in the camera. Then when starting to render each object, you first create aworldViewMatrix
by combining the object'sworldMatrix
with the camera'sviewMatrix
. Finally, you take theworldViewMatrix
and multiply it by every vertex in the object. The result of this multiplication is a new, transformed vertex, which you can then project into 2D and add to the rendering list. – David Gouveia May 14 '12 at 13:18