OYO - Rigid body wheel enemy

odyo_froyo.blend (864.0 KB)

ezgif.com-video-to-gif (6)

import bge
from mathutils import Vector

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    #Dampen rot
    own.parent.worldAngularVelocity*=.9
    
    
    #Point robot 'torso' up 
    
    own.alignAxisToVect([0,0,1],0,.95)
    
    #make sure you align to wheel axis
    own.alignAxisToVect(own.parent.worldOrientation.col[2],2,.95)
    
    #get angle from head to target
    v2 = own.children['Suzanne'].getVectTo(own.scene.objects['Cube.002'])
    
    #align head to target
    own.children['Suzanne'].alignAxisToVect(-v2[1],1,.95)
    own.children['Suzanne'].alignAxisToVect(own.worldOrientation.col[0],2,.95)
    
    #get angle diff between up and our up axis
    
    diff = own.worldOrientation.col[0] - Vector([0,0,1])
    
    
    #pull to target
    own.parent.applyForce(v2[1]*(10/v2[0]),0)
    
    
    #don't tip over
    own.parent.applyTorque(-diff*200,0)
    
    
    
    #sideways friction
    vel = own.worldOrientation.inverted() @ own.parent.worldLinearVelocity
    vel[2]*=.95
    own.parent.worldLinearVelocity = own.worldOrientation @ vel
        
        
     
        
    #are we facing the target?
    local = own.worldTransform.inverted() @ own.scene.objects['Cube.002'].worldPosition
    diff2 = -own.worldOrientation.col[1] - v2[1]    
        
        
    #punch gas and run down enemy
    if diff2.magnitude<1:
        own.parent.applyTorque([0,0,-10],1)
        
    if diff2.magnitude<.5:
        own.parent.applyTorque([0,0,-10],1)
        
    if diff2.magnitude<.25:
        own.parent.applyTorque([0,0,-10],1)
    
    
    #brakes
    if diff2.magnitude>1.25:
        own.parent.applyTorque([0,0,10],1)
        
    if diff2.magnitude>1:
        own.parent.applyTorque([0,0,10],1)
            
    
    
    #steering
    if local.y<0:
        if local.z>-.5:
            own.parent.applyTorque(own.worldOrientation.col[0]*-6,0)
        elif local.z<-.5:
            own.parent.applyTorque(own.worldOrientation.col[0]*6,0)
        else:
            vel = own.worldOrientation.inverted() @ own.parent.worldAngularVelocity    
            vel[0]*=.9
            own.parent.worldAngularVelocity = own.worldOrientation @ vel
    else:
        
        
        #brakes 
        vel = own.worldOrientation.inverted() @ own.parent.worldLinearVelocity
        vel[1]*=.95
        own.parent.worldLinearVelocity = own.worldOrientation @ vel
        
        
        #steering in reverse
        if local.z<0:
            own.parent.applyTorque(own.worldOrientation.col[0]*2,0)
        elif local.z>0:
            own.parent.applyTorque(own.worldOrientation.col[0]*-2,0)
        
                
                
main()

hierarchy

wheel(named cube atm) ------'Arm(contains logic)'cube.001 -------head(suzzane)

1 Like