Using Linear Interpolation to map android touch screen values to OpenGL's coordinate system

Table of contents The code What we are trying to do Non-formal definition Linear Interpolation Point slope form and equations My app on the Google play store The app My app's GitHub code The app's GitHub code Resources Explained: Linear Interpolation (YouTube video) Linear Interpolation Formula The equation simplified in Kotlin override fun onTouch(v: View?, event: MotionEvent?): Boolean { // Get screen dimensions val width = width.toFloat() val height = height.toFloat() // Convert screen coordinates to OpenGL coordinates val glX = (2.0f * event.x / width) - 1.0f val glY = 1.0f - (2.0f * event.y / height) } What we are trying to do So, I need to be able to convert Android's pixel based grid system to OpenGL's coordinate system. This will allow my user to interact with a ping pong game I created in OpenGL. Below is the feature where my user can watch their streams and play ping pong against an AI. Non-formal definition As it turns out, As long as we have the boundaries of the two graphs. We can map values between them using Linear Interpolation Linear Interpolation Apple's definition of linear interpolation: Linear interpolation is a method of calculating intermediate data between known values by conceptually drawing a straight line between two adjacent known values Which, is really just a fancy way to say, if we have two points, we can find the slope and if we have the slope we can find the next point. This really means that we can use the Point Slope Form(y−y1=m(x−x 1 ​)) to derive our equation. Point slope form and equations y−y1=m(x−x1) (

Mar 8, 2025 - 21:45
 0
Using Linear Interpolation to map android touch screen values to OpenGL's coordinate system

Table of contents

  1. The code
  2. What we are trying to do
  3. Non-formal definition
  4. Linear Interpolation
  5. Point slope form and equations

My app on the Google play store

My app's GitHub code

Resources

The equation simplified in Kotlin

 override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        // Get screen dimensions
        val width = width.toFloat()
        val height = height.toFloat()

        // Convert screen coordinates to OpenGL coordinates
        val glX = (2.0f * event.x / width) - 1.0f
        val glY = 1.0f - (2.0f * event.y / height)
}

What we are trying to do

  • So, I need to be able to convert Android's pixel based grid system to OpenGL's coordinate system. This will allow my user to interact with a ping pong game I created in OpenGL. Below is the feature where my user can watch their streams and play ping pong against an AI.

ping pong feature

Non-formal definition

  • As it turns out, As long as we have the boundaries of the two graphs. We can map values between them using Linear Interpolation

Linear Interpolation

  • Apple's definition of linear interpolation: Linear interpolation is a method of calculating intermediate data between known values by conceptually drawing a straight line between two adjacent known values
  • Which, is really just a fancy way to say, if we have two points, we can find the slope and if we have the slope we can find the next point. This really means that we can use the Point Slope Form(y−y1=m(x−x 1 ​)) to derive our equation.

Point slope form and equations

  • y−y1=m(x−x1)
  • (