This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DSlink2010's profile picture

Java game division not working correctly

Started by DSlink2010, 27 February 2013 - 01:23 PM
DSlink2010 #1
Posted 27 February 2013 - 02:23 PM
The problem is that at line 72 in Display.java the division problem outputs "0.0" instead of between "0.001"-"1". So can anyone please help? All the classes are down there.
Classes
Display.java
Spoiler
package com.cube.cubefront;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import com.cube.cubefront.graphix.Render;
import com.cube.cubefront.graphix.Screen;
public class Display extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static String TITLE = "Cubefront Pre-Alpha 0.02";
private Thread thread;
private Screen screen;
private Game game;
private BufferedImage img;
private boolean running = false;
private int[] pixels;
public Display() {
  Dimension size = new Dimension(WIDTH, HEIGHT);
  setPreferredSize(size);
  setMinimumSize(size);
  setMaximumSize(size);
  screen = new Screen(WIDTH, HEIGHT);
  game = new Game();
  img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}
private void start() {
  if (running)
   return;
  running = true;
  thread = new Thread(this);
  thread.start();
}
public void stop() {
  if (!running)
   return;
  running = false;
  try {
   thread.join();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
}
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;
   System.out.println(passedTime+ " " + unprocessedSeconds);
   unprocessedSeconds += passedTime / 1000000000;
   while(unprocessedSeconds > secondsPerTick){
	tick();
	unprocessedSeconds -= secondsPerTick;
	ticked = true;
	tickCount++;
	if (tickCount % 60 == 0) {
	 System.out.println(frames + " fps");
	 previousTime += 1000;
	}
   }
   if (ticked) {
	render();
	frames++;
   }
   render();
   frames++;
  }
}
private void tick() {
  game.tick();
}
private void render() {
  BufferStrategy bs = this.getBufferStrategy();
  if (bs == null) {
   createBufferStrategy(3);
   return;
  }
  screen.render(game);
  for (int i = 0; i < WIDTH * HEIGHT; i++) {
   pixels[i] = screen.pixels[i];
  }
  Graphics g = bs.getDrawGraphics();
  g.drawImage(img, 0, 0, WIDTH + 10, HEIGHT + 10, null);
  g.dispose();
  bs.show();
}
public static void main(String[] args) {
  // Game Display
  Display game = new Display();
  JFrame frame = new JFrame();
  frame.add(game);
  frame.pack();
  frame.setTitle(TITLE);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null);
  frame.setResizable(false);
  frame.setVisible(true);
  game.start();
}
}

Render3D.java
Spoiler
package com.cube.cubefront.graphix;
import com.cube.cubefront.Game;
public class Render3D extends Render {
public Render3D(int width, int height) {
  super(width, height);
}
public void floor(Game game) {
  for (int y = 0; y < height; y++) {
   double ceiling = (y - height / 2.0) / height;
   double z = 8 / ceiling;
   for (int x = 0; x < width; x++) {
	double depth = (x - width / 2.0) / height;
	depth *= z;
	int xx = (int) (depth) &amp; 15;
	System.out.println(game.time);
	int yy = (int) (z + game.time) &amp; 15;
	pixels[x + y * width] = (xx * 16) | (yy * 16) << 8;
   }
  }
}
}

Render.java
Spoiler
package com.cube.cubefront.graphix;
import com.cube.cubefront.Display;
public class Render {
public final int width;
public final int height;
public final int[] pixels;
public Render(int width, int height) {
  this.width = width;
  this.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 >= height) {
	continue;
   }
   for (int x = 0; x < render.width; x++) {
	int xPix = x + xOffset;
	if (xPix < 0 || xPix >= width) {
	 continue;
	}
	int alpha = render.pixels[x + y * render.width];
	if (alpha > 0) {
	 pixels[xPix + yPix * width] = alpha;
	}
   }
  }
}
}

Screen.java
Spoiler
package com.cube.cubefront.graphix;
import java.util.Random;
import com.cube.cubefront.Game;
public class Screen extends Render {
private Render test;
private Render3D render;
public Screen(int width, int height) {
  super(width, height);
  Random random = new Random();
  render = 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 anim2 = (int) (Math.cos((game.time + i * 2) % 1000.0 / 100) * 100);
  }
  render.floor(game);
  draw(render, 0, 0);
}
}

Game.java
Spoiler
package com.cube.cubefront;
public class Game {
public int time;
public void tick() {
  time+=3;
}
}
PixelToast #2
Posted 27 February 2013 - 02:48 PM
er, this isnt really the forum >_>
Dlcruz129 #3
Posted 27 February 2013 - 02:59 PM
General: Ask anything and everything related to anything and everything (anything and everything excluded) :P/>

On-Topic: Pastebin please? I don't wanna count to line 72.
immibis #4
Posted 27 February 2013 - 03:41 PM
I'm guessing this one:

 unprocessedSeconds += passedTime / 1000000000;

passedTime is a long, and 1000000000 is an int. Neither of them are float or double, so it truncates the result.
DSlink2010 #5
Posted 28 February 2013 - 09:44 AM
I'm guessing this one:

unprocessedSeconds += passedTime / 1000000000;

passedTime is a long, and 1000000000 is an int. Neither of them are float or double, so it truncates the result.
I'll try changing them to ints.
[EDIT] I changed it to a int and it still outputs 0 as the answer.
oeed #6
Posted 28 February 2013 - 10:12 AM
You might want to post this on StackOverflow or similar if you haven't already done so, you're more likely to get it fixed.
DSlink2010 #7
Posted 28 February 2013 - 11:20 AM
I looked on Stack Overflow, and it showed that someone had a FPS count malfunction, and the answer was to remove the fps counter, I did, it works now. Its fixed, it goes fast, so I changed the amount of incrementation that game.time gets. So can any staff lock this?
Xtansia #8
Posted 28 February 2013 - 03:22 PM
I'm guessing this one:

unprocessedSeconds += passedTime / 1000000000;

passedTime is a long, and 1000000000 is an int. Neither of them are float or double, so it truncates the result.
I'll try changing them to ints.
[EDIT] I changed it to a int and it still outputs 0 as the answer.
They already were ints/longs,
He was meaning that either passedTime or 1000000000 needs to be a float or double for the division to work 'correctly'.