vector KinectMeshAnimator::GetSkeletonOrientation(NUI_SKELETON_DATA& skeleton, int index1, int index2)
	{
		vector orientation;
		Joint* joint1 = bindings[index1];
		Joint* joint2 = bindings[index2];
		if (!joint1 || !joint2) return XMVectorSet(0.0, 1.0, 0.0, 0.0);

		// INVERT Z
		orientation = XMVectorSet(	skeleton.SkeletonPositions[index2].x - skeleton.SkeletonPositions[index1].x,
									skeleton.SkeletonPositions[index2].y - skeleton.SkeletonPositions[index1].y,
									skeleton.SkeletonPositions[index1].z - skeleton.SkeletonPositions[index2].z,
									0.0);

		vector bindOrientation = bindOrientations[index1];
		vector axis = XMVector3Normalize(XMVector3Cross(bindOrientation, orientation));

		if (XMVector3Equal(axis, XMVectorZero()))
			return XMVectorSet(0.0, 1.0, 0.0, 0.0);

		float angle;
		XMStoreFloat(&angle, XMVector3Dot(bindOrientation, orientation));
		angle = acos(angle);
		return XMQuaternionRotationAxis(axis, angle);
	}
 bool operator==(const VertPosColor & rhs)
 {
   return XMVector3Equal(pos, rhs.pos);
 }
void TransformTool::Update(float dt)
{
	HydraManager* hydra = InputSystem::get()->getHydra();

	mPreviousHydraPositions[0] = mHydraPositions[0];
	mPreviousHydraPositions[1] = mHydraPositions[1];

	mHydraPositions[0] = hydra->getPointerPosition(0);
	mHydraPositions[1] = hydra->getPointerPosition(1);

	XMVECTOR hydraAveragePosition = (mHydraPositions[0] + mHydraPositions[1]) * 0.5f;

	static const int bumperBit = 1 << 7;

	if (mpTargetTransform != NULL && (hydra->getButtons(0) & bumperBit) && (hydra->getButtons(1) & bumperBit)) //Rotation
	{
		XMVECTOR oldOrientationVector = XMVector3Normalize(mPreviousHydraPositions[0] - mPreviousHydraPositions[1]);
		XMVECTOR orientationVector = XMVector3Normalize(mHydraPositions[0] - mHydraPositions[1]);

		if (!XMVector3Equal(oldOrientationVector, orientationVector))
		{
			XMVECTOR rotationAxis = XMVector3Normalize(XMVector3Cross(oldOrientationVector, orientationVector));
			float rotationAmount = XMVectorGetX(XMVector3Dot(oldOrientationVector, orientationVector));

			//Avoid undefined values from acosf
			if (rotationAmount > 1.0) 
			{
				rotationAmount = 1.0;
			}
			else if (rotationAmount < -1.0)
			{
				rotationAmount = -1.0;
			}

			rotationAmount = acosf(rotationAmount);
			
			XMVECTOR rotationQuaternion = XMQuaternionRotationAxis(rotationAxis, rotationAmount);

			//Handle rotations around the grab point
			XMVECTOR newTranslation = mpTargetTransform->getTranslation();
			XMVECTOR translationOffset = newTranslation - hydraAveragePosition;//From point between two controllers to origin of translation
			newTranslation = hydraAveragePosition + XMVector3Rotate(translationOffset, rotationQuaternion); 

			mpTargetTransform->setTranslation(newTranslation);
			mpTargetTransform->rotate(rotationQuaternion);
		}
	}
	else if (mpTargetTransform != NULL && (hydra->getButtons(1) & bumperBit)) //Translation
	{
		XMVECTOR offsetVector =  mHydraPositions[1] - mPreviousHydraPositions[1];

		mpTargetTransform->translate(offsetVector);
	}
	else if (mpTargetTransform != NULL && (hydra->getButtons(0) & bumperBit)) //Scaling
	{
		float oldDistance = XMVectorGetX(XMVector3Length(mPreviousHydraPositions[0] - mPreviousHydraPositions[1]));
		float newDistance = XMVectorGetX(XMVector3Length(mHydraPositions[0] - mHydraPositions[1]));

		float ratio =  newDistance / oldDistance;

		XMVECTOR newTranslation = mpTargetTransform->getTranslation();
		XMVECTOR translationOffset = newTranslation - hydraAveragePosition;//From point between two controllers to origin of translation
		newTranslation = hydraAveragePosition + translationOffset * ratio;

		mpTargetTransform->setTranslation(newTranslation);
		mpTargetTransform->scale(ratio);
	}
}
Beispiel #4
0
void Unit::Update(float DeltaTime, Terrain* TerrainInstance)
{
	if ( IsAlive() && gHealth.first <= 0 )
	{
		SetWeaponState( Hold );
		DropWeapon();
		Kill();
		StopAllAnimations();
		PlayAnimation("Death");
		PlayAnimationAfter("Death", "Dead");
		SoundManager::GetInstance()->Play( gDeathSound, SFX, false );

		if (!PlayingAnimation("Death"))
		{
			SetState( Dead );
			return;
		}		
	}

	GameObject::Update(DeltaTime, TerrainInstance);

	if ( GetState() == Dying )
		return;

	XMVECTOR vel = XMLoadFloat3(&GetFloat3Value( Velocity ));
	if (!XMVector3Equal(vel, XMVectorZero()))
	{
		XMVECTOR dir  = XMLoadFloat3(&GetFloat3Value( Direction ));
		XMVECTOR angleV = XMVector3AngleBetweenVectors(dir, vel);

		float angle;
		XMStoreFloat(&angle, angleV);

		XMVECTOR crossV = XMVector3Cross(dir, vel);

		if (XMVectorGetY(crossV) < 0)
			angle *= -1;

		//cout << 180 * angle / PI << endl;

		if (fabs(angle) <= PI * 0.25f)
			gMoveDirection = Forward;

		else if (fabs(angle) > PI * 0.75f)
			gMoveDirection = Back;

		else if (angle < 0)
			gMoveDirection = Left;

		else if (angle > 0)
			gMoveDirection = Right;

		XMVECTOR L = XMVector3Length(vel);
		float speed;
		XMStoreFloat(&speed, L);
		speed /= UnitsPerMeter;

		switch (gMoveDirection)
		{
		case Forward:
			if (speed > 1.05f * gWalkSpeed)
			{
				SetMoveState(Run);	
				SetAnimationSpeed("Run", speed);
				SetAnimationSpeed(GetAnimation("UpperRun"), speed);							
			}
			else
			{
				SetMoveState(Walk);
				SetAnimationSpeed("Walk", speed);
			}
			break;
		case Back:
			SetMoveState(Walk);
			SetAnimationSpeed("Back", speed);
			break;
		case Right:
			SetMoveState(Walk);
			SetAnimationSpeed("SideStepRight", speed);
			break;
		case Left:
			SetMoveState(Walk);
			SetAnimationSpeed("SideStepLeft", speed);
			break;
		}
	}
	else
	{
		gMoveDirection = None;

		if (!Crouching())
			SetMoveState(Stand);
	}

	if ( gCurrentWeapon )
	{
		XMFLOAT3 handPos;
		XMFLOAT4 handRot;
		
		XMVECTOR pos = XMLoadFloat3(&GetFloat3Value( Position ));
		XMVECTOR dir = XMLoadFloat3(&GetFloat3Value( Direction ));
		XMVECTOR lv = XMVector3Length(dir);

		bool gotJointPos = false;
		bool gotJointRot = false;
		if (m_ModelInstance)
		{
			gotJointPos = GetJointPosition("RightHand", handPos);
			gotJointRot = GetJointRotation("RightHand", handRot);
		}

		if (!gotJointPos)
		{
			pos += dir * (GetRadius() - 10);
			XMStoreFloat3(&handPos, pos);
		}
		
		if (gCurrentWeapon->NeedReload())
		{
			ReloadWeapon();
		}
		
		//gWeapon->Update(DeltaTime);
		//gWeapon->MoveTo( position );
		gCurrentWeapon->MoveTo( handPos );

		XMFLOAT3 weaponPos;
		if (gCurrentWeapon->GetJointPosition("RightHand", weaponPos))
		{
			XMStoreFloat3(&handPos, XMLoadFloat3(&handPos) + (XMLoadFloat3(&handPos) - XMLoadFloat3(&weaponPos)));
			gCurrentWeapon->MoveTo( handPos );
		}
		
		if (gotJointRot)
		{
			gCurrentWeapon->SetRotation( handRot );
		}
		else
			gCurrentWeapon->SetRotation( GetQuaternation() );
	}
	/*
	else
	{
		LoopAnimation("PistolUpperWalk");
	}
	*/
}
bool CollisionMesh::CheckCollisionsGJK1(CollisionMesh& otherMesh)
{
  std::vector<XMFLOAT3> convexHull;
  bool foundOrigin = false;

  std::vector<VPCNTDesc> vertices = GetVertices();
  std::vector<VPCNTDesc> otherVertices = otherMesh.GetVertices();
  XMMATRIX otherWorld = otherMesh.GetWorldTransform();
  XMMATRIX world = GetWorldTransform();

  // Pre-multiply the model's vertices so as to avoid transforming them during comparison.
  for (int vertIndex = 0; vertIndex < vertices.size(); vertIndex++)
  {
    XMVECTOR vertexTransform = XMLoadFloat3(&vertices[vertIndex].Position);
    XMStoreFloat3(&vertices[vertIndex].Position, XMVector3Transform(vertexTransform, world));
  }

  for (int otherVertIndex = 0; otherVertIndex < otherVertices.size(); otherVertIndex++)
  {
    XMVECTOR vertexTransform = XMLoadFloat3(&otherVertices[otherVertIndex].Position);
    XMStoreFloat3(&otherVertices[otherVertIndex].Position, XMVector3Transform(vertexTransform, otherWorld));
  }

  // Now we get to the fun part; the subtraction.
  for (int vertIndex = 0; vertIndex < vertices.size() && !foundOrigin; vertIndex++)
  {
    XMFLOAT3 vertexValue = vertices[vertIndex].Position;
    XMVECTOR vertexTransform = XMLoadFloat3(&vertexValue);

    for (int otherVertIndex = 0; otherVertIndex < otherVertices.size() && !foundOrigin; otherVertIndex++)
    {
      XMVECTOR otherVertexTransform = XMLoadFloat3(&otherVertices[otherVertIndex].Position);
      XMFLOAT3 convexHullPoint;

      XMVECTOR difference = XMVectorSubtract(vertexTransform, otherVertexTransform);
      XMStoreFloat3(&convexHullPoint, difference);
      convexHull.push_back(convexHullPoint);

      foundOrigin = XMVector3Equal(difference, XMVectorZero());
    }

    convexHull.push_back(vertexValue);
  }

  if (!foundOrigin)
  {
    XMFLOAT3 collisionLine = XMFLOAT3(0.0f, 1250.0f, 500.0f);
    printf("We ain't found shit!");
    bool collision = true;
    int intersections = 0;
    for (int hullVertexIndex = 0; hullVertexIndex < convexHull.size() && convexHull.size() > 3; hullVertexIndex += 3)
    {
      int secondIndex = (hullVertexIndex + 1) % (convexHull.size() - 1);
      int thirdIndex = (hullVertexIndex + 2) % (convexHull.size() - 1);

      XMFLOAT3 firstVert = convexHull[hullVertexIndex];
      XMFLOAT3 secondVert = convexHull[secondIndex];
      XMFLOAT3 thirdVert = convexHull[thirdIndex];
      XMFLOAT3 origin = XMFLOAT3(0.0f, 0.0f, 0.0f);

      // we need to check the normal. Calculate using cross product.
      XMVECTOR firstVector = XMVectorSet(secondVert.x - firstVert.x, secondVert.y - firstVert.y, secondVert.z - firstVert.z, 0.0f);
      XMVECTOR secondVector = XMVectorSet(thirdVert.x - secondVert.x, thirdVert.y - secondVert.y, thirdVert.z - secondVert.z, 0.0f);

      XMFLOAT3 normal;
      XMStoreFloat3(&normal, XMVector3Normalize(XMVector3Cross(firstVector, secondVector)));

      // check to ensure no parallels are detected.
      float firstDot = (normal.x * collisionLine.x) + (normal.y * collisionLine.y) + (normal.z * collisionLine.z);
      if (firstDot < 0)
      {
        float delta = -((normal.x * (origin.x - firstVert.x)) + (normal.y * (origin.y - firstVert.y)) + (normal.z * (origin.z - firstVert.y))) /
          firstDot;

        if (delta < 0)
        {
          break;
        }

        XMFLOAT3 pointToCheck = XMFLOAT3(origin.x - (collisionLine.x * delta), origin.y - (collisionLine.y * delta), origin.z * (collisionLine.z * delta));

        bool firstCheck = CheckWinding(firstVert, secondVert, pointToCheck, normal);
        bool secondCheck = CheckWinding(secondVert, thirdVert, pointToCheck, normal);
        bool thirdCheck = CheckWinding(thirdVert, firstVert, pointToCheck, normal);

        if (firstCheck && secondCheck && thirdCheck)
        {
          intersections++;
        }
        else
        {
          collision = false;
        }
      }
    }

    if ((intersections % 2) == 1)
    {
      foundOrigin = true;
    }
  }

  return foundOrigin;
}