Opengl es Android 3D 手指觸控旋轉object

來源:互聯網
上載者:User

標籤:case   代碼   計算   object   class   return   trend   觸控   lex   

明確原理:

先確定一下OpenGL裡面的座標為:x軸正方向為螢幕向右,y軸正方向為螢幕向上,z軸正方向為螢幕由裡向外。

當手指在X軸方向上移動時,畫面中物體應繞Y軸方向旋轉,則為gl.glRotatef(Angle,0,1,0);

當手指在Y軸方向上移動時,畫面中物體應繞X軸方向旋轉,則為gl.glRotatef(Angle,1,0,0);

除此之外,android中螢幕響應手勢移動所應用的function為:onTouchEvent.

下面展示代碼:

private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
renderer = new NewRender();
public boolean onTouchEvent(MotionEvent e) { switch (e.getAction()) { case MotionEvent.ACTION_DOWN: ypos = e.getY(); xpos = e.getX(); return true; case MotionEvent.ACTION_MOVE: float dy = e.getY() - ypos;//計算觸控筆Y位移 float dx = e.getX() - xpos;//計算觸控筆X位移 renderer.center.mAngleX = dy * TOUCH_SCALE_FACTOR;//設定沿x軸旋轉角度 renderer.center.mAngleY = dx * TOUCH_SCALE_FACTOR;//設定沿y軸旋轉角度 requestRender();//重繪畫面 return true; } return true; }

NewRender()是我繼承Renderer建立的一個class.

renderer.center.mAngleX = dy * TOUCH_SCALE_FACTOR;//設定沿x軸旋轉角度  renderer.center.mAngleY = dx * TOUCH_SCALE_FACTOR;//設定沿y軸旋轉角度  

center.mAngleX 和 center.mAngleY是指我的Object(center)的旋轉角度。

在center中,我的旋轉角度設定為:

gl.glRotatef(mAngleX, 1, 0, 0);//旋轉  gl.glRotatef(mAngleY, 0, 1, 0);  
gl.glRotatef(mAngleZ, 0, 0, 1);

 

Opengl es Android 3D 手指觸控旋轉object

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.