Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  3D Game Programming Error  (Read 1273 times)
0 Members and 1 Guest are viewing this topic.
Offline DurzoBlint15

Senior Newbie





« Posted 2012-03-24 06:13:15 »

I'm following this video : http://www.youtube.com/watch?v=RKPEQfkhbAY&feature=channel  and when I was done with it, I ran the program and got this: http://imgur.com/eammB   It should look staright without the zig zag lines. Here is all of my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
package me.kenneth.game;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import me.kenneth.graphics.Screen;

import java.awt.image.BufferStrategy;
import java.awt.image.DataBufferInt;

public class Window extends Canvas implements Runnable {
   
   public static final long serialVersionUID = 1L;
   public static final int WIDTH = 800;
   public static final int HEIGHT = 600;
   public static final String TITLE = "MineGame Pre-Alpha 0.2";
   
   private Thread thread;
   private boolean running = false;
   private BufferedImage img;
   private Screen render;
   private int pixels[];
   private Game game;
   
   public Window() {
      Dimension dim = new Dimension(WIDTH, HEIGHT);
      setPreferredSize(dim);
      setMinimumSize(dim);
      setMaximumSize(dim);
      render = new Screen(WIDTH, HEIGHT);
      img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
      pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
      game = new Game();
   }
   
   private void start() {
      if (running) return;
     
      running = true;
      thread = new Thread(this);
      thread.start();
   }
   
   private void stop() {
     
      if (!running) return;
     
      running = false;
      try {
         thread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
         System.exit(0);
      }
     
   }
   
   public void run() {
     
      int frames = 0;
      double unprocessedSeconds = 0;
      long previousTime = System.nanoTime();
      double secondsPerTick = 1 / 60.0;
      int tickCount = 0;
      boolean ticked = false;
     
      while (running) {
         
         long currentTime = System.nanoTime();
         long passedTime = currentTime - previousTime;
         previousTime = currentTime;
         unprocessedSeconds += passedTime / 1000000000.0;
         
         while (unprocessedSeconds > secondsPerTick) {
            tick();
            unprocessedSeconds -= secondsPerTick;
            ticked = true;
            tickCount++;
            if (tickCount % 60 == 0) {
               System.out.println(frames + "fps");
               previousTime += 1000;
               frames = 0;
            }
         }
         
         render();
         frames++;
         
      }
     
      Toolkit.getDefaultToolkit().sync();
     
   }
   
   private void tick() {
     
      game.tick();
     
   }
   
   private void render() {
     
      BufferStrategy bs = this.getBufferStrategy();
      if (bs == null) {
         createBufferStrategy(3);
         return;
      }
     
      render.render(game);
     
      for (int i = 0; i < WIDTH * HEIGHT; i++) {
         pixels[i] = render.pixels[i];
      }
     
      Graphics g = bs.getDrawGraphics();
      g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
      g.dispose();
      bs.show();
     
   }
   
   public static void main(String[] args) {
     
      Window game = new Window();
      JFrame frame = new JFrame(TITLE);
      frame.add(game);
      frame.setResizable(false);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.pack();
     
      game.start();
     
   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
package me.kenneth.game;

public class Game {
   
   public int time;
   
   public Game() {
      time = 0;
   }
   
   public void tick() {
      time += 2;
   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
package me.kenneth.graphics;

import me.kenneth.game.Window;

public class Render {
   
   public final int WIDTH;
   public final int HEIGHT;
   public final int[] pixels;
   
   public Render(int width, int height) {
     
      WIDTH = width;
      HEIGHT = height;
      pixels = new int[width * height];
     
   }
   
   public void draw(Render render, int xOffset, int yOffset) {
     
      for (int y = 0; y < render.HEIGHT; y++) {
         int yPix = y + yOffset;
         if (yPix < 0 || yPix >= Window.HEIGHT) continue;
         for (int x = 0; x < render.WIDTH; x++) {
            int xPix = x + xOffset;
            if (xPix < 0 || xPix >= Window.WIDTH) continue;
           
            int alpha = render.pixels[x + y + render.WIDTH];
           
            if (alpha > 0) {
           
            pixels[xPix + yPix * WIDTH] = render.pixels[x + y * render.WIDTH];
           
            }
         }
      }
     
   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
package me.kenneth.graphics;

import java.util.Random;

import me.kenneth.game.Game;

public class Screen extends Render {

   private Render test;
   private Render3D render3D;

   public Screen(int width, int height) {
      super(width, height);
      Random random = new Random();
      render3D = new Render3D(WIDTH, HEIGHT);
      test = new Render(256, 256);
      for (int i = 0; i < 256 * 256; i++) {

         test.pixels[i] = random.nextInt() * (random.nextInt(5) / 4);

      }
   }

   public void render(Game game) {
      for (int i = 0; i < WIDTH * HEIGHT; i++) {
         pixels[i] = 0;
      }

      for (int i = 0; i < 50; i++) {
         int anim = (int) (Math.sin((game.time + i * 2) % 1000.0 / 100) * 100);
         int anim1 = (int) (Math.cos((game.time + i * 2) % 1000.0 / 100) * 100);

      }
     
      render3D.floor();
      draw(render3D, 0, 0);

   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package me.kenneth.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.4;
         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;
         }
      }
     
   }

}

Is it something wrong in the Render3D class?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-03-24 07:50:46 »

In the video, he did "(int) xDepth & 15", not "& 5". Try to double check your code before posting immediately for help Cranky

Offline DurzoBlint15

Senior Newbie





« Reply #2 - Posted 2012-03-24 08:00:01 »

Sorry  ha ha. I tripple checked the video and I really thought it said & 5.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DurzoBlint15

Senior Newbie





« Reply #3 - Posted 2012-03-25 03:23:18 »

He actually wrote & 5 and not &15. I copied him exactly and it has a 3D effect but there are diagonal lines going through everything so it kind of ruins the effect.
Offline sproingie

JGO Kernel


Medals: 200



« Reply #4 - Posted 2012-03-25 05:00:20 »

& 15 is masking off the low four bits.  & 5 just doesn't make sense for anything I can think of other than maybe checking two flags (1 and 4).  You should always be keeping powers of two in mind when doing bitwise arithmetic, and more generally you should understand why code is written the way it is rather than blindly copying it.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (83 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (213 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!