public Vector3 calculatePos(int x, int y, Object3D object3D) { Matrix4 mViewMatrix = getCurrentCamera().getViewMatrix(); Matrix4 mProjectionMatrix = getCurrentCamera().getProjectionMatrix(); double[] mNearPos4 = new double[4]; double[] mFarPos4 = new double[4]; Vector3 mNearPos = new Vector3(); Vector3 mFarPos = new Vector3(); Vector3 mNewObjPos = new Vector3(); int[] mViewport = new int[] {0, 0, getViewportWidth(), getViewportHeight()}; GLU.gluUnProject( x, getViewportHeight() - y, 0, mViewMatrix.getDoubleValues(), 0, mProjectionMatrix.getDoubleValues(), 0, mViewport, 0, mNearPos4, 0); GLU.gluUnProject( x, getViewportHeight() - y, 1.f, mViewMatrix.getDoubleValues(), 0, mProjectionMatrix.getDoubleValues(), 0, mViewport, 0, mFarPos4, 0); mNearPos.setAll( mNearPos4[0] / mNearPos4[3], mNearPos4[1] / mNearPos4[3], mNearPos4[2] / mNearPos4[3]); mFarPos.setAll( mFarPos4[0] / mFarPos4[3], mFarPos4[1] / mFarPos4[3], mFarPos4[2] / mFarPos4[3]); double factor = (Math.abs(object3D.getZ()) + mNearPos.z) / (getCurrentCamera().getFarPlane() - getCurrentCamera().getNearPlane()); mNewObjPos.setAll(mFarPos); mNewObjPos.subtract(mNearPos); mNewObjPos.multiply(factor); mNewObjPos.add(mNearPos); return mNewObjPos; }
@Override protected void onRender(long ellapsedRealtime, double deltaTime) { // -- no proper physics here, just a bad approximation to keep // this example as short as possible ;-) mRaptor.setZ(mRaptor.getZ() + 2.0); mRaptor.setX(Math.sin(mTime) * 20.0); // mRaptor.setRotZ(Math.sin(mTime + 8.0) * -30.0); // mRaptor.setRotY(180 + (mRaptor.getRotZ() * 0.1)); // mRaptor.setRotY(180); mRaptor.setY(Math.cos(mTime) * 10.0); mRaptor.setRotX(Math.cos(mTime + 1.0) * -20.0); mSphere.setZ(mRaptor.getZ()); mTime += 0.01; if (mRootCube.getZ() - mRaptor.getZ() <= (30 * -6)) { mRootCube.setZ(mRaptor.getZ()); } mPointLight.setPosition(getCurrentCamera().getPosition()); mPointLight.setLookAt(mRaptor.getWorldPosition()); super.onRender(ellapsedRealtime, deltaTime); }