[HELP] [BGE] Car AI

NavMesh steering actuator and checkpoints,

Okay I will try it (I am not so good in python or navmesh)

If I try to manupulate the wheels they won’t move Any idea to turn the wheels outside of Powertrain.py?

I can try to explain this the way i did it playing around with an air-racing thing I was playing around with. I set up my original aircraft(car) to fly using the physics that i want. Then I gave my AI planes the same physics(so that they have to actually fly as well). I have them target to checkpoints around the course(which is a random of scattered checkpoints at one checkpoint). Checkpoint 3 may have 4 options. This is to keep all the AI from choosing the same exact checkpoint so that they wont all run into each other. Then the plane will bank until its checkpoint is being headed toward. I give it large margins so that the planes can have some distance up or down or left and right so they dont collide heading to exactly the same checkpoint with 0 tolerance. The result turns out pretty well. Sometimes the planes collide which sends them crashing. Which is what I figure planes are supposed to do. The point is I have each vehicle use the same physics as my own, and just point them(with tolerances to a checkpoint) it works pretty well, and since all the planes have the same physics as mine, it makes it difficult to beat them. Heres of video of a lap of a race. You can see the checkpoints and stuff.

yes, but with weiclewrapper is much less simple :wink:

  1. you had to applystuff to 4 wheels (rather than one obj)
  2. overall each applystuff is permanent, so, doNothing() mean -> setEverithing at value 0.0

so you have to do function redundant as
turn_nothing()
gas_nothing()
brakes_nothing()

(clearly permanent stuff is good for other stuff as suspensions :wink: )

How did he do it?

Bump… :confused:

Serious no one can help me?

If you get the linear velocity of the car, you can fake it by rotating the wheels by a set amount as the car speeds up- e.g. for every meter / blender unit the car moves forwards / backwards a wheel will do one rotation.

This would need to be done in Python using http://www.tutorialsforblender3d.com/GameModule/ClassKX_GameObject_14.html and then using this value on the wheels with http://bgepython.tutorialsforblender3d.com/GameObject/applyRotation .

Thanks Rubbernuke but the carSetup.py wont let me rotate a wheel. And in the powertrain script i see

vehicleID.setSteeringValue(turn, 0)

Is it possible to use that, if so what is vehicleID?

I think your view on the problem is a bit twisted. At that stage you do not think about each single wheel.

The AI operations are: turn, gas, brake

That are the parameters the AI can play with to meet the expected goal.

Obviously these operations do not include a “met my goal”, “set me to the target location” or other operations. So you need a series of timed operations (instructions) to get what you want.

Option A): pre-calculate the instructions (similar to path finding) = follow a path
Option B): decide on-the-fly what to do next = follow a strategy

The steering actuator can’t be used directly as it will turn the target on place, which is not the case for a car.
But how about that: Let the steering actuator steer an empty (path-following). The empty does not have the vehicles restrictions and can turn on place. The empty acts as navigator which shows the vehicle where to go next. The vehicle follows the empty via strategy (e.g. when the empty is ahead = forward, if the empty is left turn left …).

I recommend a feedback e.g. if the distance to the empty becomes too large - the empty stops until the vehicle comes near. The empty should try to stay in a place where the vehicle can reach it.

This does not cover all situations. You need to think about them too. E.g. what if the empty is behind the vehicle (either you place the empty in front or you let the vehicle perform a u-turn). What happens if the empty is too near and too much left to be hit with full left turn.

Thanks Monster its good idea but do you or anyone have a example on how to steer?
I can’t rotate the wheels.

if the car is 4x4 the brakes can also be replaced by gas_reverse (for the AI)

in this way you had just to know where the car should go:
left,right
forward, back

this require that you do some function in a way that you write car.left() (rather than make a loop for set all constraints)

this is what i use


import bge
from imput import keyboard 
kb = keyboard.active


def go():
    
    own = bge.logic.getCurrentController().owner
    cd = own["car_driver"]
    cd.reset_all() 
    
    
    cd.grip_low() 
    
    if kb("leftctrl"):
        cd.brakes()
        
    else:
        if kb("uparrow"):
            cd.gas()
            if kb("leftarrow") or kb("rightarrow"):
                cd.grip_high()
                # otherwise not turn
                
        if kb("downarrow"):
            cd.gas_reverse()
            
    if kb("leftarrow"):
        cd.left()
        
    if kb("rightarrow"):
        cd.right()
    
    if abs(own.localAngularVelocity.z) > 1.5:
        cd.grip_low()
        # drifting control
        
go()



Here’s an example blend I made. The basic principle is that the car will be able to move forward to the target as long as the target lies outside of the circle defined by the car’s turn radius. Otherwise you will have to back up to reach the target. There’s a bit of math involved, but it’s not to complicated. You can make the circle object visible to get a better idea of what’s going on.

car_ai.blend (112 KB)

Note that it’s obviously possible to make further improvements depending on how you want your AI to behave. For example, it would be faster in many cases to just back up to the target while the current implementation always goes forward if it can.

Thanks Mobious i got it finally working :smiley:
I edited your ai script so it can accelerate and steer using the wheels

Download:
CarAI.blend (1.74 MB)