Copy Rotation of (nearest empty with 'x' Property)

Ok, so:
I can copy the rotation of an object via python in BGE (thanks to you good folks;))

But, how do I:

  1. make it only copy/apply 1 axis rotation (y local for example)?
  2. only from objects that have a certain property?
  3. that is the closest out of all that have that property?

(QuestionCombo lol),
Any help you all can provide will be greatly appreciated!

Thanks :slight_smile:

1:


eu_own = own.worldOrientation.to_euler()
eu_target = target.worldOrientation.to_euler()
eu_own.y = eu_target.y

own.worldOrientation = eu_own

2:


objects_with_prop = [o for o in scene.objects if "prop_name" in o]

3:


dist = lambda o: (o.worldPosition - own.worldPosition).magnitude
object_distance = [(o, dist(o)) for o in objects_with_prop]
object_distance.sort(key=lambda t: t[1])
closest = object_distance[0][0]

Putting it together should be a good exercise for you.

Thanks for your reply Goran!
I’ve spent a few hours putting this together and debugging error messages (took me a while to figure out what each one meant, but I did it :slight_smile: ).

But I still did something wrong though, because it’s not copying the rotation. (and there is no error message telling me why)

Here is the .blend, I think maybe I did something wrong on the logic side?

(W,A,S,D to move the cube)

Attachments

copy closest rotation.blend (652 KB)

The file you posted does raise an error at line 19 since you left target as a list instead of a game object. You need to set target to an individual object from the list before you use target.worldOrientation.

YES!!! It WORKED!!!

Thank you both Goran and Mobious!!!

Here is what I did.

import bge

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner

# objects with property
objects_with_prop = [o for o in scene.objects if "CopyMe" in o]
target = objects_with_prop

# closet
dist = lambda o: (o.worldPosition - own.worldPosition).magnitude
object_distance = [(o, dist(o)) for o in objects_with_prop]
object_distance.sort(key=lambda t: t[1])
closest = object_distance[0][0]
target = closest

# copy rotation
eu_own = own.worldOrientation.to_euler()
eu_target = target.worldOrientation.to_euler()
eu_own.y = eu_target.y

Thank you two for your help!

side note: the reason I wasn’t seeing the error line was because I had multiple blend files open and I was looking at the wrong console :stuck_out_tongue:

Attachments

copy closest rotation.blend (648 KB)

Nice try. It seems to do some kind of something , but it’s not really working properly. It’s not doing a true copy of the closest rotation.

Try this one. Is this what you were trying to do ? It would be nice to interpolate between transitions.

Attachments

copy closest rotation.blend (647 KB)

Here is another version without the near sensor.

Attachments

copy closest rotation without near sensor.blend (145 KB)

Thanks blenderer2012, my file was behaving odd as a result of the Euler rotation, I changed it to quaternion and it behaved better.
Euler has this odd hate for 360 degree rotations, it can’t seem to interpret how the object should reach a particular point in rotation.

I needed to copy only one axis because I need the other axis (axises? axies?) to be influenced by other things.
So what I have now is this:


import bge

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner

# objects with property
objects_with_prop = [o for o in scene.objects if "CopyMe" in o]
target = objects_with_prop

# closet
dist = lambda o: (o.worldPosition - own.worldPosition).magnitude
object_distance = [(o, dist(o)) for o in objects_with_prop]
object_distance.sort(key=lambda t: t[1])
closest = object_distance[0][0]
target = closest

# copy rotation
eu_own = own.worldOrientation.to_quaternion()
eu_target = target.worldOrientation.to_quaternion()
eu_own.z = eu_target.z
eu_own.w = eu_target.w

own.worldOrientation = eu_own


Euler has been such a headache because it doesn’t do 360 degrees (some years ago, I screamed at my monitor in rage as I attempted to make my character spin)

Knowing this now, I don’t use Euler because it produces odd results like you saw in my blend, you guys gave me the code and I simply fit it to my needs :slight_smile: (I didn’t think to post my corrected blend lol)

Thank you for your help :slight_smile: