Gravity around a tube

Hi guys,

I was playing F-ZeroGX the other day and wondered how I’d do something similar. Basically, in GX, the race courses in a few levels are tubes and you can go all around them.

How would I apply gravity so there’s a constant force pushing down on each face of the tube?

Once I’ve figured this out I can see loads of other uses for it…

Mike

1 Like

First of all, you turn off gravity.

The easiest way to do it would be to apply gravity relative to the up vector (green axis) of the object

var gravity = 9.81;
function FixedUpdate ()
{
    rigidbody.AddForce(-Vector3.up * rigidbody.mass * gravity);
}

This might have some issues if the object bounces off walls and starts rotating, since it will change the gravity direction in that case.

The other way to do it is to determine the position relative to the tube. And apply the force so it goes outwards. Basically the direction from the center of the tube to the position of the vehicle.

if you used the car wizard theres the “Down Pressure” varible which would help I would think
See the project examples -race demo
AC

Could you have an object in the center of your tube that constantly radiates force to your objects?

If you use a chain of line segments along the middle of the tube (probably defined by an array of points), you can get the nearest point on the line segment by looping through the array and use Mathfx.NearestPointStrict from the Wiki (http://www.unifycommunity.com/wiki/index.php?title=Mathfx) to get the point the object should gravitate towards.

I used something similar for track movement in Botbuilder; most of this code is copied and pasted from that. It also requires at least 2 points in its current form, otherwise bad things probably happen.

var TrackPos : Vector3[];
var gravity = 9.81;

		var shortestDist : float = 999999.0;
		var sdi : int = 0;
		for (s=0;s<TrackPos.length-1;s++) {
			var thePoint = Mathfx.NearestPointStrict(TrackPos[s], TrackPos[s+1], targetObj.position);
			var dist = Vector3.Distance(thePoint, targetObj.position);
			if (dist < shortestDist) {
				shortestDist = dist;
				sdi = s;
			}
		}
		gPoint = Mathfx.NearestPointStrict(TrackPos[sdi], TrackPos[sdi+1], targetObj.position);


rigidbody.AddForce((gPoint - transform.position).Normalized()*gravity*rigibody.mass;

I’ve been wondering about something similar, a “half pipe” as in snowboarding or skateboarding. In this case it’s centrifugal force that keeps the rider from falling down. Anyone played with this?

Centrifugal force is not a force at all, but only inertia, which is certainly emulated by the physics engine.

Model a halfpipe, add a mesh collider, and drop a ball on one side.

I’ve been wondering how one might set up a sphere to have gravity that always attracts towards it’s center. Any ideas?

The last line of the script I posted does exactly that.

Else you can have an empty gameObject following the car but staying in the center of the tube (you might have issues if for example you have different sizes of tubes) and this gameObject will apply an ExplosionForce (see: Unity - Scripting API: Rigidbody.AddExplosionForce) to push the car on the road.

well, if you want an attractive effect, add a negative explosion force…

I’m trying to do this too…