This site requires JavaScript, please enable it in your browser!
Greenfoot back

Report as inappropriate.

SPower
SPower presents ...

2019/8/20

3D cubes demo

This scenario can render 3D objects consisting of polygons (instead of only triangles) of different colours. It is fully 3D, meaning that its camera can be turned in any direction, and have the program render the world appropriately.

To illustrate its capabilities, a demo is included, showing 400 small cubes (of a random colour), along with a larger one that misses two sides, and which has a triangle inside of it. Note that the downloaded version runs much faster (consistent 61 FPS in this demo) than the online HTML5 version.

Controls for the demo are:
- wasd to move the camera forward/backward or sideways;
- arrow keys to turn the camera
- z and x to rotate the camera around its axis.

The source code is available and documented.

Some details on how it works: it renders by thinking of the screen as an object in 3D space itself. This is achieved by giving the camera a location (which can be thought of as the 'focal point'), a direction that the player is looking at, and two directions which tell what looks like the 'left-right' direction on screen, and the 'up-down' direction. These last two directions must always be orthogonal.

To illustrate, the z and x keys rotate the 'left-right' and 'up-down' around the direction of the player, whereas the arrow keys change the direction of the player.

The rendering is done from back to front: all objects are sorted from back to front, first drawing those furthest away.

The controls can be fully modified to create many different effects, such as an airplane-like behaviour, walking behaviour, etc.

The FPS counter is taken from mjrb4 (see https://www.greenfoot.org/scenarios/261).

1933 views / 11 in the last 7 days

8 votes | 0 in the last 7 days

Tags: demo with-source

This scenario is a member of: Reusable actors & support classes, 3D


open in greenfoot
Your browser does not support the canvas tag.
This is beautiful, well done
soccer28JGsoccer28JG

2019/8/29

I like how you change colors.
LewisEroLewisEro

2020/3/25

this is blowing my mind. where would i rescale the cubes? do you think this could be used to make a functional wolfenstein/doom style game?
SPowerSPower

2021/8/4

@LewisEro For the scaling of the cubes, note that the Cube class constructor comes with a parameter for the size. In the Demo class for instance, I create cubes using new Cube(0.2, Color.BLACK, colors[colorNumber]), where the 0.2 indicates the size of the cube. As to creating a Wolfenstein/Doom game, it absolutely could be used for that. The biggest fear I have would be the performance. Offline this would be much better than online, but still you'd likely be limited in how many polygons you can have appear on screen at once. Of course, making it a full game would require more stuff than just the rendering -- I'm talking collision detection or AI, to name some basic things. Builderboy2005 has a scenario* up (Castle Storm II) which comes much closer to a such a game (although to my knowledge he never finished it). It comes with an .obj reader (meaning you can model objects in Blender and put them in the game directly), as well as floor detection. Still, the code is likely very different (I didn't look at his code when writing this scenario; I suspect his code is more optimised, but would look very different), so even implementing his approach into this scenario would take some time. And not to mention making all the content... In any case, I don't have the time or willpower to make a game out of this :P But anyone is free to use it if they want. * = https://www.greenfoot.org/scenarios/1233

Want to leave a comment? You must first log in.

Who likes this?

Gabe1098 gorilA876 RcCookie soccer28JG Super_Hippo secretbackupboss danpostismyhero valdes