Motion in relation to specific objects

I know that with servo control you can say “apply force along the x axis
of this certan object.”

Is there any way to do this through python?

There are a few ways, for a few scenarios. What type of object do you want to move?

Do you mean movement with Python or movement based on the position and movement of another object?

If you mean simple movement, then you can just use myObject.setLinearVelocity([x,y,z],local)

If you want to base it off of another object than you can use that function with variables obtained from another object like this…

scene = bge.logic.getCurrentScene()
objList = scene.objects
reference = objList['ReferenceObject']

obSpeedX, obSpeedY, obSpeedZ = reference.getLinearVelocity()
obPosX, obPosY, obPosZ = reference.worldPosition

#do some math and calculate final variables#


this suppose that you have 2 obj one static(“S”) and one dynamic(“D”)
the script must run on the obj dynamic (always true of course)

# -elastic simulation- #

import bge

cont = bge.logic.getCurrentController()

D = cont.owner              # obj dynamic
S = D.scene.objects["S"]    # obj static

maxDist = 10.0
elast = 0.05
vec = S.worldPosition - D.worldPosition
dist = vec.magnitude
if dist > maxDist:
    D.worldLinearVelocity += vec.normalized() * (dist-maxDist) * elast

# -elastic simulation- version easy #

import bge

cont = bge.logic.getCurrentController()

D = cont.owner              # obj dynamic
S = D.scene.objects["S"]    # obj static

elast = 0.05
vec = S.worldPosition - D.worldPosition
D.worldLinearVelocity += vec * elast

I just want object “A” to move along the axis of object “B”. If object “B” rotates, then object “A” would move differently. basically, local space for object “A” would be defined by the rotation of object “B”. Can I do this without editing the bge module?

Easily enough.
Get the vector of object b with:

angle_vect = objB.getAxisVect([0,1,0])

And scale it to how fast you want to move:

speed_vect = angle_vect * 5 

Or by another vector for more complex motion:

speed_vect = angle_vect * [0.5,1,0]

And finally apply it as a motion:

servo_actuator = ....
servo_actuator.linV = speed_vect

something like this ?

PS: note that if obj “A” is a children of B you can use directly “localPosition”
that is the local space (already calculated, but you cannot use velocity or force)


moveUsingVectors.blend (261 KB)

Thank you!
is there any way to use . * LinearVelocity with this? I know not of vectors… also, what’s the .col? I did some research and couldn’t figure it out. and by research I mean I spent five minutes on the API and stack overflow.

*local or global or whatever works in this case

yes, this seem give the same effect (PS: the magic number 0.016 is 1/60 )

    #A.applyForce(vecForce ,0)
    A.worldLinearVelocity += vecForce * 0.016

“.col” mean “column” ,

as say, this is a matrix 3x3
[x0, x1, x2]
[y0, y1, y2]
[z0, z1, z2]

with obj.worldOrientation.col[1] return [x1, y1, z1]
that rapresent the direction of the axis Y(green arrow) of the obj
(depend from the order of the matrix rapresentation,anyway
before of 2.62 version was different the order):wink:

this is the equivalent anyway :

vecY = obj.getAxisVect([0,1,0])