3D物體的碰撞和2D類似,都是根據坐標來計算物體的距離,判斷是否碰撞。下面舉個簡單的列子吧,我這個列子比較局限,簡單,只是為了說明這個方法而已,大家可以參照方法進行改進,下面看看代碼吧。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:id="@+id/main_liner"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </LinearLayout>
- package yy.cal;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.LinearLayout;
-
- public class GLSurfaceViewActivity extends Activity {
- private MySurfaceView mSurfaceView;//聲明MySurfaceView對象
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mSurfaceView=new MySurfaceView(this);//創建MySurfaceView對象
- mSurfaceView.requestFocus();//獲取焦點
- mSurfaceView.setFocusableInTouchMode(true);//設置為可觸控
- LinearLayout ll=(LinearLayout)this.findViewById(R.id.main_liner);//獲得線性布局的引用
- ll.addView(mSurfaceView);
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- mSurfaceView.onPause();
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- mSurfaceView.onResume();
- }
- }