ScratchData LogoScratchData
Back to DadOfMrLog's profile

3D Engine -part 2 (projection to 2D)

DADadOfMrLog•Created March 29, 2013
3D Engine -part 2 (projection to 2D)
89
54
4813 views
View on Scratch

Instructions

Part 2 of the 3D Engine - projecting a 3D point onto a 2D screen. ******************************************** More steps for the 3D engine in this studio: http://scratch.mit.edu/studios/202972 ******************************************** This step is pretty basic, and has been covered by others numerous times, but it's here for completeness, with a bit of user interaction to investigate how it behaves... The 3D points in this example have a size (so think of them as spheres), and this project also includes working out how the sphere's projected size changes as its 3D position changes (i.e. how big it should appear on the screen). This means it's rather like the way the stars work in InterXeptor 3D. The methodology is the same as in InterXeptor 3D, though I've tried to make it more obvious what's going on by creating separate projection blocks ("Project 3D point to 2D" and "Project 3D size to 2D") - but they're so small and simple that you wouldn't bother within a real project. It's all reasonably straight-forward so far, but things will become more complicated once we start thinking about surfaces...

Description

See inside to find the (very simple!) blocks that do the projections. It works by multiplying by the view factor and dividing by the Z co-ordinate. This is all that's needed to project the 3D X position to the required 2D pen X position, the 3D Y position to 2D pen Y position, and to project the sphere's size onto the screen. There's a minimum Z render distance (set to 10 here) to ensure sizes don't get too big when multiplying by view factor & dividing by Z - this becomes fairly important once we get to filling surfaces... The view factor value is such that the field-of-view is about as large as possible without too much distortion at the edges when showing objects/surfaces. The value used here is 270, which works out to be a bit under a 90 degree view horizontally (i.e. from X=-240 to X=240), and nearly 70 degrees vertically (from Y=-180 to Y=180). Using a value of 240 instead would make it exactly 90 degrees across - but the edges begin to look a bit too distorted when showing 3D shapes (although some don't mind that, so...)

Project Details

Project ID10095002
CreatedMarch 29, 2013
Last ModifiedMarch 29, 2013
SharedMarch 30, 2013
Visibilityvisible
CommentsAllowed