# ApplyImpulse to an object, relative to another object?

My setup so far is working:
I have a small object controlled by the player, this object needs to use what could be described as a ‘tractor beam’ on a larger object. The smaller object shoots out a ray when commanded by the player, and if that ray hits the surface of the larger object a force is applied at that point.
Ive discovered applyImpulse is best for this, since it cant work at a point on the object away from the center and will apply a rotation if not directly in the center (exactly what I want)

The problem is the force isnt being applied ‘relative’ to the player object. Right now I only have the force hard-coded to apply at the correct point, but on the Y axis only, so it will always only pull the object along that axis right now, which doesnt work if the player object is trying to pull from another angle.

Im trying to work out how to do this properly, so the larger object is always being pulled towards the player object. The below image might help illustrate the problem better?

On the left is how it works now, on the right is how I need it to work. The smaller object is the player, the larger is the object to be pulled, the red arrow is the force and its direction, the green is the ray being shot by the player to determine the position of the force.

Does anyone know the math/technique/function(s) to do this correctly?

I haven’t tested this so it might not work but its an idea.

Maybe use this for the impulse force:

``````
force = mathutils.Vector(0,1,0)
force.rotate(own.worldOrientation)

#or if your using a rayCast()

force =  mathutils.Vector(0,1,0)
force.rotate(own.worldOrientation * (ray[1] - own.worldPosition) )

``````

Again I don’t know if this will work, but its an idea. You’ll also have to import the mathutils library

``````force = magnitude * (player.worldPosition - hitpoint).normalized()