def _createArms(self): leftArm = RobotPart(Vector(-250, -20), "poppy/left-arm.png", Config.SPRITE_SCALE, name='left-arm') rightArm = RobotPart(Vector(230, -50), "poppy/right-arm.png", Config.SPRITE_SCALE, name='right-arm') return leftArm, rightArm
def _createEars(self): leftEar = RobotPart(Vector(-180, 369), "images/dogbot/left-ear.png", Config.SPRITE_SCALE, name='left-ear') rightEar = RobotPart(Vector(192, 370), "images/dogbot/right-ear.png", Config.SPRITE_SCALE, name='right-ear') return leftEar, rightEar
def _createEyeFidgetAnimation(self, prevAnim=None): if prevAnim is not None: morphTargetAtStart = prevAnim.getMorphTargetEnd() else: morphTargetAtStart = MorphTargetCore({ 'left-eye/white': Vector(0, 0), 'left-eye/black': Vector(0, 0), 'right-eye/white': Vector(0, 0), 'right-eye/black': Vector(0, 0) }) eyeTargetOffsetX = random.randrange(-15, 15) eyeTargetOffsetY = random.randrange(-15, 5) morphTargetAtStop = MorphTargetCore({ 'left-eye/black': Vector(eyeTargetOffsetX, eyeTargetOffsetY), 'left-eye/white': Vector(int(eyeTargetOffsetX * 0.8), int(eyeTargetOffsetY * 0.8)), 'right-eye/black': Vector(eyeTargetOffsetX, eyeTargetOffsetY), 'right-eye/white': Vector(int(eyeTargetOffsetX * 0.8), int(eyeTargetOffsetY * 0.8)) }) # Pause every x second pauseAtStart = random.randrange(self._FPS * 2, self._FPS * 4) self._eyeAnimationCtr += 1 return AnimationCore(morphTargetAtStart, morphTargetAtStop, Config.speedScale(0.03), pauseAtStart)
def _createHeadFidgetAnimation(self, prevAnim=None): if prevAnim is not None: morphTargetAtStart = prevAnim.getMorphTargetEnd() else: morphTargetAtStart = MorphTargetCore({'frame': Vector(0, 0)}, {'frame': 0}) headTargetOffsetY = random.randrange(-5, 4) headRotationOffset = random.randrange(-10, 10) morphTargetAtStop = MorphTargetCore( {'frame': Vector(0, int(headTargetOffsetY))}, {'frame': headRotationOffset}) # Pause every x second pauseAtStart = random.randrange(self._FPS * 1, self._FPS * 3) self._headAnimationCtr += 1 return AnimationCore(morphTargetAtStart, morphTargetAtStop, Config.speedScale(0.02), pauseAtStart)
def _getFinalMorphTargetPos(self, paramVec: Vector, morphTargets: [MorphTarget]): # paramVec = Vector(0.5, -0.5) finalPos = Vector(0, 0) totalWeight = 0.0 for target in morphTargets: curPos, weight = target.getAt(self._name, paramVec) # curPos = Vector(30.0, 30.0) # weight = 1.0 # curPos = target.getAt(self._name, paramVec) totalWeight += weight if curPos is not None: finalPos = finalPos.add(curPos) return finalPos.scale(totalWeight)
def _createEye(self, pos, namePrefix): eyeBase = RobotPart(pos, "images/dogbot/eye-white.png", Config.SPRITE_SCALE, name=namePrefix + "/white") eyeBlack = RobotPart(Vector(0, -10), "images/dogbot/eye-black.png", Config.SPRITE_SCALE, name=namePrefix + "/black") eyeBrow = RobotPart(Vector(-10, 70), "images/dogbot/eyebrow.png", Config.SPRITE_SCALE, name=namePrefix + "/eyebrow") eyeBase.appendPart(eyeBlack) eyeBase.appendPart(eyeBrow) return eyeBase
def _createTailFidgetAnimation(self, prevAnim=None): if prevAnim is not None: morphTargetAtStart = prevAnim.getMorphTargetEnd() morphTargetAtStop = prevAnim.getMorphTargetStart() else: morphTargetAtStart = MorphTargetCore({'tail': Vector(0, 0)}, {'tail': 0}) tailRotationOffset = random.randrange(-30, -20) morphTargetAtStop = MorphTargetCore({'tail': Vector(0, 0)}, {'tail': tailRotationOffset}) pauseAtStart = 0 if self._tailAnimationCtr % 6 == 0: # Pause every x second pauseAtStart = random.randrange(self._FPS * 3, self._FPS * 8) self._tailAnimationCtr += 1 return AnimationCore(morphTargetAtStart, morphTargetAtStop, Config.speedScale(0.3), pauseAtStart)
def _createLeftArmFidgetAnimation(self, prevAnim=None): # Alternate moving from start to stop if prevAnim is not None: morphTargetAtStart = prevAnim.getMorphTargetEnd() morphTargetAtStop = prevAnim.getMorphTargetStart() else: morphTargetAtStart = MorphTargetCore({'left-arm': Vector(0, 0)}, {'left-arm': 0}) earTargetOffsetX = random.randrange(-2, 2) earTargetOffsetY = random.randrange(15, 20) morphTargetAtStop = MorphTargetCore( {'left-arm': Vector(earTargetOffsetX, earTargetOffsetY)}, {'left-arm': -10}) pauseAtStart = 0 if self._leftArmAnimationCtr % 15 == 0: # Pause every x second pauseAtStart = random.randrange(self._FPS * 3, self._FPS * 8) self._leftArmAnimationCtr += 1 return AnimationCore(morphTargetAtStart, morphTargetAtStop, Config.speedScale(0.05), pauseAtStart)
def _createRightEarFidgetAnimation(self, prevAnim=None): # Alternate moving from start to stop if prevAnim is not None: morphTargetAtStart = prevAnim.getMorphTargetEnd() morphTargetAtStop = prevAnim.getMorphTargetStart() else: morphTargetAtStart = MorphTargetCore({'right-ear': Vector(0, 0)}, {'right-ear': 0}) earTargetOffsetX = random.randrange(-2, 2) earTargetOffsetY = random.randrange(20, 30) morphTargetAtStop = MorphTargetCore( {'right-ear': Vector(earTargetOffsetX, earTargetOffsetY)}, {'right-ear': 10}) pauseAtStart = 0 if self._rightEarAnimationCtr % 4 == 0: # Pause every x second pauseAtStart = random.randrange(self._FPS * 3, self._FPS * 8) self._rightEarAnimationCtr += 1 return AnimationCore(morphTargetAtStart, morphTargetAtStop, Config.speedScale(0.2), pauseAtStart)
def _create(self, screenSize): self._spriteList = arcade.SpriteList() self._robot = RobotPart(Vector(screenSize[0] / 2, screenSize[1] / 2), "images/dogbot/frame.png", Config.SPRITE_SCALE, scale=Config.SPRITE_SCALE, name='body-frame') tail = RobotPart(Vector(255, -45), "images/dogbot/tail.png", Config.SPRITE_SCALE) self._robot.appendPart(tail) self._body = RobotPart(Vector(0, 0), "images/dogbot/body.png", Config.SPRITE_SCALE) self._robot.appendPart(self._body) headFrame = RobotPart(Vector(0, 0), "images/dogbot/frame.png", 1.0) self._body.appendPart(headFrame) leftEar, rightEar = self._createEars() headFrame.appendPart(leftEar) headFrame.appendPart(rightEar) head = RobotPart(Vector(-20, 0), "images/dogbot/head.png", Config.SPRITE_SCALE) headFrame.appendPart(head) leftEyeBase = self._createEye(Vector(-85, 320), "left-eye") head.appendPart(leftEyeBase) rightEyeBase = self._createEye(Vector(60, 320), "right-eye") head.appendPart(rightEyeBase) allSprites = self._robot.spriteList() for sprite in allSprites: self._spriteList.append(sprite) self._fidgetAnimation = FidgetAnimationController() self._createEyeTargetAnimation() self._brainState = BrainStateMachine()
def getPosAtVecParamForQuad(self, vecParam): finalPos = {} lowerLeftPos, _ = self._morphTargets[0][0].getWithWeight(1.0) lowerRightPos, _ = self._morphTargets[1][0].getWithWeight(1.0) upperLeftPos, _ = self._morphTargets[3][0].getWithWeight(1.0) weightX = (vecParam.x - self._morphTargets[0][1].x) / ( self._morphTargets[1][1].x - self._morphTargets[0][1].x) weightY = (vecParam.y - self._morphTargets[1][1].y) / ( self._morphTargets[2][1].y - self._morphTargets[1][1].y) for name, vec in lowerLeftPos.items(): finalVec = Vector( lowerLeftPos[name].x * (1.0 - weightX) + lowerRightPos[name].x * weightX, lowerLeftPos[name].y * (1.0 - weightY) + upperLeftPos[name].y * weightY) finalPos[name] = finalVec return finalPos
def _create(self, screenSize): self._spriteList = arcade.SpriteList() # set up frame mian body self._robot = RobotPart(Vector(screenSize[0] / 2, screenSize[1] / 2), "poppy/frame.png", Config.SPRITE_SCALE, scale=Config.SPRITE_SCALE, name='body-frame') # set up arms inside frame leftArm, rightArm = self._createArms() self._robot.appendPart(leftArm) self._robot.appendPart(rightArm) # set up body frame self._body = RobotPart(Vector(0, -150), "poppy/body_part.png", Config.SPRITE_SCALE) self._robot.appendPart(self._body) headFrame = RobotPart(Vector(0, 100), "poppy/frame.png", 1.0) self._body.appendPart(headFrame) #leftEar, rightEar = self._createEars() head = RobotPart(Vector(0, 250), "poppy/head.png", Config.SPRITE_SCALE) headFrame.appendPart(head) leftEyeBase = self._createEye(Vector(-300, 60), "left-eye") head.appendPart(leftEyeBase) rightEyeBase = self._createEye(Vector(300, 60), "right-eye") head.appendPart(rightEyeBase) allSprites = self._robot.spriteList() for sprite in allSprites: self._spriteList.append(sprite) self._fidgetAnimation = FidgetAnimationController() self._createEyeTargetAnimation() self._brainState = BrainStateMachine()
def parse_objects(self, objects): if len(objects) > 0: skeleton = objects[0] left_elbow = skeleton.get_joint('left_elbow') right_elbow = skeleton.get_joint('right_elbow') left_shoulder = skeleton.get_joint('left_shoulder') right_shoulder = skeleton.get_joint('right_shoulder') nose = skeleton.get_joint('nose') neck = skeleton.get_joint('neck') # Back face for 1 second to reset the game if self._is_reset_detected(left_shoulder, right_shoulder): self._game_state = 0 if left_elbow is not None and right_elbow is not None and left_shoulder is not None and right_shoulder is not None: # left_elbow.x = 10.0 # left_elbow.y = 0.0 # left_shoulder.x = 20.0 # left_shoulder.y = 10.0 # # right_elbow.x = 40.0 # right_elbow.y = 40.0 # right_shoulder.x = 30.0 # right_shoulder.y = 30.0 # self._game_state = 0 # Calculate wing roll target from the delta between elbow and shoulder left_shoulder_to_elbow_vec = Vector( left_elbow.x - left_shoulder.x, left_elbow.y - left_shoulder.y).normalise() horizVec = Vector(1.0, 0.0) self._left_wing_roll_target = min( 90, max( -15, left_shoulder_to_elbow_vec.angle_from_vector(horizVec) * 180.0 / math.pi + 30)) right_shoulder_to_elbow_vec = Vector( right_elbow.x - right_shoulder.x, right_elbow.y - right_shoulder.y).normalise() horizVec = Vector(-1.0, 0.0) self._right_wing_roll_target = min( 90, max( -15, horizVec.angle_from_vector(right_shoulder_to_elbow_vec) * 180.0 / math.pi + 30)) # Calculate roll from the delta between left and right elbow left_elbow_to_right_elbow_vec = Vector( left_elbow.x - right_elbow.x, left_elbow.y - right_elbow.y).normalise() horizVec = Vector(1.0, 0.0) self._roll = left_elbow_to_right_elbow_vec.angle_from_vector( horizVec) * 180.0 / math.pi # Use the distance between left and right shoulder as a measurement threshold shoulder_dist = abs(right_shoulder.x - left_shoulder.x) self._body_height = self._calc_body_height( nose, neck, shoulder_dist) # Detect jump by measuring the vertical movement of both shoulder # If detected, set game state to flying if self._is_jump_detected(left_shoulder, right_shoulder, neck, nose, shoulder_dist): self._game_state = 1 print( f"Jump {self._game_state} Roll {self._right_wing_roll_target} Left {self._left_wing_roll_target} Right {self._right_wing_roll_target}" ) return self._roll, self._pitch, self._game_state, self._left_wing_roll_target, self._right_wing_roll_target, self._body_height
def transform(self, point): result = self._affine * (point.x, point.y) return Vector(result[0], result[1])
def getTranslation(self): return Vector(self._affine[2], self._affine[5])
def getEyeCenter(self): return Vector(self.boundingBox.center()[0], self.boundingBox.y1 + self.boundingBox.height() * 0.1)
def transform(self, objCoord): # To calculate the puppet eye coordinate (-0.5 -> 0.5) in order to look at the object at objCoord eyeCoord = objCoord.subtract(Vector(0.5, 0.5)) return eyeCoord
def _createEyeTargetAnimation(self): self._eyeTargetAnim = MorphTargetInterp() blackOffsetLeft = -19 whiteOffsetLeft = blackOffsetLeft * 0.8 blackOffsetRight = 15 whiteOffsetRight = blackOffsetRight * 0.8 blackOffsetTop = -15 whiteOffsetTop = blackOffsetTop * 0.8 blackOffsetBottom = 10 whiteOffsetBottom = blackOffsetBottom * 0.8 morphTarget = MorphTargetCore({ 'left-eye/white': Vector(whiteOffsetLeft, whiteOffsetTop), 'left-eye/black': Vector(blackOffsetLeft, blackOffsetTop), 'right-eye/white': Vector(whiteOffsetLeft, whiteOffsetTop), 'right-eye/black': Vector(blackOffsetLeft, blackOffsetTop) }) self._eyeTargetAnim.addMorpTarget(morphTarget, Vector(-0.5, -0.5)) morphTarget = MorphTargetCore({ 'left-eye/white': Vector(whiteOffsetRight, whiteOffsetTop), 'left-eye/black': Vector(blackOffsetRight, blackOffsetTop), 'right-eye/white': Vector(whiteOffsetRight, whiteOffsetTop), 'right-eye/black': Vector(blackOffsetRight, blackOffsetTop) }) self._eyeTargetAnim.addMorpTarget(morphTarget, Vector(0.5, -0.5)) morphTarget = MorphTargetCore({ 'left-eye/white': Vector(whiteOffsetRight, whiteOffsetBottom), 'left-eye/black': Vector(blackOffsetRight, blackOffsetBottom), 'right-eye/white': Vector(whiteOffsetRight, whiteOffsetBottom), 'right-eye/black': Vector(blackOffsetRight, blackOffsetBottom) }) self._eyeTargetAnim.addMorpTarget(morphTarget, Vector(0.5, 0.5)) morphTarget = MorphTargetCore({ 'left-eye/white': Vector(whiteOffsetLeft, whiteOffsetBottom), 'left-eye/black': Vector(blackOffsetLeft, blackOffsetBottom), 'right-eye/white': Vector(whiteOffsetLeft, whiteOffsetBottom), 'right-eye/black': Vector(blackOffsetLeft, blackOffsetBottom) }) self._eyeTargetAnim.addMorpTarget(morphTarget, Vector(-0.5, 0.5)) self._hasEyeTargetState = []
def __init__(self, offset=Vector(0, 0), scale=1.0, angle=0.0): self._pos = offset self._scale = scale self._angle = angle self._affine = Affine.identity()