Python rotation

Hey all,

I already had a file with this once, but for some reason I lost it.
I need a cube to rotate along with the flour. Needs to be a static object though and instant rotation. Like below.
Hope anyone can help.

Thanks a lot ^^


This should be a job for the S2A.

Make a ray sensor along -Z -> use S2A.hitNormalToAlign as Python Module controller ->connect with any actuator.

Uhm, I can’t downlaod the file and to be honest… I don’t really understand what you mean sry

I do not know why you can’t download the file. It is attached to the thread.

A ray sensor along -Z means the object sends a ray below it. When it hits something the controller will be activated.

If you set the controller to S2A.hitNormalToAlign it will align the object(s) of the connected actuators to the normal of the hit face.
The actuators itselfes will NOT be activated. They are just present to identify the objects that should receive the orientation.

See this picture to find out what the hitNormal is:

You can write your own script just with:

# assumption cont = current controller
hitNormal = cont.sensors["ray"].hitNormal # or whatever name the sensor has
if hitNormal:
   cont.owner.alignAxisToVect(hitNormal, 0, 1.0)

Wow, someone should give you a medail for your work at blendartists. :stuck_out_tongue:

I’m sorry to tell you though that it’s for some reason not working. The cube turns a few rounds really quic and then does nothing. o.0

Oh, wait it does work. But not if you change the rotation in edit mode… and to be honest that’s really what I need sry.

Ah wtf sry for all the posts but 1 time it works a very bit and the other time it won’t work at all

In BGE there is no edit mode :slight_smile:

Maybe you should switch on true pulse at the ray sensor.
Keep in mind the ray must hit something.

I’ll upload a .blend file. :stuck_out_tongue:
It might look fcked up because I’m a real newb in python. (A)


ray.blend (130 KB)

small change:

cont = GameLogic.getCurrentController()

# assumption cont = current controller
sRay = cont.sensors["ray"]
if sRay.positive:
    hitNormal = sRay.hitNormal # or whatever name the sensor has
    cont.owner.alignAxisToVect(hitNormal, 2, 1.0)

other axis to align to plus a different sensor check.

This should work.
Please keep in mind, if the object is rotated (because of an earlier run) the ray will ot point down anymore. This is because the -Z ray is local and will rotate with te object.

Hmm, thanks a lot. ^^ it works.

It’s very shaky though when I try it on a sphere. but thanks a lot monster. :wink:

the shaky bit might be because everytime the object rotates, the ray hits another part of the ground.

Try the attached example.
I added an empty that is vertex parented to the cube.
This makes the empty not rotate with the cube.
The empty sends the ray along -Z. Because it does not rotate it is always down.

Unfortunatly the ray will reognize the cubes faces. So I made the ray to an x-ray. That means it will not recognize any face. So let it detect faces of objects with a specific property. In this case “ground”.

Give all objects the cube should be aligned to the property “ground” and it will be fine.


ray2.blend (132 KB)

I love you man, i’m serious. xD
The only thing I need to know is how you vertex parented the cube to the empty? Cause when you move the empty the cube won’t move.
I can still do this with 2 cubes, if i’m correct. But i’m just curious. :wink:

Thanks so much already!