/**
 * A 2D velocity vector.
 * Components are expressed in pixels/millisecond, direction in radians.
 */
export default class Velocity
{
	constructor(vx = 0, vy = 0)
	{
		this.vx = vx;
		this.vy = vy;
	}

	get speed()
	{
		return Math.sqrt(this.vx * this.vx + this.vy * this.vy);
	}

	get direction()
	{
		return Math.atan2(this.vy, this.vx);
	}

	limitSpeed(maxSpeed)
	{
		if (this.speed > maxSpeed)
		{
			const dir = this.direction;
			this.vx = Math.cos(dir) * maxSpeed;
			this.vy = Math.sin(dir) * maxSpeed;
		}
	}
}
