Multi agent 2d camera controller

zoom_demo_finished.blend (554.9 KB)

import bge
from mathutils import Vector

def main():

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

    camera = own.scene.active_camera
    
    player1 = own.scene.objects['Player1']
    player2 = own.scene.objects['Player2']
    player3 = own.scene.objects['Player3']
    play = [player1,player2,player3]
    xmin = None
    xmax = None
    ymin = None
    ymax = None
    
    for actor in play:
        pos = actor.worldPosition
        
                
        if xmax == None:
            xmax = pos.x
            
        elif pos.x>xmax:
            xmax=pos.x
        
        if xmin == None:
            xmin=pos.x
            
        elif pos.x < xmin :
            xmin=pos.x
        
        if ymax == None:
            ymax = pos.y
            
        elif pos.y > ymax: 
            ymax = pos.y
            
        if ymin == None:
            ymin = pos.y
            
        elif pos.y< ymin:
            ymin = pos.y
    #own.scene.objects['Text'].text= str(ymin)+"\n"+str(ymax)+"\n"+str(xmin)+"\n"+str(xmax)
    
    
    centerY = ymin-ymax
    centerX = xmax-xmin
    if abs(centerY) > abs(centerX):
        max = abs(centerY)*1.5
    else:
        max = abs(centerX)*1.5
    own['max']=max    
    if max<20:
        max=20
    
    avgX = (xmax+xmin)/2    
    avgY = (ymin+ymax)/2
    avg = Vector([avgX,avgY,0])
    
    camera.worldPosition =  camera.worldPosition.lerp( avg+Vector([0,0,20]), .5)
    
    scale =(( camera.ortho_scale*15) + max)/16 
    camera.ortho_scale= scale        
    
main()
1 Like