歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 3D物體的碰撞——正方體的碰撞

Android 3D物體的碰撞——正方體的碰撞

日期:2017/3/1 11:09:27   编辑:Linux編程

3D物體的碰撞和2D類似,都是根據坐標來計算物體的距離,判斷是否碰撞。下面舉個簡單的列子吧,我這個列子比較局限,簡單,只是為了說明這個方法而已,大家可以參照方法進行改進,下面看看代碼吧。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:id="@+id/main_liner"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. </LinearLayout>
  1. package yy.cal;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.LinearLayout;
  5. public class GLSurfaceViewActivity extends Activity {
  6. private MySurfaceView mSurfaceView;//聲明MySurfaceView對象
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. mSurfaceView=new MySurfaceView(this);//創建MySurfaceView對象
  11. mSurfaceView.requestFocus();//獲取焦點
  12. mSurfaceView.setFocusableInTouchMode(true);//設置為可觸控
  13. LinearLayout ll=(LinearLayout)this.findViewById(R.id.main_liner);//獲得線性布局的引用
  14. ll.addView(mSurfaceView);
  15. }
  16. @Override
  17. protected void onPause() {
  18. // TODO Auto-generated method stub
  19. super.onPause();
  20. mSurfaceView.onPause();
  21. }
  22. @Override
  23. protected void onResume() {
  24. // TODO Auto-generated method stub
  25. super.onResume();
  26. mSurfaceView.onResume();
  27. }
  28. }
Copyright © Linux教程網 All Rights Reserved