ScratchData LogoScratchData
Back to DadOfMrLog's profile

3D Engine -part 3 (moving the camera)

DADadOfMrLog•Created March 30, 2013
3D Engine -part 3 (moving the camera)
215
160
6283 views
View on Scratch

Instructions

Here is part 3 of the 3D Engine - rotating and moving the camera. ******************************************** More steps for the 3D engine in this studio: http://scratch.mit.edu/studios/202972 ******************************************** Use arrow keys and WASD to move through the starfield. You can change the number of stars (in hundreds) using the slider. FPS (frames per second) is shown at the bottom left. The stars are randomly placed in a cube about your starting point, and they don't regenerate if you move out of that cube - so you can end up with blackness if you travel too far! Press space to reset the camera to its original position. There's no distance ordering, so background stars can be rendered on top of foreground stars. I've gone for greater efficiency in the way this works, and it has paid off - it's considerably better than the way InterXeptor 3D does it, so I'll have to change that at some point... :) I get about 12fps with 1000 stars on our old 32 bit Core Duo Mac Mini using both Safari and Firefox (OSX10.6), and about 3fps with 5000 stars - so I'm expecting many of you out there can do a lot better.

Description

This is where we start to run into some slightly more complicated maths because we have to find a way to go from the X,Y,Z co-ordinates for the stars to the co-ordinates from the camera's point of view (where the Z axis is straight ahead, and X & Y axes are in the same direction as the screen's X & Y). We need a star's position in this camera-PoV co-ordinate system so we can do the 3D-to-2D projection as used in the previous project (part 2 of the 3D engine). However, I've tried to come at it from an angle that will hopefully be a bit easier to visualise, rather than the way it's written out on, for example, various wikipedia pages... :) The essential concept used here is that we have an original set of axes, in which the stars' co-ordinates are known, and as the camera angle changes we actually rotate these original axes relative to the camera. We now know the direction that our original axes are pointing from the camera's point of view. We have a star's position in the original co-ordinate system: let's call it XPOS,YPOS,ZPOS. So we can find where it is from the camera's viewpoint just by moving XPOS units along the direction the original X axis is now pointing (from the camera's P.o.V.), YPOS units along the direction of the original Y axis, and ZPOS units along the original Z axis. There's a nice extra twist that comes from using this method, because it also gives us the direction the camera is pointing in the original co-ordinate system - it turns out that it's just the Z components of the directions the original axes are now pointing from the camera's P.o.V. This means we can also easily work out how to move the camera forward/backward through the starfield. :) [Understanding why that's the case requires a bit of knowledge of how rotation matrices work, but I've outlined it in one of the comments in the project.]

Project Details

Project ID10096235
CreatedMarch 30, 2013
Last ModifiedJuly 24, 2013
SharedMarch 30, 2013
Visibilityvisible
CommentsAllowed