Make lookAtVectors lock on other avatar's eyes if the mouse is over

the other avatar's head.

Add rayIntersectsSphere function to Util. glm::intersectRaySphere was buggy.
This commit is contained in:
Mark Peng 2013-07-10 15:56:23 -07:00
parent 4b6e7f1079
commit 0b334f9e5d
3 changed files with 13 additions and 4 deletions

View file

@ -1727,9 +1727,8 @@ bool Application::isLookingAtOtherAvatar(glm::vec3 &mouseRayOrigin, glm::vec3 &m
if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) {
Avatar *avatar = (Avatar *)node->getLinkedData();
glm::vec3 headPosition = avatar->getHead().getPosition();
glm::vec3 intersectionPosition, intersectionNormal;
printf("x: %f y: %f z: %f \n", mouseRayOrigin.x, mouseRayOrigin.y, mouseRayOrigin.z);
if (glm::intersectRaySphere(mouseRayOrigin, mouseRayDirection, headPosition, 10, intersectionPosition, intersectionNormal)) {
if (rayIntersectsSphere(mouseRayOrigin, mouseRayDirection, headPosition, 0.07)) {
printf("lalala\n");
eyePosition = avatar->getHead().getEyeLevelPosition();
return true;
@ -1760,8 +1759,6 @@ void Application::update(float deltaTime) {
glm::vec3 mouseRayOrigin, mouseRayDirection;
_viewFrustum.computePickRay(_mouseX / (float)_glWidget->width(),
_mouseY / (float)_glWidget->height(), mouseRayOrigin, mouseRayDirection);
// printf("x: %d y: %d \n", _mouseX, _mouseY);
// printf("x: %f y: %f z: %f \n", mouseRayOrigin.x, mouseRayOrigin.y, mouseRayOrigin.z);
// tell my avatar the posiion and direction of the ray projected ino the world based on the mouse position
_myAvatar.setMouseRay(mouseRayOrigin, mouseRayDirection);

View file

@ -509,3 +509,13 @@ float loadSetting(QSettings* settings, const char* name, float defaultValue) {
}
return value;
}
bool rayIntersectsSphere(glm::vec3 &rayStarting, glm::vec3 &rayNormalizedDirection, glm::vec3 &sphereCenter, double sphereRadius) {
glm::vec3 vecFromRayToSphereCenter = sphereCenter - rayStarting;
double projection = glm::dot(vecFromRayToSphereCenter, rayNormalizedDirection);
double shortestDistance = sqrt(glm::dot(vecFromRayToSphereCenter, vecFromRayToSphereCenter) - projection*projection);
if (shortestDistance <= sphereRadius) {
return true;
}
return false;
}

View file

@ -71,4 +71,6 @@ void runTimingTests();
float loadSetting(QSettings* settings, const char* name, float defaultValue);
bool rayIntersectsSphere(glm::vec3 &rayStarting, glm::vec3 &rayNormalizedDirection, glm::vec3 &sphereCenter, double sphereRadius);
#endif