I'm trying to ray-cast mouse clicks to the surface of a sphere (from which point, I'm going to get the coordinates of the vertex of the sphere's mesh that are closest to the click). The following Raycast code is not working, however, and I can't seem to figure out why:
public Vector3 rayCastToSphere(Vector3 origPos, Vector3 sphereCenter, Vector3 dir, float radius) { Vector3 output = new Vector3(); ray.set(origPos, sphereCenter); ray.direction.set(dir); ray.origin.set(origPos); boolean intersection = Intersector.intersectRaySphere(ray, sphereCenter, radius, output); Gdx.app.log("SphereCast Debug", output.toString() + ", Did it Intersect? " + intersection); return output; } My logging code here reveals that the raycasted click is never intersecting the sphere (thus it isn't returning a point at which the sphere is intersecting the click).
And this is the code I'm using to call the raycast from when the screen is clicked:
SphereRayCaster sr = new SphereRayCaster(); @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { Gdx.app.log("CamPos Debug", cam.position.toString()); Vector3 touchPos = new Vector3(screenX, screenY, 0f); Gdx.app.log("TouchPos", touchPos.toString()); Vector3 projectedTouchPos = cam.unproject(touchPos); projectedTouchPos.z = cam.position.z; Gdx.app.log("Projected TouchPos", projectedTouchPos.toString()); Vector3 rayCastOutput = sr.rayCastToSphere(projectedTouchPos, cam.direction, hexasphere.getCenter(), sphere.getRadius()); Gdx.app.log("Raycast Output Debug", rayCastOutput.toString()); //hexasphere.everyOther(); return true; }
rayin the 1st method you posted? There is no parameter or variable calledray. \$\endgroup\$