Hi there,
I suspect this is not possible but answers I found about this date back to 2010, so maybe there is something new here.
Is it possible to update/set the pivot point/origin of an object with python code ?
Alternatively, is it possible to rotate an object relatively to another ?
In my case, I would like to be able to rotate an object moving toward the camera and set the origin of this object to the position of the camera. Manually placing the origin of the object is not an option, as its position has to be updated each frame.
something like this in pseudo code :
obj.setPivot = camera.worldPosition
Any hint ?
answer 1 = add a empty, parent object to empty -> rotate empty
answer 2 = lots and lots of scary math.
you can localize a table of offsets to the actor to orbit, and scale them, and lerp around a set order,
to turn a local into a world
Point = target.worldPosition+(target.worldOrientation*Vector([x,y,z]))
and scale
Point*=scale
but setting the order of the points to follow could get tricky,
Thanks BPR,
I think I managed to simulate the behaviour I want according to your answer number 1. (Too much maths is a no go for me :D)
Here is what I do :
Add an empty in the scene.
From python : put empty at camera position, then each time I move the_object, if a rotation applies, parent the_object to empty, apply rotation, removeParent.
I don’t know if this is very efficient, but it does the trick.
Just for completeness, here is the code :
import bge
import math
scn = bge.logic.getCurrentScene()
cube = scn.objects['Cube']
sol = scn.objects['sol']
Empty = scn.objects['Empty']
railCol = cube.sensors['railCol']
railColG = cube.sensors['railColG']
railColD = cube.sensors['railColD']
onFront = railCol.hitObject
onLeft = railColG.hitObject
onRight = railColD.hitObject
Empty.position = cube.position
EmptyOr = Empty.localOrientation.to_euler()
if railCol.positive:
if str(onFront) == str(onRight):
sol.setParent(Empty, 1, 0)
EmptyOr[2] -= math.radians(1)
Empty.localOrientation = EmptyOr.to_matrix()
sol.removeParent()
elif str(onFront) == str(onLeft):
sol.setParent(Empty, 1, 0)
EmptyOr[2] += math.radians(1)
Empty.localOrientation = EmptyOr.to_matrix()
sol.removeParent()
And the blend is attached.
Thanks for your answers ( in this one and other threads),
Cheers
Attachments
3ray.blend (503 KB)
Actually the math is not that complicated. Mathutils will do all the heavy lifting for you. It’s just a case of getting the matrix multiplication in the right order.