# Car Ai - Calculate max corner speed

I have been working on a simple arcade racing ai, but the problem I have is cornering.
Before each corner is a simple waypoint telling the car the angle of the corner.
After receiving that info from the waypoint, the car will then start to turn.
Problem with this is that the car doens’t know if he has to accelerate or brake to clear the corner.

I was hoping there would be a way to make a circle based on the car’s speed and turnrate, and use that circle to see if he would go off track or not.
I’m sorry if I am being unclear about things, I’m not English so I don’t know how to explain it very well.

edit: I have followed a tutorial that uses a waypoint system… but that didn’t work. I download the completed version of the tutorial and it had alot of bugs

edit2:
See image below:

This is what I currently have. the car can now determine his trajectory with the following formula:
r = vt / 2pi (radius = speed * time to turn full cirkel / 2*pi)

Now I just need to find a way to do a circular raycast…
The idea behind this is that if the raycast will hit rectangle 1 or 2, the car will know that it is going off track.
I have tried Physics.OverlapSphere for this, but that function returns all colliders within that range, and I just want the colliders on the edge of the cirkel.

After having made this progress so-far, I don’t want to use a waypoint system, because I think if I can make this work I could make the ai much more advanced than a waypoint system in terms of overtaking, and smarter use of braking and acceleration before/during corners.

I really think you should try what I explain in the other post

But, the way to do what you’re asking is fortunately very easy … .just like this …

just cast rays down, where the yellow Xs are … you see ?

Simply start where the car is, and go forward (say 5 or 10 degrees at a time) around the imaginary circle.

You need only go say 100 degrees, and you are done, you can then drive.

Do not hesitate to ask any further questions !!!

“However do I have to determine to position of the yellow Xs?”

Of course, it’s only one line of code. Just use Mathf (see doco) for sine, cosine, etc.

An even better way to do it:

just make a “calculation” mechanism (ie, an empty game object) and sit it at the center of the circle.

Have a marker (“mmm”) that sticks out the correct distance. Simply rotate this “calculation” object, by ten degrees at a time.

Just shoot from “mmm” - it saves you doing the high-school math!

:The solution I just thought of was instantiating a gameobject at the center of the red circle and having a cube rotating around the gameobject with the radius as the distance to see if the cube would detect a collision. However if I could get your solution to work, then it would be better for the performance:"

You are truly thinking like a game programmer. Your life is set!

there are problems with that idea, it takes time! You have to swing it around!!!

Regarding the raycasts, you can do a few zillion of them all inside one frame you know?

Note too that you can be a smart-arse with a “hoop” that is on an ANGLE … move it down, and the POINT of contact is like the exact first point if moving around, you know?

If you were going to do your “swinging invisible tester” … do not forget too that …

YOU COULD SIMPLY MOVE THE CAR !!! heh! it is everyday ion game engines that you take an invisible version of an actor, and you “move it ahead” to see what it can do. You know?

(For this, you must learn about the LAYERS system in Unity3D … you know it? it’s great, read up)

But these things are not relevant here…

I would perhaps, suggest, encourage you to do “ray casting along a possible path” as it is an extremely common technique, it is everyday.

(Do not forget, it may not be the red circle … God knows, you may be trying a future path in a straight line, or all sorts of things … ideally, you construct yourself a little OO object that will be useful to you generally !!!)

Note that you might have to try four or five attempted radii, each with a few raycasts … it will take you maybe 30 raycasts … you will do a kind of “binary search” through the space back and fore to find a nice path, I guess.

One small point: “it would be better for the performance”… not true, PjhysX is incredibly efficient AND it’s always very hard IMO to guess what will be faster. Often it is counter intuitive. It could be that what we mention “swing a cat” is much quicker than what we mention “ten yellow shots” - it is really hard to say. Based on experience both are “absolutely no problem” – just go right ahead and do not think one atom about performance at this point,

Hope it helps in some way

Sol one day someone will say my drawings are good, I still wait …

PS you know this right: on the track you have drawn…

You could PRECALCULATE, like today, now, the solution for say 10 entry positions across, at 5 angles each, at 5 speeds each, you generate a space of maybe 1000 solutions. (so for each one it tells you “brake this much” or whatever), store it in some data.

So, this is definitely an approach you see being used. At run time (drive time?) just look up the closest and use it.

of course, it is more fun to have a car that works itself