void MotionCallback(int x, int y)
{
	int dx = mx - x;
	int dy = my - y;

	if (gMouseSphere) // Move the mouse sphere
	{
		NxVec3 pos;
		ViewUnProject(x,y, gMouseDepth, pos);
		gMouseSphere->setGlobalPosition(pos);
		gHitActor->wakeUp();
	}
	else if (gHitCloth) // Attach the cloth vertex
	{
		NxVec3 pos; 
		ViewUnProject(x,y, gMouseDepth, pos);
		gHitCloth->attachVertexToGlobalPosition(gHitClothVertex, pos);
	}
	else if (bLeftMouseButtonPressed) // Set camera
	{   
		gCameraForward.normalize();
		gCameraRight.cross(gCameraForward,NxVec3(0,1,0));

		NxQuat qx(NxPiF32 * dx * 20 / 180.0f, NxVec3(0,1,0));
		qx.rotate(gCameraForward);
		NxQuat qy(NxPiF32 * dy * 20 / 180.0f, gCameraRight);
		qy.rotate(gCameraForward);
	}

    mx = x;
    my = y;
}
bool Pick(int x, int y)
{
    double length;
    point ray_orig, ray_dir;
    ViewUnProject(x, y, 0.0f, &ray_orig);
    ViewUnProject(x, y, 1.0f, &ray_dir);
    pDIFFERENCE(ray_dir, ray_orig, ray_dir);
    pNORMALIZE(ray_dir);
    
}
void Pick(int x, int y)
{
	LetGo();

	NxRay ray; 
	ViewUnProject(x, y, 0.0f, ray.orig);
	ViewUnProject(x, y, 1.0f, ray.dir);
	ray.dir -= ray.orig; 
	ray.dir.normalize();

	NxRaycastHit hit;
	NxShape* closestShape = gScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit);

	NxVec3 origin = ray.orig;
	NxVec3 impact = hit.worldImpact;
	NxReal distRB = NX_MAX_REAL;
	if (closestShape && closestShape->getActor().isDynamic()) 
	{
		distRB = (impact.x - origin.x) * (impact.x - origin.x) + (impact.y - origin.y) * (impact.y - origin.y) + (impact.z - origin.z) * (impact.z - origin.z);
	}

	NxVec3 hitcloth, hitRecord;
	NxU32 vertexId;
	NxReal distCloth, closest= NX_MAX_REAL;
	NxU32 indexCloth, indexClothVertex;

	NxCloth **cloths = gScene->getCloths();
	for (NxU32 i = 0; i < gScene->getNbCloths(); i++) 
	{
		if (cloths[i]->raycast(ray, hitcloth, vertexId))
		{
			distCloth = (hitcloth.x - origin.x) * (hitcloth.x - origin.x) + (hitcloth.y - origin.y) * (hitcloth.y - origin.y) + (hitcloth.z - origin.z) * (hitcloth.z - origin.z);
			if(distCloth < closest)	
			{
				closest = distCloth;
				indexCloth = i;
				indexClothVertex = vertexId;
				hitRecord = hitcloth;

			}
		}
	}

	if (distRB > closest) // Pick cloth
	{
		gHitCloth = cloths[indexCloth];
		gHitClothVertex = indexClothVertex;

		int hitx, hity;
		ViewProject(hitRecord, hitx, hity, gMouseDepth);

	}
	else if (distRB < closest) // Pick actor
	{
		gHitActor = &closestShape->getActor();
		gHitActor->wakeUp();

		int hitx, hity;
		gMouseSphere = CreateSphere(hit.worldImpact, 0.1, 1);
		gMouseSphere->raiseBodyFlag(NX_BF_KINEMATIC);
		gMouseSphere->raiseActorFlag(NX_AF_DISABLE_COLLISION);
		ViewProject(hit.worldImpact, hitx, hity, gMouseDepth);

		NxDistanceJointDesc desc;
		desc.actor[0] = gMouseSphere;
		desc.actor[1] = gHitActor;
		gMouseSphere->getGlobalPose().multiplyByInverseRT(hit.worldImpact, desc.localAnchor[0]);
		gHitActor->getGlobalPose().multiplyByInverseRT(hit.worldImpact, desc.localAnchor[1]);
		desc.spring.damper = 1;
		desc.spring.spring = 200;
		desc.flags |= NX_DJF_MAX_DISTANCE_ENABLED | NX_DJF_SPRING_ENABLED;
		NxJoint* joint = gScene->createJoint(desc);
		gMouseJoint = (NxDistanceJoint*)joint->is(NX_JOINT_DISTANCE);

	}
}