About

Author: DeathByNukes

This is a part of my Audiosurf 2 scripting library.

Contents

track_bounds

Calculates various information about the size of the level. Depends on my vec3 library.

track, track_vec3s = GetTrack(), {}
do
	local track, track_vec3s, vec3 = track, track_vec3s, vec3
	local min, max
	do
		track_vec3s[1] = vec3.fromApi(track[1].pos)
		local minx,miny,minz = unpack(track_vec3s[1])
		local maxx,maxy,maxz = minx,miny,minz

		local pos,x,y,z
		for i=2,#track do
			pos = track[i].pos
			x,y,z = pos.x, pos.y, pos.z
			track_vec3s[i] = {x,y,z}

			if x < minx then minx = x elseif x > maxx then maxx = x end
			if y < miny then miny = y elseif y > maxy then maxy = y end
			if z < minz then minz = z elseif z > maxz then maxz = z end
		end

		min, max = {minx,miny,minz}, {maxx,maxy,maxz}
	end

	-- approximate the size of the rail geometry
	do
		local side, up, down = GetSkinProperties().trackwidth + 15, 36, 40
		node_bounds = {
			side = side,
			up = up,
			down = down,
			min = {-side, -down, -side},
			max = { side,  up,    side},
		}
	end
	track_bounds = {
		node_min = vec3.add(min, node_bounds.min),
		node_max = vec3.add(max, node_bounds.max),
		min = min, -- this is the bounds that AS2 uses internally
		max = max,
	}
	local track_bounds = track_bounds
	track_bounds.node_center = vec3.avg(track_bounds.node_min, track_bounds.node_max)
	track_bounds.center = vec3.avg(track_bounds.min, track_bounds.max)

	local cx,cz = track_bounds.node_center[1], track_bounds.node_center[2]
	local pos,dist
	local max = 0
	for i=1,#track do
		pos = track_vec3s[i]
		dist = (pos[1] - cx)^2 + (pos[3] - cz)^2
		if dist > max then max = dist end
	end
	track_bounds.node_center_horizontal_radius = math.sqrt(max) + node_bounds.side
end