[HELP] [BGE] Car AI

Hi,

I need some help with a game i am making i have multiple cars with suspension, 1 of the am i controlling the other will have an AI.
The AI is what i need help with. I have some ideas but do not how to work it out.

My idea is: Track object and calculate how to get there then drive to there with the vehicle setup.

Try the inbuilt navmesh system. Anything wrong with that?
It may do funny things (such as the car turn around on the spot), but the only other way to do it is to write your own A* (or other) implementation.

Depending on which vehicle setup you’re using, you may be able to set variables to make the vehicle accelerate, brake, and steer. Very useful when coding an AI.

I agree with sdfgeoff, if you wish to make your AI car navigate properly, and you aren’t a programming wizard, you’d be best advised to use the default navigation system in conjunction with the steering actuator.

However, I’d like to clarify that the steering actuator is setup more for character AIs, so you’ll get glitches like turning in place, and accelerating/stopping instantly if you use it on the car itself. The solution is to use another object to direct the AI vehicle. Code it so that it keeps its distance close-but-not-too-close to the car, and so that the car can calculate the angle to the object (you’ll need that for steering and reversing). There’s a lot of complex explanation in that statement alone, but it should be enough to start you off. Don’t hesitate to reply if you need clarification.

Thanks for the replays I will work on it ASAP.
:slight_smile:

Okay sorry for waiting so long but I can’t get it to work.
If someone has some examples, can I have them?

The car AI can accelerate and brake but can’t let is steer to the target

bump… :frowning:

Put collision sensor and two boxes that are targets.when the car collide with one of the boxes it will cause the car to switch states.Making it target another the other box with the steering actuator set to path follow.Please do try to figure this out.
It you can’t figure it out.I will provide provide a blend.But only tommorrow.Look at the Tutorials for Blender 3d.Okay i will give it to you today.Here is what i think you want.http://www.pasteall.org/blend/32727
If not tell me.

Hi,
well, i suppose you use the veicle wrapper constraint, downloaded from tutorialsforblender3d.com (great site), right?

so, the first thing to do , is separate the code of imput (keyboard pressed) to the code of the constraints setup(powertrain.py)

do a new script , (imput.py)
where you pass the values of the keyboard , to a property .

example:


#imput.py
    if sensors["Gas"].positive:
        own["Gas"] = True
    else:
        own["Gas"] = False

then modificate , powertrain.py… from this:


#powertrain.py
    if sensors["Gas"].positive:
        setconstraint..

to this:


#powertrain.py
    own = controller.owner
    if own["Gas"] == True:
        setconstraint..

so, powertrain is not more dependend from the keyboard , but from the property

these properties can be written or from the player or from the AI
while powertrain.py react to these property exactly in the same way

2 worlds about the AI .

the AI need to have a target (or a point 3d)
and calculate where it is , forward/back,left/right

you can get this info using this function:


distance, vec, vec_loc = own.getVectTo(target)


#the matter is the last
x,y,z = vec_loc


if x < 0 ... target is at left
if x > 0 ... target is at right


if y > 0 ... target if forward
if y < 0 ... target if back

based on that , write the property that then are readed in powertrain.py

choose what seem better :
own[“steer”] = “left”
or:
own[“imput_x”] = -1

a number is more flexible but also less readable

in all case , the property written from the player must be exactly the same that write the AI

so, start before to write the player , then do the AI

Thanks @3d_solar_system_buil,
but i know this but i want this with the vehicle setup script

To determine how much the car should slow down in order to take a curve without going of track you should know the angle to which the car has to turn next, after reaching it’s first target. I would suggest to use an armature to define the path. It’s just a theory.

Have a ray cast sideways, label each face with a property, turnMagnitude

The higher the value, the slower the car should go?

Have a invisable fence that has the value in it?

Use the value, and some kind of handling value in each car?

I think using bones is much easier. When the game starts you could get an array of coordinates from the armature and then delete the armature. It is just a workaround for extracting coordinates from a spline or array of vertices in the Outliner.

That is pretty niffty, never really messed with armature api very deeply

How about the front wheels with a limit rotation (local) and a track to acuator.
and the car slows down depending on the wheels rotation

The easiest way to do this, is a ray hitting the track, and a property in the track brake level,

Use the property to apply the brakes,

So your track is broken up into many sections, and each section has this property set in it.

BluePrintRandom do you have a example on that?

Ray---------python


import bge
cont = bge.logic.getCurrentController()
own = cont.owner

Ray = cont.sensors['Ray']

if Ray.positive:
    own.localLinearVelocity.x*=Ray.hitObject['brake']

Set the Ray to look for objects with property brake in them

.99 = 1% speed reduction

.5 = 50%

Per frame! So .99 through .95 should be about right,

example in a jiff

Attachments

Brake.blend (420 KB)

Thanks BluePrintRandom if the AI is near a turn it brakes :slight_smile:
But do you know how i can let him steer because with a trackto sensor it does nothing

I like Linux too:)