// Update is called once per frame
	void Update () {
		if(sw.pollSkeleton()){
			float currentTime = Time.time;
			// get right hand position (index = 11)
			this.rightHandPos = sw.bonePos[PlayerId,11];
			// get left hand position (index = 7)
			this.leftHandPos = sw.bonePos[PlayerId,7];
			// get spine position (index = 1)
			this.spinePos = sw.bonePos[PlayerId,1];
			// get head position (index = 3)
			this.headPos = sw.bonePos[PlayerId,3];
			// calculate the relative position of right hand
			this.rightHandRelativePos = this.rightHandPos - this.spinePos;
			float yHandRelativePos = this.rightHandPos.y - this.spinePos.y;
			float xHandRelativePos = this.rightHandPos.x - this.spinePos.x;
			/*
			Debug.Log("=======");
			Debug.Log("right hand: "+rightHandPos);
			Debug.Log("spine: "+spinePos);
			Debug.Log ("Relative pos:" + xHandRelativePos+"# "+yHandRelativePos);
			Debug.Log ("height: " + (headPos.y - spinePos.y));
			Debug.Log("=======");
			*/
			if(xHandRelativePos >= rightHandMarginX && yHandRelativePos <= 0.1){ // right hand position: down
				if(currentRightHandPos != RightHandPosition.DOWN){
					currentRightHandPos = RightHandPosition.DOWN;
					posStartTime = currentTime;
				}
			}
			else if(xHandRelativePos >= rightHandMarginX && yHandRelativePos > 0.2 && yHandRelativePos <=0.4){ // right hand position: mid
				if(currentRightHandPos != RightHandPosition.MID){
					currentRightHandPos = RightHandPosition.MID;
					posStartTime = currentTime;
				}
			}
			else if(xHandRelativePos >= rightHandMarginX && yHandRelativePos > 0.45){ // right hand position: up
				if(currentRightHandPos != RightHandPosition.UP){
					currentRightHandPos = RightHandPosition.UP;
					posStartTime = currentTime;
				}
			}
			else{
				currentRightHandPos = RightHandPosition.NON;
			}

			// validation of right hand position
			if(currentRightHandPos!=RightHandPosition.NON && currentTime - posStartTime >= detectPositionDuration){
				// rise right hand position detected event
				OnRightHandPositionDetected(currentRightHandPos);
				// reset the timer
				posStartTime = currentTime;
			}

			if(firstPos){
				firstPos = false;
			} else{
				// get right hand velocity
				this.rightHandVelo = rightHandPos - rightHandPosPre;
				// get left hand velocity
				this.leftHandVelo = leftHandPos - leftHandPosPre;
				// right hand motion detection
				if(this.rightHandVelo.x > accuracy && Math.Abs(this.rightHandVelo.x) > Math.Abs(this.rightHandVelo.y)){
					if(currentRightHandMotion != HandMotion.RIGHT_HAND_WAVE_OUT){
						currentRightHandMotion = HandMotion.RIGHT_HAND_WAVE_OUT;
						motionRightHandStartTime = currentTime;
						motionStartRightHandPos = rightHandPos;
					}
				}
				else if(this.rightHandVelo.y > accuracy && Math.Abs(this.rightHandVelo.x) < Math.Abs(this.rightHandVelo.y)){
					if(currentRightHandMotion != HandMotion.RIGHT_HAND_RISE){
						currentRightHandMotion = HandMotion.RIGHT_HAND_RISE;
						motionRightHandStartTime = currentTime;
						motionStartRightHandPos = rightHandPos;
					}
				}
				else{
					currentRightHandMotion = HandMotion.NON;
				}

				// left hand motion detection
				if(this.leftHandVelo.x < -accuracy && Math.Abs(this.leftHandVelo.x) > Math.Abs(this.leftHandVelo.y)){
					if(currentLeftHandMotion != HandMotion.LEFT_HAND_WAVE_OUT){
						currentLeftHandMotion = HandMotion.LEFT_HAND_WAVE_OUT;
						motionLeftHandStartTime = currentTime;
						motionStartLeftHandPos = leftHandPos;
					}
				}
				else if(this.leftHandVelo.y > accuracy && Math.Abs(this.leftHandVelo.x) < Math.Abs(this.leftHandVelo.y)){
					if(currentLeftHandMotion != HandMotion.LEFT_HAND_RISE){
						currentLeftHandMotion = HandMotion.LEFT_HAND_RISE;
						motionLeftHandStartTime = currentTime;
						motionStartLeftHandPos = leftHandPos;
					}
				}
				else{
					currentLeftHandMotion = HandMotion.NON;
				}

				// validation of hand motion
				bool rightHandRiseValide = false;
				bool leftHandRiseValide = false;
				// right
				if(currentRightHandMotion == HandMotion.RIGHT_HAND_WAVE_OUT 
				   && currentTime - motionRightHandStartTime <= detectMotionDuration
				   && rightHandPos.x - motionStartRightHandPos.x >= detectMotionDistance){
					// right hand wave out detected
					// rise motion detected event
					OnHandMotionDetected(HandMotion.RIGHT_HAND_WAVE_OUT);
					// reset timer for new detection
					motionRightHandStartTime = currentTime;
					motionStartRightHandPos = rightHandPos;
				}
				else if(currentRightHandMotion == HandMotion.RIGHT_HAND_RISE 
				        && currentTime - motionRightHandStartTime <= detectMotionDuration
				        && rightHandPos.y - motionStartRightHandPos.y >= detectMotionDistance){
					rightHandRiseValide = true;
				}
				// left
				if(currentLeftHandMotion == HandMotion.LEFT_HAND_WAVE_OUT 
				   && currentTime - motionLeftHandStartTime <= detectMotionDuration
				   && motionStartLeftHandPos.x - leftHandPos.x >= detectMotionDistance){
					// left hand wave out detected
					// rise motion detected event
					OnHandMotionDetected(HandMotion.LEFT_HAND_WAVE_OUT);
					// reset timer for new detection
					motionLeftHandStartTime = currentTime;
					motionStartLeftHandPos = leftHandPos;
				}
				else if(currentLeftHandMotion == HandMotion.LEFT_HAND_RISE 
				        && currentTime - motionLeftHandStartTime <= detectMotionDuration
				        && leftHandPos.y - motionStartLeftHandPos.y >= detectMotionDistance){
					leftHandRiseValide = true;
				}
				// double hand
				if(rightHandRiseValide&&leftHandRiseValide){
					// double hands rise detected
					// rise motion detected event
					OnHandMotionDetected(HandMotion.DOUBLE_HAND_RISE);
					motionRightHandStartTime = currentTime;
					motionLeftHandStartTime = currentTime;
					motionStartRightHandPos = rightHandPos;
					motionStartLeftHandPos = leftHandPos;
				}
			}
			rightHandPosPre = rightHandPos;
			leftHandPosPre = leftHandPos;
		}
	}
	// trigger the event handlers
	protected virtual void OnRightHandPositionDetected(RightHandPosition pos) {
		if (rightHandPositionDetected != null) {
			RightHandPositionDetectedEventArgs e = new RightHandPositionDetectedEventArgs();
			e.pos = pos;
			rightHandPositionDetected (this, e);
		}
	}