Ejemplo n.º 1
0
def firstStep(indAnt, curve, vel, isFlat=True, ground=None):
    """Initialise the simulation
    
    This function initialises the settings at the beginning of the simulation.
    
    """
    speed = np.linalg.norm(vel)
    curveStartPoint = up.getPosCurvePoint(curve, 0.01)
    tan = up.getTanCurvePoint(curve, 0.01)
    yAngle = up.getAngle(tan)
    
    comTrajList = []
    comTrajList.append(curveStartPoint)    
       
    triList = []
    newTri = tr.createNewTriTemplate(curveStartPoint, True, yRot=yAngle)    
    newTri = tr.plotTri(newTri, isFlat=isFlat, ground=ground)
    triList.append(newTri)

    
    strideLen = tr.triangleSpeed(speed)
    newTri = tr.createNewTriTemplate(curveStartPoint + strideLen/2*tan, False, yRot=yAngle)    
    newTri = tr.plotTri(newTri, isFlat=isFlat, ground=ground)
    triList.append(newTri)    
    
    diffCOM = np.average(triList[1], 0) - np.average(triList[0], 0)
    curTriRight = triList[1] - diffCOM    
    up.animateBodyLeg(indAnt, curveStartPoint, up.mergeTwoTripod(triList[0], curTriRight))    
    
    jointLTList = []
    jointRTList = []
    jointLT, jointRT = fe.solveJointAngles(curveStartPoint + [0, 0, -0.1], triList[0], triList[1], True)    
    jointLTList.append(jointLT)
    jointRTList.append(jointRT)        
    return comTrajList, triList, jointLTList, jointRTList
Ejemplo n.º 2
0
def walking(indAnt, startFrame, endFrame, curve, ground, speed, load, isFlat=True):
    """Simulate the character motion
    
    The function *walking* iterates through all the frames, reads the user settings, determines the foot contacts and simulates the body animation.
    """
    
    cmds.currentTime(startFrame, edit=True)
    
    t, firstLeft, indTri, nextLeft, comPos, vel = up.initialValues(curve, speed)        
    comTrajList, triList, jointLTList, jointRTList = firstStep(indAnt, curve, vel, isFlat=isFlat, ground=ground)    
    
    legTable = up.setupLegTable(comPos, triList[0], triList[1])

    curveArray = up.curveToArray(curve)
    up.setAttr(indAnt, speed, load)
    
    curTri = triList[0]
    nextTri = triList[1]
    indFrame = startFrame
    
    while (indFrame < endFrame):
        cmds.currentTime(indFrame, edit=True)        
        
        extForceFrame = up.getInputExtForce(indAnt)
        loadFrame = up.getInputLoad(indAnt)
        speedFrame = up.getInputSpeed(indAnt, vel)
        
        T = se.computePeriod(speedFrame)
        
        lastJointLT = jointLTList[-1]
        lastJointRT = jointRTList[-1]
        jointLT, jointRT = fe.solveJointAngles(comPos, curTri, nextTri, nextLeft)
        jointLTList.append(jointLT)
        jointRTList.append(jointRT)
        ## reshuffle the arrays into the legs order: 0-5
        jointAngles = []
        jointVels = []
        velLT = (np.array(jointLT) - np.array(lastJointLT))/dt
        velRT = (np.array(jointRT) - np.array(lastJointRT))/dt
        for indLeg in [0, 1, 2]:
            jointAngles.append(jointLT[indLeg*3:indLeg*3+3])
            jointAngles.append(jointRT[indLeg*3:indLeg*3+3])
            jointVels.append(velLT[indLeg*3:indLeg*3+3])
            jointVels.append(velRT[indLeg*3:indLeg*3+3])
        flutParam = {'indAnt':indAnt, 'comVel': (np.average(nextTri, axis=0) - np.average(curTri, axis=0))/T*2}
        force, forces, torqueArray = fe.computeForce(indAnt, jointAngles, jointVels, dt, flutParam=flutParam)        
        comPos = fe.computeComPos(indAnt, comPos, vel, force, extForceFrame, dt=dt)
        
                
        cross = tr.checkNewTri(comPos, comTrajList[-1], triList[indTri])
        if cross == True:
            lastTriCom = np.average(triList[indTri], 0)            
            dirVec = comPos - comTrajList[-1]
            nextCurvePoint = tr.getNextCurvePoint(curve, curveArray, lastTriCom, speedFrame)                        
            if nextLeft == True:
                newTri = tr.createNewTri(lastTriCom, dirVec, nextCurvePoint, speedFrame, loadFrame, extForceFrame, nextLeft)                
            else:
                newTri = tr.createNewTri(lastTriCom, dirVec, nextCurvePoint, speedFrame, loadFrame, extForceFrame, nextLeft)                        
            newTri = tr.plotTri(newTri, isFlat=isFlat, ground=ground)    
            vel = tr.updateRot(indAnt, vel, curTri, newTri, comPos, nextLeft, extForceFrame)
            indTri = indTri + 1
            nextLeft = not nextLeft
            triList.append(newTri)
            curTri = triList[indTri]
            nextTri = triList[indTri+1]    
        comTrajList.append(comPos)
        indFrame = indFrame + 1
        
        
        if indTri == 0:
            preTri = triList[0]
        else:
            preTri = triList[indTri-1]
        if se.checkFroude(speedFrame):
            legTable = se.refreshFastMode(legTable, cross, preTri, triList[indTri], triList[indTri+1], comPos)
        else:
            # dCOM = comTrajList[-1] - comTrajList[-2]
            if speedFrame == 0:
                legTable = se.refreshStaticMode(indAnt, legTable, comPos, nextLeft, triList[indTri], triList[indTri+1])            
            else:                
                legTable = se.refreshSlowMode(indAnt, legTable, cross, nextLeft, triList[indTri], triList[indTri+1], comPos, forces, speedFrame)
        feetPos = se.determineFeetPos(indAnt, legTable, speedFrame)     
        up.animateBodyLeg(indAnt, comPos, feetPos)