Search code examples
javaandroidtouchopengl-es-2.0particle-system

Android OpenGL ES Teapot Explosion Implementation


Hi I have successfully rendered Utah Teapot in OpenlGL ES 2.0. Currently I am trying to implement touch events so that whenever I touch the teapot it will explode.

My question is where should I implement the touch event in Renderer Class or in GLSurfaceView? and how do i make the teapot explode. Thank you in advance. I am new in Android Any suggestion is highly appreciated


Solution

  • About the touch event:

    You would need to show the system/architecture you are currently having. Generally you would have a separate class that controls the scene. In your case you may only have a single object that contains the teapot and all its values that are needed to draw or move or explode... So this class should be initialized and owned by the surface view or its parent. In both cases the surface view would have the access to the scene. Now if I assume the renderer is responsible for drawing then the surface view is the owner of the renderer and would call something similar to this.myRenderer.drawScene(this.myScene) or if the renderer has a control on the draw initialization then the surface view must forward the access to the scene so this.myRenderer.setScene(this.myScene). So in the end both of the classes have the access to the scene and with it to the teapot object.

    Now to handle the touch event you will need to check what is the nearest location you can intercept a touch event. If the surface view can intercept these calls then implement it there. If not I am sure the owner of the surface view can, in this case the owner would call this.mySurfaceView.handleTouchEvent(touch).

    Now the surface view can optionally do some checks to see if the pot was hit and begin the explosion procedure. This might be as easy as only calling a method on the teapot like this.myScene.teapotObject.explode().

    About the explosion itself:

    There are many ways on how to explode an object and generally none of them are easy. The minimum would most likely be to have a system where your vertex buffer will be split into smaller chunks where each of the chunk is then misplaced animatedly while exploding.

    Even creating an animation might be a hard procedure. One way is to create an interpolation where you would have an animation start time startTimeStamp, its duration, object start position startPosition and object end position endPosition. Then the current position is retrieved by currentPosition = startPosition + (endPosition -startPosition)*(startTimeStamp-currentTimeStamp)/duration. Another way is to implement physics where an object would move on every frame. In this case you define the objects speed and then on every frame you would call teapot.chunk.move(1.0/60.0) which would then do this.position += this.speed*dt. You can then add gravity to manipulate speed or collisions...