AI Car Turning but taking speed into account

Not sure how I would take speed into account with my turning. I have tried deciding and minusing the targetAngle by the speed (angle / or - speed). Results would either return an insanely small number (0.000) or an insanely high one (single whole numbers - 10's or higher) which is not desired at all.

The car system uses the car from the race template.

Snippet:


local speed = controller:getChassisForwardSpeed()
		
		local pos = target.Position
		
		local targetOffset = ccar.Chassis.CFrame:PointToObjectSpace(target.Position)
		local targetAngle = math.atan2(targetOffset.Y, -targetOffset.X)	
		local adjustedTargetAngle = math.deg(targetAngle) --* 0.003  --/ math.sin((math.round(speed))) 

4 Likes

If you know the top speed of the car, you could use:

local adjustedTargetAngle = angle * math.min(1 - (the current speed / the top speed), 0.25)

The math.min is necessary, otherwise the car will have no steering at top speed. You can adjust the 0.25 value to tune the maximum angle at high speed, a smaller value means less angle


@thatguybarny1324 I made a mistake: use math.max, not math.min, I tend to mix them up, sorry about that

4 Likes

Works kinda? Ill provide a clip below. I don’t remember if it was doing this before but I am pretty sure it was so I may swell kill two birds in one.

I did also see your edit and swapped over to the intended.

Clip below. Ignore the stats. I tried to show the tires moving to help visually it.

2 Likes

I think I understand the issue a bit better now: The steer angle also depends on the thumbstick or joystick’s position

Essentially: If the car is traveling at top speed and the player tries to steer it at full lock, you wish to reduce the angle so that the car doesn’t spin out, but if the player is slightly steering, you’d like the angle to be largely unchanged

I’ll try to think of a formula that would take this into consideration :slight_smile::+1:

1 Like

Your looking at the player car not the AI one which are both completely different.

Has nothing to do with players car just ai.

1 Like

Nothing in the main post or the reply mentioned the AI car, which is why I assumed that the issue was with the car being driven by the player

2 Likes

Oh right my fault for not mentioning that sorry. Now you’re aware at least.

2 Likes

How would you like the AI to steer the car, do you want it to steer less when traveling at low speeds? If so, modifying the formula will achieve the desired result:

local adjustedTargetAngle = targetAngle * (the current speed / the top speed)

You can use math.max if you’d like the AI to be able to steer slightly if the current speed of their car is 0

1 Like

The idea is when it is at a higher speed it will be able to take I suppose sharper turns if that makes sense?

So for example if it is going 50mph and the calculate angle without the speed being taken into account is something like 0.255 it will take into the account of the speed and adjust accordingly. Something like that anyway I have a lot of things to think of and write as its going to be similar to the NFS games.

Its kind of hard to explain as I am thinking of multiple things and fighting sleep deprivation so bear with me.


I got you. Doing this causes the car to endlessly spin in a circle. We should keep the value under 0.0 preferably in the 0.00’s but no lower otherwise we can’t steer then. Sorry if I sound like I am writing to a bot.

2 Likes

In real life, cars use the ackermann steering formula (more accurately, a percentage of it) to calculate the angle that the wheels need to turn. A car with a high turn radius is unable to turn as sharply as one with a smaller turn radius (but is more stable at high speeds), and a car’s turn radius depends on its wheelbase, track width, and whether or not is has 4 wheel steering (plus other factors)

The formulas are:

angle of inside wheel = atan(wheelbase / (turn radius - 1/2 of the track width))

If the car is turning left, the inside wheel represents the front-left wheel

angle of outside wheel = atan(wheelbase / (turn radius + 1/2 of the track width))

You can multiply the answers with a coefficient to adjust how close the car’s steering is to the true ackermann angle (the coefficient will need to be the same for the inside and outside angle), and adjust the turn radius’s value until the car turns as sharp as you desire

Just in case you don’t know: A car’s wheelbase is the distance between the center point of the front wheels, and the center point of the rear wheels, and in this case the track width is the distance between the center point of the front-left wheel, and the center point of the front-right wheel

If you set the turn radius to a value that happens to be <= 1/2 of the track width, the resulting angle will be 90 or negative, which is unrealistic (and if negative, results in the steering being inverted), so make sure that the turn radius is greater-than 1/2 of the track width

Actually the value needs to be greater-than 0 in-order for the car to still be able to steer, if the value is less than zero the car will steer opposite to the desired direction

3 Likes

So calculate the value then get the turn radius which I assume would be the same as the target offset and then add half of the track?

Thanks for adding this elaboration I was confused for a second.


Right. So I would pretty much just get the track calculation, compare the target angle (I say offset sometimes to refer to the code) and then after doing the math before hand and this last it should be good in theory?


I assume your talking about the Ackerman steering? The same is true with the throttle they both work with less than 0 inputs even ones that are 0.xxxx. Otherwise please elaborate im slightly confused?


I am actually learning something so thanks.

2 Likes

It would be best to measure the wheelbase and track width rather than to try to calculate it. When I make a car that uses ackermann steering, I use a part as a ruler to do so, and so far that has worked quite well

You can choose the turn radius’ value yourself: If the car needs more high speed stability, increase the turn radius (this comes at a cost of worse slow speed cornering), and if the car struggles to corner at low speeds, reduce it (although as mentioned before, make sure that it’s never less than or equal to 1/2 of the track width)

Since the track width is constant, you can do something like this to make sure that the turn radius is always within the correct range:

local trackWidth = 16

local turnRadius = math.max(32, trackWidth / 2) -- 32 is the turn radius of your choosing

For a value to be less than 0, it needs to be negative. As an example, a value of 0.000001 still counts as greater-than 0, even though it’s very close to 0

1 Like

So where would you start with the measurements? Front middle or? And to where, each wheel?

1 Like

This image explains where you should measure the track width and wheelbase (ignore the center of gravity part, though):

2 Likes

Guessing:

  • black = part
  • blue/yellow = width to get from each
  • red is obvious
1 Like

This new picture should hopefully explain it a bit more clear:

The green line represents the track width, and the blue line the wheelbase

By the way, you only need to measure the track width of the wheels which steer, in this case

Unfortunately it’s getting quite late where I live, so I won’t be able to continue helping you until tomorrow


@thatguybarny1324

I’m back now :slight_smile::+1:

Since the car is being driven by AI (and assuming that you’re using PathfindingService to move them), you could get the angle and distance between the current waypoint and the next waypoint, which would allow you to calculate the radius of the turn, thus allowing you to adjust the angle of the wheels using the ackermann formula

2 Likes

Makes a bit more sense thanks. And that’s fine I can wait. its 7:15 here anyway.

2 Likes