Convert JavaScript to Lua

How would I convert these 2 JS functions below to LUA?

function rotate(velocity, angle){
	const rotatedVelocties = {
		x: velocity.x * Math.cos(angle) - velocity.y * Math.sin(angle),
		y: velocity.x * Math.sin(angle) + velocity.y * Math.cos(angle)
	};
	return rotatedVelocties;
}

function resolveCollision(particle, otherParticle){
	const xVelocityDiff = particle.velocity.x - otherParticle.velocity.x;
	const yVelocityDiff = particle.velocity.y - otherParticle.velocity.y;
	const xDist = otherParticle.x - particle.x;
	const yDist = otherParticle.y - particle.y;
	if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) {
		const angle = - Math.atan2(otherParticle.y - particle.y, otherParticle.x - particle.x);
		const m1 = particle.mass;
		const m2 = otherParticle.mass;
		const u1 = rotate(particle.velocity, angle);
		const u2 = rotate(otherParticle.velocity, angle);
		const v1 = { x: u1.x * (m1 - m2) / (m1 + m2) + u2.x * 2 * m2 / (m1 + m2), y: u1.y};
		const v2 = { x: u2.x * (m1 - m2) / (m1 + m2) + u1.x * 2 * m2 / (m1 + m2), y: u2.y};
		const vFinal1 = rotate(v1, -angle);
		const vFinal2 = rotate(v2, -angle);
		particle.velocity.x = vFinal1.x;
		particle.velocity.y = vFinal1.y;
		otherParticle.velocity.x = vFinal2.x;
		otherParticle.velocity.y = vFinal2.y
	}
}

Edit: code below

function rotate(velocity, angle)
	local rotatedVelocties = {
		x = velocity.X * math.cos(angle) - velocity.Y * math.sin(angle);
		y = velocity.X * math.sin(angle) + velocity.Y * math.cos(angle);
	}
	return rotatedVelocties
end

function resolveCollision(particle, otherParticle)
	local xVelocityDiff = particle.Velocity.X - otherParticle.Velocity.X
	local yVelocityDiff = particle.Velocity.Y - otherParticle.Velocity.Y
	local xDist = otherParticle.X - particle.X
	local yDist = otherParticle.Y - particle.Y
	if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) then
		local angle = -(math.atan2(otherParticle.Y - particle.Y, otherParticle.X - particle.X));
		local m1 = particle.mass;
		local m2 = otherParticle.mass;
		local u1 = rotate(particle.velocity, angle);
		local u2 = rotate(otherParticle.velocity, angle);
		local v1 = { x = u1.x * (m1 - m2) / (m1 + m2) + u2.X * 2 * m2 / (m1 + m2), y = u1.Y};
		local v2 = { x = u2.x * (m1 - m2) / (m1 + m2) + u1.X * 2 * m2 / (m1 + m2), y = u2.Y};
		local vFinal1 = rotate(v1, -angle);
		local vFinal2 = rotate(v2, -angle);
		particle.Velocity.X = vFinal1.X;
		particle.Velocity.Y = vFinal1.Y;
		otherParticle.Velocity.X = vFinal2.X;
		otherParticle.Velocity.Y = vFinal2.Y
	end
end
2 Likes

That was incredibly tedious, but I think I changed all the necessary syntax.

Still, there was a lot of missing information so I couldn’t know exactly what to change. Like, what is ‘mass’, what is ‘particle’ (a BasePart?)

This should get you moving in the right direction though.

5 Likes

Are these supposed to be parts or something for UI elements?

1 Like

They exist in the rest of the code, I was just wondering how to change these

1 Like