Aiming ahead of moving targets

Time for an upgrade in my game, this time it is in the AI.
Previously they have been simple track to actuators, which is OK, but you circle strafe and shoot, and find they are too easy.
So, I want to find out about aiming ahead of moving targets.

I have run a few searches and have returned various formula, but consider them in-suitable. Here’s the problem:
In blender added objects (ie: bullets) don’t inherit the velocity of the parents. In real life they do.
The result is that you have to anticipate this as well as the movement. So any of the formula that I found wouldn’t work.

Does anyone have an amazing way of doing this?

you can do this:


def copyVelocity(source, target):
'''
source = gameObject to copy from
target = gameObject to copy to
'''
  target.setLinearVelocity( source.getLinearValocity() )
  target.setangularVelocity( source.getAngularValocity() )
 

now you need to bring addingObject and addedObject together:


# assumtions: 
# addObjectActuator = your actuator adding the object
# own = the adding object
addedObject = addObjectActuator.objectLastCreated
addingObject = own
copyVelocity( addingObject, addedObject ) # do this only once after creation!

I hope it helps

BTW. They will not aim much better, because they still target the players old position. But together with your formulas it would better.

So if I am right this is a way to get a bullet to have the starting velocity of the object that it was launched from. Without any logic in the bullet itself?
Great, thanks.
I’ll try with this.

Right, I’ve run into few problems with this. The first few were just typos, but now, with the following code I have hit a problem.

#Setup...
import bge    

#Shortening names                   
cont = bge.logic.getCurrentController()
obj = cont.owner

#Defnining Actuators
addObjectActuator = cont.actuators["addObjectActuator"]

#Defining copyVelocity
def copyVelocity(source, target):
    target.setLinearVelocity( source.getLinearVelocity() )
    target.setAngularVelocity( source.getAngularVelocity() )
    return

#Inding the bullet and gun
addedObject = addObjectActuator.objectLastCreated
addingObject = obj
copyVelocity(addingObject, addedObject) # do this only once after creation!

You can see some straight copy paste there can’t you…

Anyway, the problem is that:
you get an error:

Python script error - object 'Empty', controller 'Python#CONTR#2':
Traceback (most recent call last):
  File "Aim", line 20, in <module>
  File "Aim", line 13, in copyVelocity
AttributeError: 'NoneType' object has no attribute 'setLinearVelocity'

I have checked that the objects are dynamic, so they have linear velocity, but it seems that added objects don’t obey the same rules as everyone else.

You should check if the actuator really added an object.


if addedObject is None:
  return

or you add directly with the python code:


addedObject = addObjectActuator.instantAddObject()
if addedObject is None:
  # should not happen
  return
 
...

Have a look at the API

by the way you should check if your sensors are positive (see Double execution of a python controller )