smoother x/y-axis increments

Is there a way to increment the x/y coordinates of a render smoothly by merely just incrementing it by 1 ‘faster’?

I’ve come across situations in which I am changing the speed/rate of travel of an object by just adding a larger value to the x/y coordinates of said object

so, something like

x += 5;

would make it appear to go faster but instead result in choppy rendering (since you’re adding a large value like 5 instead of progressively adding values of 1)

in essence:

is there a way to ‘speed up’ incrementing by 1 (ie. ++x) in order to reduce choppiness when changing the rate of travel of an object?

Yes you can I believe like this example:
(Ints)


	public void move() {
		if (player.movingRight()) {
			player.absX++;
		}
	}

Or if you’re using a ‘double’:


	public void move() {
		if (player.movingRight()) {
			player.absX = player.absX += 0.1;
		}
	}

If that’s not what you’re looking for let me know.

If you’re using any sort of timer that updates (or allows updating) of the sprite position when time’s up, then you can just use the speed of the sprite to calculate the new timer time. Each sprite would need it’s own timer, but that’s normal.

I am referring to faster increments of the x/y-axis.

not sure how that would play out, maybe if I were to increase the time depending on how long the key is held? I’m mainly having trouble increasing the value by 1 fast enough so that I can move at a fast pace but still prevent choppiness

it’s like basically trying to scroll over 200 pixels when you could just increment by like 50 pixels and get there extremely quickly whereas you’d increment by 1 pixel very quickly and get there as quick (except not producing a choppy, ‘bursty’ travel)

Slowly increasing?


	public int elapsed = 0; // milleseconds.
	public int desiredLimit = 12; // max xSpeed
	public int desiredFix = 30; // somewhere between 0 and 80.
	public int xSpeed = 2; // start out slow.
	
	public void slowlyIncrease() {
		elapsed++;
		if (elapsed > 0 && elapsed < 80) {
			xSpeed = 4;
		} else if (elapsed > 80 && elapsed < 180) {
			xSpeed = 8;
		} else if (elapsed > 180 && elapsed < 280) {
			xSpeed = 8;
		} else if (elapsed > 280 && elapsed < 380) {
			xSpeed =10;
		} else if (xSpeed > desiredLimit) {
			xSpeed = desiredLimit;
			elapsed = desiredFix;
		}
	}

Adjusted to your liking’s of course.

Or, you know, do it the normal way and have an acceleration value ;D


private double x, vX = 50, aX = 2;
private double y, vY = 50, aY = 2;

public void update() {
    vX += aX;
    x += vX;

    vY = aY;
    y = vY;
}

What’s with the double assignment?


player.absX += 0.1;

That is all you need.

I was messing around in a class.


	private static void randomMovement() {
		absX = absX + x1; // hint
		absY = absY + y1; // hint, hint
		if (absX < 40) {
			absX = 40;
		}
		if (absY+20 > 520) {
			absY = 500;
		}
		if (absX+20 > 760) {
			absX = 760-20;
		}
		if (absY < 40) {
			absY = 40;
		}
		if (absX < Player.getAbsX()) {
			x1++;
		} else if (absX > Player.getAbsX()) {
			x1--;
		}
		if (absY < Player.getAbsY()) {
			y1++;
		} else if (absY > Player.getAbsY()) {
			y1--;
		}
	}

Smoothness depends on your framerate. If you have enough fps and you update the position of all objects every frame it should be perfectly smooth.

Look at this code.
It’s an example of a simple game loop that tries to achieve a given target fps.

class Sprite {
    double  posX, posY, speedX, speedY;
    
    public void update(double time) {
        posX += time*speedX;
        posY += time*speedY;
    }
}

class Game extends JPanel {
    double targetFPS = 60; // try to achieve 60 fps
    double timePerFrame = 1.0/targetFPS;
    ArrayList<Sprite> sprites = new ArrayList<Sprite>();
    //for double buffering
    Image image;
    
    public void start() {
        image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        int sleep=(int)(timePerFrame*1000);
        double timeOfLastFrame = System.nanoTime()/1e9;
        double startTime = timeOfLastFrame;
        int frames = 0;
        while(true) {
            double time = System.nanoTime()/1e9;
            double timePassed = time - timeOfLastFrame;
            timeOfLastFrame = time;
            for(s: sprites) {
                s.update(timePassed);
            }
            Graphics g = image.getGraphics();
            g.setColor(backgroundColor);
            g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
            for(s: sprites) {
                s.paint(g);
            }
            g.dispose();
            g = getGraphics();
            g.drawImage(image, 0, 0 ,null);
            g.dispose();
            try {
                if(timePassed>timePerFrame && sleep > 0) sleep--;
                else if(timePassed<timePerFrame) sleep++;
                Thread.sleep(sleep);
            } catch (Exception e) {
            }
            frames++;
            time=timeOfLastFrame-startTime;
            if(time >= 1) {
                double fps=frames/time;
                frames=0;
                System.out.println("FPS: "+fps);
                startTime = timeOfLastFrame;
            }
        }
    }
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }
}

You want to divide timePassed by 1e9 before multiplaying by speed :wink:

Why? timePassed already is in seconds. No need to divide it.

Oh! I didn’t see that division by 1e9 on the actual timeOfLastFrame. However I do advise against that since it is quite inaccurate. You should be only dividing the final raw timePassed value.

Yes, I guess you are right. Although the inaccuracy is actually very small - less then a µs. If I was using a float instead of a double however it could be a problem.

Thanks, all. I’ll try out some of your solutions and report back.

edit: I found a solution by using the delta*moveSpeed method.