- #ROTATING GRAVITY SOURCE ENGINE SDK HOW TO#
- #ROTATING GRAVITY SOURCE ENGINE SDK ANDROID#
- #ROTATING GRAVITY SOURCE ENGINE SDK CODE#
Multiply the current rotation by the accumulated rotation, and then set the accumulated Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 0.0f) Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f) Set a matrix that contains the current rotation.
#ROTATING GRAVITY SOURCE ENGINE SDK CODE#
Here’s what our matrix code looks like in the onDrawFrame method: // Draw a cube. Initialize the accumulated rotation matrix Private final float mCurrentRotation = new float void onSurfaceCreated(GL10 glUnused, EGLConfig config) Private final float mAccumulatedRotation = new float I have left the code like this to bring up this point as an exercise for the reader I leave it to you to add synchronized statements around the public variable read and write pairs instead of using volatile variables.įirst, let’s add a couple of matrices and initialize them: /** Store the accumulated rotation. Remember, on Android, the OpenGL renderer runs in a different thread than the UI event handler thread, and there is a slim chance that the other thread fires in-between the X and Y variable assignments (there are also additional points of contention with the += syntax).
We apply those directly to the renderer to a couple of public variables that we have also declared as volatile, so that they can be updated between threads. We then divide that delta offset by the pixel density and a slowing factor of 2.0f to get our final delta values. If (event.getAction() = MotionEvent.ACTION_MOVE)įloat deltaX = (x - mPreviousX) / mDensity / 2f įloat deltaY = (y - mPreviousY) / mDensity / 2f Įvery frame, we compare the current pointer position with the previous, and use that to calculate the delta offset. Then we add our touch event handler to our custom GLSurfaceView: public boolean onTouchEvent(MotionEvent event) GetWindowManager().getDefaultDisplay().getMetrics(displayMetrics)
#ROTATING GRAVITY SOURCE ENGINE SDK HOW TO#
Here’s how to get the pixel density: final DisplayMetrics displayMetrics = new DisplayMetrics() We also store the screen density so that drags across the screen can move the object a consistent amount across devices, regardless of the pixel density. We will store the previous pointer position each frame, so that we can calculate the relative movement left, right, up, or down. In LessonSixGLSurfaceView.java, we declare a few member variables: private float mPreviousX
#ROTATING GRAVITY SOURCE ENGINE SDK ANDROID#
The code is based on Android Lesson Six: An Introduction to Texture Filtering. The code examples here are written for Android, but can easily be adapted to any platform running OpenGL ES.
What this means is that drags left, right, up, and down will always move the cube in the direction that we expect. This delta will be used to rotate our accumulated rotation matrix.