0

I watched TheCherno's game programming tutorial:
http://www.youtube.com/watch?v=RKPEQfkhbAY

And in this episode he wrote this code to make the 3d world.

package game.display.graphics;

public class Render3D extends Render {

    public Render3D(int width, int height) {
        super(width, height);
    }

    public void floor() {
        for(int y = 0; y < height; y++) {
            double ydepth = y - height / 2;
            double z = 100.0 / ydepth;

            for(int x = 0; x < width; x++) {
                double xdepth = x - width / 2;
                xdepth *= z;
                int xx = (int) (xdepth) & 5;

                pixels[x+y*width] = xx * 128;
            }
        }
    }
}

I don't really understand the code.. so someone can explain it to me?

5
  • What is it that you don't understand? The meaning of the values or the syntax of the code? It basically fills an array of pixels (presumably on screen) with a bunch of depth values, i.e. the color of the pixel depends on its position on the screen and the variable names indicate it's some visualization of distance to the screen (aka depth). Commented Sep 17, 2013 at 10:56
  • Btw, doesn't the video explain the code? Commented Sep 17, 2013 at 11:02
  • nope.. he is saying "debug it in your mind".. Commented Sep 17, 2013 at 11:25
  • Well, I assume he tells you what the result should mean. I skimmed over the video (without sound) and from what I saw, it just looks like some preparations for filling a screen buffer. So the meaning of the result might actually not be that relevant and I reask my question: what is it that you don't understand? The meaning of the variables/result or the syntax of the code? Commented Sep 17, 2013 at 12:56
  • @Thomas I don't understand the syntax, I mean all that variables thing.. all of those calculations are so confusing, and the bitwise operator that is coming from nowhere is confusing also. Commented Sep 17, 2013 at 16:37

1 Answer 1

1

I still think the calculations themselves are not relevant, it just seems like some experiment (especially with the screens from the video in mind).

However, I'll try and explain what I think the code does (without more information it's not that easy):

First, he loops over all screen pixels and calculates a color for those.

  • double ydepth = y - height / 2; would cause the depth to be negative for all pixels at the top half of the screen
  • double xdepth = x - width / 2; would cause the depth to be negative for the left half of the screen

This seems to be done in order to center the generated pattern within the screen. There are those 2 black segments (lower left and upper right), but the code you posted doesn't explain what is done here.

int xx = (int) (xdepth) & 5; would set the value of xx to 0 (i.e. black) whenever neither bits 1 nor 3 (5 is binary 101) are set. Thus xx can only have the values 0, 1, 4 and 5 which would cause those black streaks.

xx * 128 would boost the value to 0, 128, 512 and 640, which would result in a different depending on how pixels is used. (If pixels represents colors in ARGB format, 0 would be black, 128 would be dark blue, 512 would be dark green (almost black) and 640 would also be dark blue with some slight green component.

That being said, I just skimmed over the code and did some example calculations where necessary. For more information just follow that trail.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man.. I kind of understand it better now. as i look at the code and play with it a bit i am starting to see the picture about how it works. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.