0

I'm trying to use delta to move a rectangle. However when I multiply it by the speed and add it to the rectangle x position, the rectangle does not move. I thought the problem might lie in the game loop, but I don't think this is it. Here's the code:

package Main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

//do double buffering

public class Game extends Canvas {
    int x, y;
    private static final long serialVersionUID = 1L;

    public static final int height = 400;
    public static final int width = height * 16 / 9;

    JPanel p;
    Game game;

    Image buffer;

    KeyListener kl;
    MouseListener ml;

    public boolean running = true;

    public Game(){
        kl = new KeyListener(){

            public void keyPressed(KeyEvent e) {

            }

            public void keyReleased(KeyEvent e) {

            }

            public void keyTyped(KeyEvent e) {

            }
        };

        ml = new MouseListener(){

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }
            public void mouseClicked(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {

            }

            public void mouseExited(MouseEvent e) {

            }
        };
    }

    public void update(double delta){
        x += 10/1000 * delta;
    }

    public void render(){
        BufferStrategy bs = getBufferStrategy();
        if(bs==null){
            createBufferStrategy(2);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 100, 100);

        g.dispose();
        bs.show();
    }

    public void run(){

        //initialize time loop variables
        long lastLoopTime = System.nanoTime();
        final int TARGET_FPS = 60;
        final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
        double lastFpsTime = 0;

        //Main game loop
        while(running)
        {
            //Calculate since last update
            long now = System.nanoTime();
            long updateLength = now - lastLoopTime;
            lastLoopTime = now;
            double delta = updateLength / ((double)OPTIMAL_TIME);

            //update frame counter
            lastFpsTime += updateLength;

            //update FPS counter
            if(lastFpsTime >= 1000000000)
            {
                lastFpsTime = 0;
            }

            //game updates
            game.update(delta);

            //graphics (gameState)
            game.render();

            try{
                Thread.sleep((Math.abs(lastLoopTime - System.nanoTime() + OPTIMAL_TIME)/1000000));
            }catch(Exception e){
                System.out.println("Error in sleep");
            }
        }
    }

    public void start(){
        JFrame frame = new JFrame("Game");

        game = new Game();

        frame.add(game);
        frame.pack();

        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.addKeyListener(kl);
        frame.addMouseListener(ml);

        frame.setVisible(true);

        run();
    }

    public static void main(String[] args){
        new Game().start();
    }
}

3 Answers 3

0

x and y are integers and 10/1000 * delta is most of the time less than 1. Incremeting x with such a value will not change x at all.

To fix this, try to change the type of x to float or double, and just use Math.floor() or just cast it to an int when drawing it:

g.fillRect((int) x, (int) y, 100, 100);
0

To continue on the previous answer: 10/1000 will give you zero while 10./1000 will give you the real number that it represents. So you should change your statement to

x += (int)(10./1000 * delta);

and hopefully delta is some number greater than 1000/10 to raise this to an integer greater than 0.

0

You need to change your x and y variables to be float or double.

double x, y;

And if you wish to see movement, you'll need to change to something like this...

x += 100d / 1000d * delta;

What you were doing before was dividing an integer 10 by an integer 1000 and getting 0 as a result.

You'll also need to cast the results to ints when you draw the rect...

g.fillRect((int)x, (int)y, 100, 100);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.