android3D物体的碰撞——正方体的碰撞
3D物体的碰撞和2D类似,都是根据坐标来计算物体的距离,判断是否碰撞。下面举个简单的列子吧,我这个列子比较局限,简单,只是为了说明这个方法而已,大家可以参照方法进行改进,下面看看代码吧。
<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 GLSu易做图ceViewActivity extends Activity {
private MySu易做图ceView mSu易做图ceView;//声明MySu易做图ceView对象
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSu易做图ceView=new MySu易做图ceView(this);//创建MySu易做图ceView对象
mSu易做图ceView.requestFocus();//获取焦点
mSu易做图ceView.setFocusableInTouchMode(true);//设置为可触控
LinearLayout ll=(LinearLayout)this.findViewById(R.id.main_liner);//获得线性布局的引用
ll.addView(mSu易做图ceView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mSu易做图ceView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mSu易做图ceView.onResume();
}
}
package yy.cal;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSu易做图ceView;
import android.opengl.GLU;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class MySu易做图ceView extends GLSu易做图ceView{
private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
private SceneRenderer mRenderer;//场景渲染器
private float mPreviousX;//上次的触控位置X坐标
public MySu易做图ceView(Context context) {
super(context);
mRenderer = new SceneRenderer(); //创建场景渲染器
setRenderer(mRenderer); //设置渲染器
setRenderMode(GLSu易做图ceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
}
//触摸事件回调方法
@Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;//计算触控笔X位移
mRenderer.angle += dx * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角度
requestRender();
//重绘画面
}
mPreviousX = x;//记录触控笔位置
return true;
}
public boolean onKeyDown(int keyCode,KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)
{
mRenderer.x +=0.5f;
}
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){
mRenderer.x -=0.5f;
}
if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT){
mRenderer.angle+=90;
}
if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
mRenderer.angle+=90;
}
return super.onKeyDown(keyCode, event);
}
private class SceneRenderer implements GLSu易做图ceView.Renderer
补充:移动开发 , Android ,