void GridSystem::forceGroundedView(PerspectiveCamera &pc) { float lookAt_distance = offset/2; float old_distance = pc.GetDistance(); //need? Vector3f _p = pc.getCameraLocation(); Vector3f p = _p - Vector3f(0, _p.y(), 0); //xz only Vector3f _vec = pc.GetCenter() - p; Vector3f vec = _vec - Vector3f(0, _vec.y(), 0); Vector3f lookAt = p + ((offset*2) * vec.normalized()); //from camera to lookAt direction (xz only) Vector3f p_ground = Vector3f(p.x(), getYLevel(p.x(), p.z()) + 0.5f, p.z()); Vector3f lookAt_ground = Vector3f(lookAt.x(), getYLevel(lookAt.x(), lookAt.z()) + 0.5f, lookAt.z()); Vector3f nvec = (lookAt_ground - p_ground); //new camera location to new camera center Vector3f lookAt_final = p_ground + (lookAt_distance * nvec.normalized()); //force distance to be at ground pc.SetCenter(lookAt_final); //apply new camera center //pc.SetCenter(lookAt_ground); //apply new camera center /*if (pc.GetCenter().y() < lookAt_ground.y() - 0.2){ pc.SetCenter(lookAt_ground - Vector3f(0, 0.2, 0)); //apply new camera center } else if (pc.GetCenter().y() > lookAt_ground.y() + 0.2) { pc.SetCenter(lookAt_ground + Vector3f(0, 0.2, 0)); }*/ //float lookAt_distance = sqrt(pow(nvec.x(), 2) + pow(nvec.y(), 2) + pow(nvec.z(), 2)); pc.SetDistance(lookAt_distance); Vector3f rotaxis = Vector3f::cross(nvec, Vector3f::UP); float angle_cur = atan((lookAt.y() - p_ground.y())/old_distance); float angle_new = atan((lookAt_ground.y() - p_ground.y())/lookAt_distance); float angle = angle_new - angle_cur; pc.SetViewRotation(RotationMatrixOnAxis(angle, rotaxis.x(), rotaxis.y(), rotaxis.z())); ////set view to correct Z axis //pc.SetCenter(lookAt_ground); //apply new camera center //Vector3f nvec = (lookAt_ground - p_ground); //new camera location to new camera center (alrd applied) ////float lookAt_distance = sqrt(pow(nvec.x(), 2) + pow(nvec.y(), 2) + pow(nvec.z(), 2)); //float lookAt_distance = lookAtDistance; //pc.SetDistance(lookAt_distance); cout << "p:" << p_ground.x() << " " << p_ground.y() << " " << p_ground.z() << endl; cout << "lookAt:" << lookAt_ground.x() << " " << lookAt_ground.y() << " " << lookAt_ground.z() << endl; //cout << "test:" << test.x() << " " << test.y() << " " << test.z() << endl; //cout << p.x() << " " << p.z() << " " << getYLevel(0.758, 0.875) << endl; }