Well, I’m trying to make things hover. And to do this I created a simple script:
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
hover_pads = [o for o in own.children if 'hover' in o]
for pad in hover_pads:
down = pad.getAxisVect([0,0,-1])
_, hit_point, _ = pad.rayCast(pad.worldPosition+down, pad.worldPosition, 10)
if hit_point != None:
height = (pad.worldPosition - hit_point).length
impulse = -down/(height)
pad.parent.applyImpulse(pad.localPosition, impulse)
bge.render.drawLine(pad.worldPosition, pad.worldPosition+impulse*100, [0,1,0])
Pretty simple, right?
It gets a bunch of children designated as hoverpads by a propery, and uses their heights and locations to apply an impulse at it’s location to the main vehicle.
This method has a couple advantages of rigid bodies, as it requires less physics objects, and the rigid body joints seem to be a little ‘sloppy’ where large forces are involved. (say, a 700kg vehicle. Hang, even with ten newtons of force they flex)
So here’s the problem. The script doesn’t work properly. Check the blend:
Hovertest.blend (543 KB)
Hover-vehicle number 2 has four symmetrically arranged hover-pads. And surprise, it has a stable hover
Hover-vehicle number 1 has three pads arranged non-symmetrically. It should flip over towards the top of the screen. So why does it flip sideways? The forces should be balanced in that direction.
By pausing the scene after a couple frames (and studying the afterimage of the drawline command), I analysed what was happening. In the first frame, all the lines are the same length. So it should just travel vertically. In the second frame, it’s tilted slightly, and the lower one is applying more force than the higher one, so it should level out. In the third frame, it’s tilted more in the same direction, and the corrective force has got stronger, but it isn’t correcting.
I find this odd, because the drawline is exactly where the impulse is being applied, and of the magnitude that it will be applied.
So why are the forces not being applied correctly? Is it a problem in the script? Because I can’t find it.