I need to make a shape like a rectangular prism bend into a curve shape, while having the physics match the curved object. What is the best way to do this (without spending money). Some background info: this is for a Roller Coaster simulation.
Here is a free Catmull Rom script and its derivative
public static Vector3 CatmullRomPoint( Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t, float tension ) {
t = Mathf.Clamp01( t );
float tSquared = t*t;
float tCubed = t*t*t;
return p0 * ( -tension * t + 2f * tension * tSquared - tension * tCubed ) +
p1 * ( 1f + (tension - 3f) * tSquared + (2f - tension) * tCubed ) +
p2 * ( tension * t + (3 - 2 * tension) * tSquared + (tension - 2) * tCubed ) +
p3 * ( -tension * tSquared + tension * tCubed );
}
public static Vector3 CatmullRomDirection( Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t, float tension ) {
t = Mathf.Clamp01( t );
return 3f*((p3 + p2 - p1 - p0)*tension - 2f*p2 + 2f*p1)*t*t - 2f*((p3 + 2f*p2 - p1 - 2f*p0)*tension - 3f*p2 + 3f*p1)*t + (p2 - p0)*tension;
}
this will give you a point or direction on a curve starting from p1 coming from the direction of p0 and ending at p1 going in the direction of p2, sampled at point t which is between 0 and 1. the tension is how big the tangents are and looks best at a value of 0.5.
you would then need to create a mesh that follows this curve, there is alot of resources available for this just google it. Unity - Scripting API: Mesh
this should be a good statring point it is not an easy task to do.