ROtation of cube controlled by mouse movement

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

ROtation of cube controlled by mouse movement

Postby sonia.gopalakrishnan » Mon Oct 04, 2010 6:22 am

Hi,
I am a beginner in Android app developmet. I have a rotating cube, I need to control its rotation using either mouse or by arrow keys. Can anyone help me in this issue. I am new to OpenGl so juz give me an idea from the very basics? Where should i start from?
Thanks in advance
sonia.gopalakrishnan
Freshman
Freshman
 
Posts: 5
Joined: Mon Oct 04, 2010 5:08 am

Top

Re: ROtation of cube controlled by mouse movement

Postby SilverLogic » Thu Oct 21, 2010 10:41 pm

You'll want to attach an OnTouchListener event to your view. In this event you can get rotation using the 'event' variable that gets passed to the input listener, I used something like this in my game engine to retrieve a value that equals the amount of change in whatever direction, (so if you swipe your finger vertically an inch, or about 50 units, the code gets the original mouse down position, and subtracts this from the new position)
Code: Select all
   @Override
   public boolean onTouch(View v, MotionEvent event) {
      switch(event.getAction()) {
      case MotionEvent.ACTION_DOWN:
         origX = event.getX();
         origY = event.getY();
         break;
      case MotionEvent.ACTION_MOVE:
         changeX = event.getX() - origX;
         changeY = event.getY() - origY;
         origX = event.getX();
         origY = event.getY();
         break;
      case MotionEvent.ACTION_UP:
         changeX = 0;
         changeY = 0;
         break;
      }
      return false;
   }

// These are used to retrieve any change in screen touch 'move' events
   public static float GetChangeX() { return changeX; }
   public static float GetChangeY() { return changeY; }


And then simply use this code in your games render() method to change rotation:
Code: Select all
rotationX = Input.GetChangeX();
rotationY = Input.GetChangeY();
      
myModel.Rotate(rotationX, rotationY, 0);


You may have to tweak the render code a little, my games all use landscape orientation so its a slightly altered version of this, but you'll figure it out. Good luck, hope this helps!

p.s. - I strongly suggest checking out these resources:
http://developer.android.com/guide/topics/ui/ui-events.html
http://developer.android.com/reference/android/view/View.OnTouchListener.html
User avatar
SilverLogic
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Oct 21, 2010 10:31 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests