# Help with vectors / combining two chunks of script

Hi all

In my game I have one annoying visual bug that is a bit beyond me- I know what is causing it (roughly) but I have no idea how to fix it. Here are the two chunks:

The first finds the nearest AI player and simply nudges itself away from it to avoid crashes:

``````

targetList = [tar for tar in Radar_Emitter.hitObjectList if 'racer' in tar]
minValue = 400
for tar in targetList:
distanceTo = ow.getDistanceTo(tar)
if distanceTo &lt; minValue:
locked_target = tar
minValue = distanceTo
ow["current_Target"] = locked_target
(dist, glob, loc) = ow.getVectTo(ow["current_Target"])
ow.alignAxisToVect(-glob, 1, 0.006)

``````

The second (donated to me by MarcoIT) banks the AI towards the target (an object in the scene used for navigation):

``````

node  = target
NP    = node.position
OP    = ow.position
vec   = NP - OP
up    = Vector((0,0,1))
cross = vec.cross(up).normalized()

target = NP + cross *10 # the distance to the node
vec = target - OP
ow.alignAxisToVect(vec, 1, 0.05)
ow.alignAxisToVect(up,  2, 0.02)

vecLoc=ow.getVectTo(target)[2]
vx, vy, vz = vecLoc
velRot = 0.05
ow.applyRotation((0, vx*velRot, 0), 1)

``````

Now, both work really well…in isolation. What happens in game is that they fight against each other, and AI objects ‘jitter’ as they switch between alignments (as far as I can tell). If I disable the line in the first chunk…

``````

ow.alignAxisToVect(-glob, 1, 0.006)

``````

…the jitter stops. Is there a way to combine both these bits to smooth things out, or am I barking up the wrong tree?

Paul

You’re looking for a steering solution. I’ve implemented some example code from http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-collision-avoidance--gamedev-7777

steering.blend (614 KB)

Nice! Thanks for that- your skills are scarily good! Here it is working in my demo:

It works well- its only downside is that it eats the CPU (almost double of the buggy code I used).

Will have to tinker some more…

I suspect you’ve left the drawLine calls in the code. This slows down execution remarkably. You might also want to modify the list comprehension such that it treats other agents as obstacles. Simply give the agents an “obstacle” property, and modify the list comprehension to ignore obstacles if they are “own” (o for o in scene.objects if “obstacle” in o and o is not own)

Update:
steering.blend (779 KB)
There were a number of faults with my original implementation. I was careless with sizing a vector, and I reworked an obstacle collision test which was amiss.

Fantastic! Thanks for updating this, you did not have too.

Again it works well, although I had to do some basic modifications to get it to work in three dimensions (deleting the z velocity value, changing the own.mass to a property).

A nice picture to finish:

As a sidenote, perhaps you should post this file in a more visible resource area / section? For people making racing games this is very handy indeed.

Are those lines drawn as part of your program? They will contribute to significant overhead.