[Fixed] Degrading velocity

One more silly problem: I’m degrading my velocities like this:


if (input.isKeyDown(Input.KEY_UP)) {
	    player.setXV(player.getXVelocity() +
		Math.cos(Math.toRadians(player.getRotation())));
	    player.setYV(player.getYVelocity() +
		Math.sin(Math.toRadians(player.getRotation())));
	} else {
	    player.setXV(player.getXVelocity()*0.95);
	    player.setYV(player.getYVelocity()*0.95);
}

However, when I’ve just moved and my vehicle has gotton to the point of stopping, I see this is my terminal:


XVelocity: 1.8496787833130738E-16 YVelocity: 1.5520647848744412E-16
XVelocity: 1.75719484414742E-16 YVelocity: 1.474461545630719E-16
XVelocity: 1.6693351019400487E-16 YVelocity: 1.400738468349183E-16
XVelocity: 1.5858683468430463E-16 YVelocity: 1.3307015449317238E-16
XVelocity: 1.506574929500894E-16 YVelocity: 1.2641664676851377E-16
XVelocity: 1.4312461830258492E-16 YVelocity: 1.2009581443008806E-16
XVelocity: 1.3596838738745567E-16 YVelocity: 1.1409102370858365E-16
XVelocity: 1.291699680180829E-16 YVelocity: 1.0838647252315446E-16
XVelocity: 1.2271146961717873E-16 YVelocity: 1.0296714889699673E-16
XVelocity: 1.1657589613631978E-16 YVelocity: 9.781879145214689E-17

I’m also experiencing some “pulls” in the vehicle when it’s slowing down. For example, when moving diagonally down/left, it would suddenly drag the vehicle a lot more to the left than it would under normal physics. I think these two are related.

In my head multiplying something by 0.95 seems perfectly fine for degrading something slowly. Although it’s never hit zore fully… Meh…
Anyone got any other options on degrading, that doesn’t spawn weird bugs? :stuck_out_tongue:

Thanks

I usually just add in:


xVel = xVel > -0.01 && xVel < 0.01 ? 0 : xVel;
yVel = yVel > -0.01 && yVel < 0.01 ? 0 : yVel; 

Basically just cutting off very small values. Your results look correct if you don’t have that logic.

Also rather than directly adding sin and cos results, I’d add sin * speed and cos * speed, and if you’re using a delta in your game loop do sin * speed * delta. What happens when you press the Left key? The code you posted above shouldn’t have any issues.

I’m still just fooling around to the speed parameter will have to wait. Now the variable are reset proporly, but the problem persists.
When slowing down, the vehicle gets slightly pulled in a direction.

I’ll just post my render method:


public void update(GameContainer container, int delta)
	    throws SlickException {
	Input input = container.getInput();
	Actor player = model.getPlayer();
	
	// Input handling
	if (input.isKeyDown(Input.KEY_LEFT)) {
	    player.rotate(-5);
	} 
	if (input.isKeyDown(Input.KEY_RIGHT)) {
	    player.rotate(5);
	}
	    
	if (input.isKeyDown(Input.KEY_UP)) {
	    player.setXV(player.getXVelocity() +
		Math.cos(Math.toRadians(player.getRotation())));
	    player.setYV(player.getYVelocity() +
		Math.sin(Math.toRadians(player.getRotation())));
	} else {
	    if (player.getXVelocity() > -0.01 && player.getXVelocity() < 0.01) {
		player.setXV(0);
	    }
	    if (player.getYVelocity() > -0.01 && player.getYVelocity() < 0.01) {
		player.setYV(0);
	    }
	    player.setXV(player.getXVelocity()*0.95);
	    player.setYV(player.getYVelocity()*0.95);
	}
	
	player.setX(player.getX()+(int)player.getXVelocity());
	player.setY(player.getY()+(int)player.getYVelocity());
	
	System.out.println("XVelocity: "+ player.getXVelocity() + " YVelocity: "+player.getYVelocity());
	
    }

…and my Actor class:



package com.javadaemon.asteroids.model;

public class Actor {
    
    private int worldX, worldY; // Pixel coordinates - the middle of the actor
    private double rotation; // The amount this actor is rotated
    
    private double xVelocity;
    private double yVelocity;
    
    public Actor(int worldX, int worldY) {
	this.worldX = worldX;
	this.worldY = worldY;
	
	rotation = 0;
    }
    
    public void rotate(double amount) {
	rotation += amount;
	if (rotation > 360) {
	    rotation = 0;
	} else if (rotation < 0) {
	    rotation = 360;
	}
    }
    
    public void setXV(double xv) {
	xVelocity = xv;
    }
    
    public void setYV(double yv) {
	yVelocity = yv;
    }
    
    public void setX(int x) {
	worldX = x;
    }
    
    public void setY(int y) {
	worldY = y;
    }
    
    public double getRotation() {
	return rotation;
    }
    
    public int getX() {
	return worldX;
    }
    
    public int getY() {
	return worldY;
    }
    
    public double getXVelocity() {
	return xVelocity;
    }
    
    public double getYVelocity() {
	return yVelocity;
    }
}

Should I upload a video? :clue:

Fixed. It was rounding. Now all my variables are doubles, and i round them upon render. 8)