Skip to main content
GameDev.net gamedev.net
🔒 Locked

Function approximation

Started by taby Jun 8, 2025 at 6:20 PM 4 replies 1.9k views
Original Post
taby
taby

I am successfully running a numerical algorithm to determine the collision between gravitational field lines and an axis-aligned bounding box – https://github.com/sjhalayka/ellipsoid_emitter

I am trying to come up with an approximation. Below is one of many ways I've tried to attack the problem. How would you do it differently?

MyBig get_intersecting_line_count_real(
	const MyBig n,
	const MyBig box_location_x,
	const MyBig box_length,
	const MyBig D)
{
	MyBig running_total_small = 0;
	MyBig running_total_big = 0;

	MyBig running_ratio = 0;

	const MyBig big_area =
		4 * pi
		* box_location_x * box_location_x;

	const MyBig big_length =
		2 * pi
		* box_location_x;

	const MyBig small_area =
		box_length * box_length;

	const MyBig small_length =
		box_length;

	const MyBig disk_like = 3 - D;

	const MyBig start_distance = 0;
	const MyBig end_distance = 1;
	const size_t distance_res = 10000;

	const MyBig distance_step_size =
		(end_distance - start_distance)
		/ (distance_res - 1);

	for (size_t step_index = 0;
		step_index < distance_res;
		step_index++)
	{
		const MyBig rn =
			start_distance +
			step_index * distance_step_size;

		if (rn <= disk_like)
			running_ratio += 1;
	}

	const MyBig r =
		running_ratio 
		/ static_cast<MyBig>(distance_res);

	const MyBig big = lerp_func(big_area, big_length, r);
	const MyBig small = lerp_func(small_area, small_length, r);

	return n * (small/big);
}
taby
taby

I think that perhaps instead of finding the analytical function, which may or may not exist, I can do regression with a neural network.

JoeJ
JoeJ

Tools like Mathematica can find / fit an approxation function providing a table, in case you didn't know.

taby
taby

That's cool. Unfortunately, I cannot afford Mathematica. There is Sage too, but I don't know if it does curve fitting.

JoeJ
JoeJ

taby said:
There is Sage too, but I don't know if it does curve fitting.

Seems yes. Googling for ‘sage curve fitting’ brings up AI generated advise and code example.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.