# Align to 2 Ray hitNormals

Check out the blend. I got things to work, but no the way I wanted. I am trying to get the cube’s Y axis to match the average of the 2 rays hitNormal along the Z. I added a rayD for making the cube stay a distance above the ground. But that doesn’t work either. Suggestions?

Thanks

EDIT: fixed a few things

``````import bgeimport mathutils

def ave():
cont = bge.logic.getCurrentController()
owner = cont.owner

rayL = cont.sensors['Ray']
rayR = cont.sensors['Ray1']
rayD = cont.sensors['Ray2']

left = rayL.hitNormal
right = rayR.hitNormal
ave = ((left[2] + right[2]) / 2)
aver = rayD.hitNormal[0], rayD.hitNormal[1], ave

owner.alignAxisToVect(aver, 2, .05)
#owner.worldPosition[2] = rayD.hitPosition[2] + 2

``````

### Attachments

align2Rays.blend (416 KB)

I think you need this equation

(x1+x2/2,y1+y2/2)

this gives the median of a triangle

Hey! That worked. Thanks:cool:

I had that in there earlier, too. :\

I can’t get the owner.worldPosition[2] = rayD.hitPosition[2] + 2 to work now

I think that’s because of the length of your rays to get the average. When you go up 2 on the z axis the rays are no longer hitting anything so the average wont be right causing the object to freak out a little.

have a diffrent animation is the length is over 2?

Like “falling animation” ?

Or engage state “falling”

impact ->ground change state and trigger "impact animation "

@kevin, the ray is set to 5

@Blue, It won’t stay up. It slowly moves down. I feel there is math I could try. . . I give vector a go.

Yes but the rays on the empties are only set to 1. It works if you set them to 2 or higher.

It still falls slowly.

That’s odd…

oh an aveMaria script :evilgrin:.

I do not think your method will ever work as you expect.
A) you destroy the normal vectors. a vector consists of all components. If you separate a term you get a projection = anther vector.
B) a normal vector has always the length of 1.0
C) the normal of the ground is not necessarily the best choice to align the “hovering” object. But this depends what you really want and what you define as “down”

Imagine a wavy ground
[ATTACH=CONFIG]237925[/ATTACH]
If your rays hit both the inclining faces your vehicle will suddenly turn to the moon. A small step later it turns the opposite. (I would not want to sit in such a vehicle.

As I do not really know what you are aiming for. Here is my wild guess:
[ATTACH=CONFIG]237924[/ATTACH]
detect along the rays (local Z). When they hit assume the ground is average between the two hitpoints.