Smooth box to curve collision in 2D

I’m starting my game development trials with a top-down, tile-based 2D racing game like the old Super Sprint.

I’ve mapped a car sprite onto a box (which rotates and moves forward/back based on input), and made 2 different track planes, one straight and one corner. The corner tile is a curve, basically one quarter of a circle. I’m having trouble confining the car to the inside of the curve.

I am doing the following… If the car is over a corner tile, I get the origin of the circle, which is the corner of the tile in world space based on the rotation of the corner (so the north-east corner has the bottom left corner as the origin). For each of the corners of the rotated car, I check which is the furthest away from the origin. I then check if the distance of that corner from the origin is larger than the radius of the circle. If so, I want to move the box back towards the origin by the amount it exceeds the radius, so that it stays inside the curve.

This works to some extent, but every time I trigger the collision code, the car jerks all over the place, sometimes even getting stuck or jumping outside the tile. I don’t know how to make it smoothly traverse the inside of the curve.

Here is the relavant code.

Inside the car’s FixedUpdate() is this:

if (roadtile.IsCorner())
{
	// Get the corner of the tile that is the origin of the circle
	Vector2 origin = roadtile.GetOrigin();
	// The car box is 1x1, so the top right corner in local space is at (0.5, 0.5)
	Vector2 topright = new Vector2(0.5f, 0.5f);
	// Cheap way of getting the 4 corners in world space
	Vector2[] corners = {
		new Vector2(transform.TransformPoint(-topright.x, +topright.y, 0).x, transform.TransformPoint(-topright.x, +topright.y, 0).y), // top left
		new Vector2(transform.TransformPoint(+topright.x, +topright.y, 0).x, transform.TransformPoint(+topright.x, +topright.y, 0).y), // top right
		new Vector2(transform.TransformPoint(+topright.x, -topright.y, 0).x, transform.TransformPoint(+topright.x, -topright.y, 0).y), // bottom right
		new Vector2(transform.TransformPoint(-topright.x, -topright.y, 0).x, transform.TransformPoint(-topright.x, -topright.y, 0).y)  // bottom left
	};
	Vector2 furthestPoint = origin;
	float distance = 0;
	// Check all 4 corners for the furthest from the origin
	foreach (Vector2 corner in corners)
	{
		if (Vector2.Distance(origin, corner) > distance)
		{
			distance = Vector2.Distance(origin, corner);
			furthestPoint = corner;
		}
	}
	// Now check if the furthest point is outside the circle
	if (distance > roadtile.GetRadius())
	{
		// Calculate the angle and distance to move
		float angle = Vector2.Angle(origin, furthestPoint);
		float amount = distance - roadtile.GetRadius();
		Vector2 moveVector = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
		moveVector *= amount;
		// Move the car
		transform.position = new Vector3(transform.position.x + moveVector.x, transform.position.y + moveVector.y, transform.position.z);
	}
}

Vector2.Angle returns degrees (odd, but Vector2.Angle(Vector2.right, Vector2.up) is 90, not PI/2) and Cos/Sin want radians.

Another trick for “pulling in”, which doesn’t need any angles, is to scale a line segment by the percentage you’re off:

Vector2 toCarCorner = furthestPoint - origin;
float dist = ...
if(dist too big)
  // find the point on circle nearest carCorner by scaling toCarCorner down:
  float scaleFactor = roadTile.GetRadius/dist; // will be <1
  Vector2 nearestPointOnCircle = toCarCorner*scaleFactor;
  // this will be the same angle, but on the circle

  Vector2 moveVector=nearestPointOnCircle-furthestPoint;