Android3d object collision-cube collision

Source: Internet
Author: User


The collision between 3D objects is similar to that of 2D objects. It calculates the distance between objects based on coordinates and determines whether a collision is performed. The following is a simple column. My column is relatively limited and simple, just to illustrate this method. You can refer to the method for improvement. Let's take a look at the code.

 

 

 

 

 

<?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; // declare the mysurfaceview object public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); msurfaceview = new mysurfaceview (this); // create the mysurfaceview object msurfaceview. requestfocus (); // obtain the focus msurfaceview. setfocusableintouchmode (true); // set to touch linearlayout LL = (linearlayout) This. findviewbyid (R. id. main_liner); // obtain linear layout references ll. addview (msurfaceview) ;}@ overrideprotected void onpause () {// todo auto-generated method stubsuper. onpause (); msurfaceview. onpause () ;}@ overrideprotected void onresume () {// todo auto-generated method stubsuper. onresume (); msurfaceview. onresume ();}}

 

 

 

Package yy. cal; import javax. microedition. khronos. EGL. eglconfig; import javax. microedition. khronos. opengles. gl10; import android. content. context; import android. openGL. glsurfaceview; import android. openGL. glu; import android. view. keyevent; import android. view. motionevent; public class mysurfaceview extends glsurfaceview {private final float touch_scale_factor = 180.0f/320; // Private scenerenderer mrenderer; // scene Renderer private float mpreviousx; // last touch position X coordinate public mysurfaceview (context) {super (context); mrenderer = new scenerenderer (); // create scene Renderer setrenderer (mrenderer ); // set the setrendermode (glsurfaceview. rendermode_continuously); // sets the rendering mode to active rendering} // The callback method of the touch event @ override public Boolean ontouchevent (motionevent e) {float x = E. getx (); Switch (E. getaction () {Case motionevent. action_move: Float dx = x-mpreviousx; // calculate the position of the touch pen x mrenderer. angle + = DX * touch_scale_factor; // set the Rotation Angle along the X axis requestrender (); // re-painting surface} mpreviousx = x; // record the touch pen position 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 glsurfaceview. renderer {cube = new cube (); // cube float angle = 45; // total rotation angle float x = 0, y = 0, Z = 0; public void ondrawframe (gl10 GL) {// set to open the back and crop GL. glable (gl10.gl _ cull_face); // set the color model to smooth color GL. glshademodel (gl10.gl _ smooth); // clear the color cached in the deep cache GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // set the current matrix to the mode matrix GL. glmatrixmode (gl10.gl _ modelview); // sets the current matrix as the unit matrix GL. glloadidentity (); Glu. glulookat // The angle of view that is unlikely to be deformed-a small angle of view (GL, 0f, // X 10f of the human eye position, // y 15.0f of the human eye position, // Z 0 of the human eye position, // point of human eye x 0f, // point of human eye x 0, // point of human eye x 0, 1, 0 ); // rotate the total coordinate system GL. glrotatef (angle, 0, 1, 0); // draw the right cube GL. glpushmatrix (); GL. gltranslatef (x, y, z); cube. drawself (GL); GL. glpopmatrix (); GL. glpushmatrix (); GL. gltranslatef (2, 0, 0); cube. drawself (GL); GL. glpopmatrix (); If (x> 1.0f & x <3.0f) {colorrect. flag = false;} else {colorrect. flag = true ;}} public void onsurfacechanged (gl10 GL, int width, int height) {// set the window size and position GL. glviewport (0, 0, width, height); // sets the current matrix as the projection matrix GL. glmatrixmode (gl10.gl _ projection); // sets the current matrix as the unit matrix GL. glloadidentity (); // calculate the ratio of Perspective Projection float ratio = (float) height/width; // call this method to calculate and generate a Perspective Projection Matrix // GL. glfrustumf (-1, 1,-ratio, ratio, 1,100); // The angle of view that may be deformed-large-angle GL. glfrustumf (-1, 1,-ratio, ratio, 8f, 100); // The angle of view that is unlikely to be deformed-Small Angle} public void onsurfacecreated (gl10 GL, eglconfig config) {// disable anti-jitter GL. gldisable (gl10.gl _ dither); // sets the mode of a specific hint project. Here, it is set to use the Quick Mode GL. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest); // sets the screen background color to black rgba GL. glclearcolor (,); // enable the deep test GL. glenable (gl10.gl _ depth_test );}}}

 

 

Package yy. cal; import javax. microedition. khronos. opengles. gl10; import static yy. cal. constant. *; public class cube {// used to draw the color rectangle of each plane colorrect Cr = new colorrect (scale, scale); Public void drawself (gl10 GL) {// The overall drawing idea: by rotating a colored rectangle to the position of each plane of the cube // draw the GL of each plane of the cube. glpushmatrix (); // draw the front and back area GL. glpushmatrix (); GL. gltranslatef (0, 0, unit_size * scale); CR. drawself (GL); GL. glpopmatrix (); // after painting, the small GL. glpushmatrix (); GL. gltranslatef (0, 0,-unit_size * scale); GL. glrotatef (180, 0, 1, 0); CR. drawself (GL); GL. glpopmatrix (); // draw a large GL. glpushmatrix (); GL. gltranslatef (0, unit_size * scale, 0); GL. glrotatef (-90, 1, 0, 0); CR. drawself (GL); GL. glpopmatrix (); // draw the big face GL. glpushmatrix (); GL. gltranslatef (0,-unit_size * scale, 0); GL. glrotatef (90, 1, 0, 0); CR. drawself (GL); GL. glpopmatrix (); // draw the left wide GL. glpushmatrix (); GL. gltranslatef (unit_size * scale, 0, 0); GL. glrotatef (-90, 1, 0, 0); GL. glrotatef (90, 0, 1, 0); CR. drawself (GL); GL. glpopmatrix (); // draw the right big face GL. glpushmatrix (); GL. gltranslatef (-unit_size * scale, 0, 0); GL. glrotatef (90, 1, 0, 0); GL. glrotatef (-90, 0, 1, 0); CR. drawself (GL); GL. glpopmatrix (); GL. glpopmatrix ();}}

 

 

 

Package yy. cal; import Java. NIO. bytebuffer; import Java. NIO. byteorder; import Java. NIO. floatbuffer; import Java. NIO. intbuffer; import javax. microedition. khronos. opengles. gl10; public class colorrect {private floatbuffer mvertexbuffer; // vertex coordinate data buffer private intbuffer mcolorbuffer, mcolorbuffer1, mcolor; // Vertex coloring data buffer int vcount = 0; // Number of vertices static Boolean flag = true; Public colorrect (float width, float height) {// initialize the vertex coordinate data ============================ begin ==================== ============= vcount = 6; final float unit_size = 1.0f; float vertices [] = new float [] {0, 0, width * unit_size, height * unit_size, 0,-width * unit_size, height * unit_size, 0,-width * unit_size,-height * unit_size, 0, width * unit_size,-height * unit_size, 0, width * unit_size, height * unit_size, 0 }; // create a vertex coordinate data buffer // vertices. length * 4 is because an integer is four bytes bytebuffer vBB = bytebuffer. allocatedirect (vertices. length * 4); vBB. order (byteorder. nativeorder (); // sets the byte sequence mvertexbuffer = vBB. asfloatbuffer (); // converts it to a float buffer mvertexbuffer. put (vertices); // puts the vertex coordinate data in the buffer zone mvertexbuffer. position (0); // set the starting position of the buffer. // Note: The bytebuffer must be converted because the byte sequence and data units of different platforms are not bytes, the key is to set nativeorder () through byteorder (), otherwise, the initialization of the vertex coordinate data may fail. ==========================/// initialize the vertex coloring data ===================== begin ========================== final int one = 65535; int colors [] = new int [] // an array of vertex color values. Each vertex has four color values: rgba {one, 0, one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, one, 0, 0, 0, one, 0,}; // create a vertex coloring data buffer // vertices. length * 4 is because a four-bytebuffer CBB = bytebuffer integer of the int type. allocatedirect (colors. length * 4); CBB. order (byteorder. nativeorder (); // sets the byte sequence mcolorbuffer = CBB. asintbuffer (); // converts it to an int-Type Buffer mcolorbuffer. put (colors); // Add the vertex coloring data mcolorbuffer to the buffer. position (0); // set the starting position of the buffer int colors1 [] = new int [] {, one, 0, one, 0, 0, 0, one, 0, one, one, one, 0, 0, 0, one, 0, 0, one, 0,}; bytebuffer cbb1 = bytebuffer. allocatedirect (colors1.length * 4); cbb1.order (byteorder. nativeorder (); mcolorbuffer1 = cbb1.asintbuffer (); // convert it to int-Type Buffer mcolorbuffer1.put (colors1); // Add the vertex coloring data mcolorbuffer1.position (0) to the buffer ); // set the starting position of the buffer. // Note: Because the byte sequence and data units of different platforms are not bytes, they must be converted to bytebuffer. The key is to set nativeorder () through byteorder (), otherwise, problems may occur. // initialize Vertex coloring data ======================== =======================} public void drawself (gl10 GL) {GL. glableclientstate (gl10.gl _ vertex_array); // enables the vertex coordinate array GL. glableclientstate (gl10.gl _ color_array); // enable the vertex color array // specify the vertex coordinate data for the paint brush GL. glvertexpointer (3, // The number of coordinates for each vertex is 3 XYZ gl10.gl _ float, // The type of the vertex coordinate value is gl_fixed 0, // interval between consecutive vertex coordinate data mvertexbuffer // vertex coordinate data); If (FLAG) {mcolor = mcolorbuffer;} else {mcolor = mcolorbuffer1 ;} // specify the vertex coloring data GL for the paint brush. glcolorpointer (4, // set the composition of the color, which must be 4-rgba gl10.gl _ fixed, // The type of the vertex color value is gl_fixed 0, // interval between consecutive Vertex coloring data mcolor // Vertex coloring data); // draw the graph GL. gldrawarrays (gl10.gl _ triangle_fan, // fill in 0 as a triangle, // start point number vcount // Number of vertices );}}

 

 

 

 

Package yy. Cal; public class constant {public static final float unit_size = 1.0f; // The unit size is public static final float scale = 0.5f; // The size scaling ratio}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.