# How in the world were the physics in F-zero X done?

I was playing F-Zero X today and it boggles my mind how they achieved the car physics.
The game was released in 1998 on the N64.
I tried raycasting down from empty objects at different points on the vehicle and applying force as necessary, but it wasn’t smooth and you could fly straight through the track even at lower speeds since it relied on rigidbodies.

I’m trying to figure out how to re-create that magnetic car effect in Unity WITHOUT using the physics engine, as I don’t think it would work well at high speeds and wouldn’t be needed if the same effect was made on a N64.

If you’ve never played it, here’s a good example of the kinds of tracks in it

I have this weird thing where I have to know how stuff works, and this is really bugging me.
Any ideas?

I have this weird Idea that it may work as a magnet, so certain objects are attracted to the game object, in this case the racing track and the cars/gliders are attracted. for the tracks that is only one sided I think they either use “Normal” physics or “directional” physics (only attracts objects from one direction, eg normal physics is from up to down.)

Bear in mind this may be COMPLETELY wrong, but It’s my Idea how it may work

I tried this once just to see what I was able to come up with.

My first approach was pretty much like the one you said, however it didn’t turn out nicely so I decided to forget about physics engine stuff aswell.

What I tried next was to use a single raycast from the center of the ship and in the direction of its local down axis. With the info from this raycast you can know the distance to the track and also the normal of the track’s surface at your ship’s location. Then I smoothly lerped the ship’s rotation so that its up axis matched the direction of the track’s normal.
As for keeping the ship hovering you can try a similar approach: you check your current distance to the ground (given by the raycast) and compare it to your desired hovering height to adjust your height accordingly (probably using a lerp aswell so that it looks smooth).

I will try to find my code to post it here to make things clearer.

If you wanted a perfectly round track, the position of the car could probably be expressed as a distance away and angle relative to the spline that defines the road, to move the car forward, just sample the spline some distance further along, at the same relative angle

I was thinking of that, but totally forgot about lerping it to make it smoother lol
I’m gonna try to whip up some code using that idea.

EDIT:
"Then I smoothly lerped the ship’s rotation so that its up axis matched the direction of the track’s normal. "
wat.
How does one do such a thing?

Ok, I managed to put some new code together since I couldn’t find my old script. I rewrote it by heart so maybe it doesn’t work 100% and might need some tweaking. Here it is:

``````using UnityEngine;
using System.Collections;

public class ShipTest : MonoBehaviour
{
/*Ship handling parameters*/
public float fwd_accel = 100f;
public float fwd_max_speed = 200f;
public float brake_speed = 200f;
public float turn_speed = 50f;

/*Auto adjust to track surface parameters*/
public float hover_height = 3f;		//Distance to keep from the ground
public float height_smooth = 10f;	//How fast the ship will readjust to "hover_height"
public float pitch_smooth = 5f;		//How fast the ship will adjust its rotation to match track normal

/*We will use all this stuff later*/
private Vector3 prev_up;
public float yaw;
private float smooth_y;
private float current_speed;

void Update ()
{
/*Here we get user input to calculate the speed the ship will get*/
if (Input.GetKey(KeyCode.W))
{
/*Increase our current speed only if it is not greater than fwd_max_speed*/
current_speed += (current_speed >= fwd_max_speed) ? 0f : fwd_accel * Time.deltaTime;
}
else
{
if (current_speed > 0)
{
/*The ship will slow down by itself if we dont accelerate*/
current_speed -= brake_speed * Time.deltaTime ;
}
else
{
current_speed = 0f;
}
}

/*We get the user input and modifiy the direction the ship will face towards*/
yaw += turn_speed * Time.deltaTime * Input.GetAxis ("Horizontal");
/*We want to save our current transform.up vector so we can smoothly change it later*/
prev_up = transform.up;
/*Now we set all angles to zero except for the Y which corresponds to the Yaw*/
transform.rotation = Quaternion.Euler(0, yaw, 0);

RaycastHit hit;
if (Physics.Raycast(transform.position, -prev_up, out hit))
{
Debug.DrawLine (transform.position, hit.point);

/*Here are the meat and potatoes: first we calculate the new up vector for the ship using lerp so that it is smoothed*/
Vector3 desired_up = Vector3.Lerp (prev_up, hit.normal, Time.deltaTime * pitch_smooth);
/*Then we get the angle that we have to rotate in quaternion format*/
Quaternion tilt = Quaternion.FromToRotation(transform.up, desired_up);
/*Now we apply it to the ship with the quaternion product property*/
transform.rotation = tilt * transform.rotation;

smooth_y = Mathf.Lerp (smooth_y, hover_height - hit.distance, Time.deltaTime * height_smooth);
transform.localPosition += prev_up * smooth_y;
}

/*Finally we move the ship forward according to the speed we calculated before*/
transform.position += transform.forward * (current_speed * Time.deltaTime);
}
}
``````

You can attach it to a sphere or something to test it first. Try changing the parameters to get it the way you want.

That works quite well, although it is important to note that all loops/twists must not be too sharp or the smoothing will cause the vehicle to fail to keep up.
I’m gonna make a new test track that isn’t so sharp and see how that works out.
Other than that sick job dude!

Yea, you are right. The track geometry needs to be smooth enough.

I actually had a look at wip3out game for the PSX (quite similar to FZero) in an emulator so I could see it in wireframe mode. I found out that they used some kind of “Level of detail” trick so that only the section of the track you are in is actually smooth while the rest is really low poly. However I don’t know how that could affect the AI ships since the geometry they “see” would not be smooth at all…

It is amazing how simple these games look at first sight and how complex they can get in the inside. That’s why I loved the idea

I made a decent sized track, now there’s the next issue:
Barriers.
How would the programming be done for the side collisions if the vehicle uses no colliders?
Raycasts I guess?

EDIT:
New Issue,
Vehicle doesn’t function well upside down.
Example:
I have a tube track where you drive on the outside, and if I go down the side too far it falls off and snaps to the ground way down below.

EDIT2:
Also, if you watch the video in the original post, you can see that the vehicle is unaffected by the low poly tube it drives across. There has to be another way to do this.

BTW Thanks Nevak for all the input

See my video and code at the bottom of that url. Pretty much the same concept. Use Sphere cast of appropriate sizes 50-100 % radii of vehicle for single cast 25-50 % for multiple casts. You can also code a backup rotation position in case all of the casts fail.

I would use colliders personally. Put barriers in a different layer or tag them and use the colliders or raycast to the sides and check the tag or layer and code the response accordingly.

Handle your own gravity and have a sufficiently large downforce. Look at the link above to see a discussion and my example video and code for help. Using a larger sphere radius than the vehicle for a sphere cast can eliminate fall off issues.

Using multiple sphere cast at locations similar to wheels and averaging them together then Lerp or Slerp with appropriate damping would handle just about any surface.

You gotta remember that games like that took many, many hours of tweaking and trying to get things to work that way. I hope I helped

Look at my video and see how using a sphere cast can make even angles equal to or sharper than -90° and 90° still work.

Not really an F-Zero example but some hovercraft code I had laying around using 4 raycast at the corners used as hover thrusters. Then smoothing the rotation to the ground normal below the craft. One could combine the Magic Boots and hover code to create an F-Zero like game.

``````		for(int i = 0; i < stabilizers.Length; i++)
{
if (Physics.Raycast(stabilizers[i].position, -stabilizers[i].up, out hit))
{
thrustRatio = Mathf.Clamp(hit.distance, 0.1f, 5.0f);
rigidbody.AddForceAtPosition(stabilizers[i].up * ((hoverForce + thrust) / thrustRatio), stabilizers[i].position);
}
}

if (Physics.Raycast(transform.position, -transform.up, out hit))
{
thrustRatio = Mathf.Clamp(hit.distance, 0.1f, 5.0f);
rigidbody.AddForce(transform.up * ((hoverForce + thrust) / thrustRatio));
if (hit.distance < 2.0f)
{
rotationVector = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal), hit.normal), Time.deltaTime * 7.5f);
}
else
{
rotationVector = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal), hit.normal), Time.deltaTime * 3.5f);
}
transform.rotation = rotationVector;
}

transform.rotation = new Quaternion(Mathf.Clamp(transform.rotation.x, -0.35f, 0.35f), transform.rotation.y, Mathf.Clamp(transform.rotation.z, -0.35f, 0.35f), transform.rotation.w);

transform.rigidbody.velocity *= 0.99f;
transform.rigidbody.angularVelocity *= 0.99f;
``````

So after playing F-Zero some more, I noticed that the front of the vehicle clipped through the track when starting a small loop.
That gave me the idea of having an invisible cube running the code by Nevak without any rotation smoothing, and having the actual vehicle set it’s position to the cube’s and lerp the rotation.
It almost worked.
I put this code on the vehicle

``````#pragma strict
var target : Transform;
var speed = 15;

function Update () {
transform.position = target.transform.position;
var anglex : float = Mathf.MoveTowardsAngle (transform.eulerAngles.x, target.eulerAngles.x, speed * Time.deltaTime);
var angley : float = Mathf.MoveTowardsAngle (transform.eulerAngles.y, target.eulerAngles.y, speed * Time.deltaTime);
var anglez : float = Mathf.MoveTowardsAngle (transform.eulerAngles.z, target.eulerAngles.z, speed * Time.deltaTime);

transform.eulerAngles = Vector3(anglex, angley, anglez);
}
``````

I’m not too surprised it didn’t work, as most things I code usually don’t lol.

I wanna use raycasting on the edges like in my initial attempt, but if you’re going too fast and in one frame the vehicle is on the track, and in the next it is a little bit inside it, everything would goof up.

I have tried so many different things trying to get this to work I’m convinced it neither uses unity physics or raycasts. It probably uses splines to keep the car on the track. I noticed that on tube track, it has 6 sides. I replicated that in unity, and used raycasting to stay on track, and the angles were way too sharp to work. Even when I was going at a slow enough speed to stay on, it was not a smooth effect going around it no matter how much I mess with the tilt/pitch smoothing. In F-Zero X, the car always stays at the same distance around the track so it has to be rotating around a central point. and for such a point to exist, to move along a track would mean it needs some sort of path or curve to follow.

Movement
If I were to move an object straight forward, it would travel straight along the z axis. If I wanted to move it along the spline then I would treat the distance of my point on the spline the same as I would the z axis. for normal track, your point on the spline also has a rotation so you would just use the x direction of your point as the ships local space x axis. For tube track, you would just rotate your ship around the spline.

Walls and falling
You could also set a min and max x distance for normal track. Then if you go beyond those limits, your ship would detach from the spline and fall. You could also simulate walls by not letting it go beyond your limits. No need for that on tube track. You could also set zones along the spline so that if you are going at a high enough speed you would detach from the spline like going off a ramp on normal track or a sharp curve on tube track. You could also re-attach to a spline by getting close enough to the track. you would just need to create an invisible mesh around your track to act as an attach zone.

I haven’t been able to test this theory yet, but I will soon.

Here’s how to make splines: Curves and Splines, a Unity C# Tutorial

I used to play this game a lot…and by a lot I mean a lot…I am interested in giving this a try…

Me also…

Hoping to have a solution for this.

Perhaps F-Zero X wasn’t made using the same effect, but you can achieve similar thing with Curved World asset.

If I understood correctly, it is a shader, as it is stated in description “per mesh material shader effect”. It basically curves your camera output, meaning your level is flat and you don’t have to worry about those problems like curvature.

I haven’t tried it, but it seems the reviews are great. And it looks quite promising. Hope this helps!

i’m pretty sure fzero keeps it simple with gravity always in the direction of the line segment, and distance to line segment + offset = floor height (for the tube bits).

I really don’t think it bothers with any form of raycasting on such limited hardware.

I did this entire thing before based on another test game in unity. As a matter of fact it operated almost exactly like this. I did 3 rays from the base of the ship and calculated down on whichever rays collided with the floor. if none of them did, then I assumed you were out in space so I applied gravity. other than that gravity was the direction of the calculation of rays. It looked exactly like what you have there.

1 Like

Sorry for reviving this dead topic, but are there any appropriate solutions yet?

I might be late to this. I am currently working on an anti gravity racer and figuring stuff out

By interpolating between vertex normals you can get the movement pretty smooth.

Now I am working on getting a constant height above the track

8 Likes