SoFunction
Updated on 2025-03-02

Detailed explanation of the usage examples of Android game development learning engine

This article describes the usage of Android game development and learning engines. Share it for your reference. The details are as follows:

Car engines are the heart of cars, which determine the performance and stability of cars and are something people pay great attention to when buying cars. The physics engine in the game occupies a very important position just like the engine of a car. A good physics engine can simulate the real world very realistically, making the game more realistic and providing a better entertainment experience.

1. Introduction to JBox2D

JBox2D is a Java version of the open source physics engine Box2D, which can be used directly on Android. Since JBox2D's graphics rendering uses the Processing library, when using JBox2D on the Android platform, graphics rendering can only be developed by yourself. The engine can automatically perform all-round simulation of the physical motion of 2D rigid bodies based on parameters set by developers, such as gravity, density, friction coefficient and elastic coefficient.

2. Example

1. Small ball bounce advanced version

In Section 1, the whereabouts, collisions and bounces of small balls are maintained by code. The following is a physical engine to implement it, and collisions between rigid bodies are added.

(1) Constant class Constant

package ; 
public class Constant { 
  public static final float RATE=10; //The ratio of screen to real world  public static final boolean DRAW_THREAD_FLAG=true; //Draw thread work identification bit  public static final float TIME_STEP=2.0f/60.0f; //Simulated frequency  public static final int ITERA=10; //Number of iterations  public static int SCREEN_WIDTH; //Screen Width  public static int SCREEN_HEIGHT; //Screen height}

(2) Abstract class MyBody

This class is a custom abstract class and is the base class of all custom rigid body classes. Since the rigid body object in JBox2D only has the function of physical simulation calculation and does not provide the drawing function under the Android platform, it is not very convenient to use directly. Therefore, here, MyBody is defined to encapsulate custom rigid bodies and JBox2D physical simulation objects.

package ; 
import org.; 
import ; 
import ; 
public abstract class MyBody { 
  Body body; //The rigid body in JBox2D physics engine  int color; //The color of the rigid body  public abstract void drawSelf(Canvas canvas,Paint paint); //Drawing method}

(3) Circular rigid body type MyCircleColor

package ; 
import org.; 
import ; 
import ; 
import ; 
import static .*; // Static importpublic class MyCircleColor extends MyBody { 
  float radius; //Circular radius  public MyCircleColor(Body body,float radius,int color) { 
    =body; 
    =radius; 
    =color; 
  } 
  @Override 
  public void drawSelf(Canvas canvas, Paint paint) { 
    (color&0xCFFFFFF); //Set the color    float x=().x*RATE; 
    float y=().y*RATE; 
    (x, y, radius, paint); //Draw a circle    (); //Set hollow without filling    (1); 
    (color); //Draw the edge    (x, y, radius, paint); 
    (); //Restore brush settings  } 
}

(4) Rectangular rigid body type MyRectColor

package ; 
import static ; 
import org.; 
import ; 
import ; 
import ; 
public class MyRectColor extends MyBody { 
  float halfWidth;//Half width  float halfHeight;// Half height  public MyRectColor(Body body,float halfWidth,float halfHeight,int color) 
  { 
    =body; 
    =halfWidth; 
    =halfHeight;    
    =color; 
  } 
  public void drawSelf(Canvas canvas,Paint paint) 
  {      
    (color&0x8CFFFFFF);  
    float x=().x*RATE; 
    float y=().y*RATE; 
    float angle=(); 
    (); 
    Matrix m1=new Matrix(); 
    ((float)(angle),x, y); 
    (m1); 
    (x-halfWidth, y-halfHeight, x+halfWidth, y+halfHeight, paint);  
    (); 
    (1);//Set the line width    (color); 
    (x-halfWidth, y-halfHeight, x+halfWidth, y+halfHeight, paint);  
    ();    
    (); 
  } 
}

(5) Box2DUtil that generates rigid body shape

package ; 
import static ; 
import org.; 
import org.; 
import org.; 
import org.; 
import org.; 
public class Box2DUtil { 
  /**
    * Create rectangular objects (color)
    */ 
  public static MyRectColor createBox ( 
      float x, 
      float y, 
      float halfWidth, 
      float halfHeight, 
      boolean isStatic, //Is it still?      World world, 
      int color 
  ) { 
    PolygonDef shape=new PolygonDef(); //Create a polygon description object    if(isStatic) { 
      =0; 
    } else { 
      =1.0f; 
    } 
    =0.0f; //Set the friction coefficient    =0.6f; //Set the energy loss rate    (halfWidth/RATE, halfHeight/RATE); 
    BodyDef bodyDef=new BodyDef(); //Create a rigid body description object    (x/RATE,y/RATE); //Set the location    Body bodyTemp=(bodyDef); //Create rigid bodies in the world    (shape); //Specify the shape of the rigid body    (); //Set object mass    return new MyRectColor(bodyTemp, halfWidth, halfHeight, color);
  } 
  /**
    * Create a circular object (color)
    */ 
  public static MyCircleColor createCircle ( 
      float x, 
      float y, 
      float radius, 
      World world, 
      int color 
  ) { 
    CircleDef shape=new CircleDef(); //Create a circle description object    =2; //Set the density    =0.0f; //Set the friction coefficient    =0.95f; //Set the energy loss rate    =radius/RATE;//Set the radius    BodyDef bodyDef=new BodyDef(); //Create a rigid body description object    (x/RATE,y/RATE); //Set the location    Body bodyTemp=(bodyDef); //Create rigid bodies in the world    (shape); //Specify the shape of the rigid body    (); //Set object mass    return new MyCircleColor(bodyTemp, radius, color); 
  } 
}

(6) Color Util

package ; 
public class ColorUtil { 
  static int[][] result=  
    { 
      {56,225,254},   
      {41,246,239}, 
      {34,244,197}, 
      {44,241,161}, 
      {65,239,106}, 
      {45,238,59}, 
      {73,244,51},   
      {99,233,58}, 
      {129,243,34}, 
      {142,245,44}, 
      {187,243,32}, 
      {232,250,28}, 
      {242,230,46}, 
      {248,196,51}, 
      {244,125,31}, 
      {247,88,46}, 
      {249,70,40}, 
      {249,70,40}, 
      {248,48,48}, 
      {250,30,30}, 
      {252,15,15}, 
      {255,0,0},  
    }; 
    public static int getColor(int index) 
    { 
      int[] rgb=result[index%]; 
      int result=0xff000000; 
      result=result|(rgb[0]<<16); 
      result=result|(rgb[1]<<8); 
      result=result|(rgb[2]); 
      return result; 
    } 
}

(7) Main control class MyBox2dActivity

package ;    
import ; 
import ; 
import org.;   
import org..Vec2;   
import org.;    
import ;   
import ; 
import ;   
import ; 
import ;   
import ;  
import static .*; 
public class MyBox2dActivity extends Activity  
{   
  AABB worldAABB;//Create a world that manages collisions  World world; 
  Random random=new Random(); 
  //Object List  ArrayList&lt;MyBody&gt; bl=new ArrayList&lt;MyBody&gt;(); 
  public void onCreate(Bundle savedInstanceState)  
  {   
    (savedInstanceState); 
    //Set to full screen    requestWindowFeature(Window.FEATURE_NO_TITLE);   
    getWindow().setFlags(. FLAG_FULLSCREEN ,   
    . FLAG_FULLSCREEN);  
    //Set to landscape mode    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    //Get screen size    DisplayMetrics dm=new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm);  
    if(&lt;) 
    { 
       SCREEN_WIDTH=; 
       SCREEN_HEIGHT=; 
    } 
    else 
    { 
      SCREEN_WIDTH=; 
      SCREEN_HEIGHT=;   
    } 
    worldAABB = new AABB();   
    //The upper and lower bounds are taken as the origin of the upper left of the screen. If the created rigid body reaches the edge of the screen, the simulation will be stopped.    (-100.0f,-100.0f); 
    (100.0f, 100.0f);//Note that the real world units are used here    Vec2 gravity = new Vec2(0.0f,10.0f); 
    boolean doSleep = true; 
    //Create the world    world = new World(worldAABB, gravity, doSleep);      
    //Create 4 sides    final int kd=40;//Width or height    MyRectColor mrc=(kd/4, SCREEN_HEIGHT/2, kd/4, SCREEN_HEIGHT/2, true,world,0xFFe6e4FF); 
    (mrc); 
    mrc=(SCREEN_WIDTH-kd/4, SCREEN_HEIGHT/2, kd/4, SCREEN_HEIGHT/2, true,world,0xFFe6e4FF); 
    (mrc); 
    mrc=(SCREEN_WIDTH/2, kd/4, SCREEN_WIDTH/2, kd/4, true,world,0xFFe6e4FF); 
    (mrc); 
    mrc=(SCREEN_WIDTH/2, SCREEN_HEIGHT-kd/4, SCREEN_WIDTH/2, kd/4, true,world,0xFFe6e4FF); 
    (mrc); 
    //Create bricks    //Brick spacing: Line spacing is 20, module width is 10, up to 9 pieces per line    final int bs=20; 
    final int bw=(int)((SCREEN_WIDTH-2*kd-11*bs)/18); 
    //============================================================
    for(int i=2;i&lt;10;i++) 
    { 
      if((i%2)==0) 
      { 
        //The blue wood block on the left        for(int j=0;j&lt;9-i;j++) 
        { 
          mrc= 
          ( 
            kd/2+bs+bw/2+i*(kd+5)/2+j*(kd+5)+3, 
            SCREEN_HEIGHT+bw-i*(bw+kd)/2, 
            bw/2, 
            kd/2, 
            false, 
            world, 
            ((())) 
          ); 
          (mrc); 
        } 
        //Blue wood block on the right        for(int j=0;j&lt;9-i;j++) 
        { 
          mrc= 
          ( 
            3*kd/2+bs-bw/2+i*(kd+5)/2+j*(kd+5)-3, 
            SCREEN_HEIGHT+bw-i*(bw+kd)/2, 
            bw/2, 
            kd/2, 
            false, 
            world, 
            ((())) 
          ); 
          (mrc); 
        } 
      }   
      if((i%2)!=0) 
      { 
        for(int j=0;j&lt;10-i;j++) 
        { 
          mrc= 
          ( 
            kd/2+bs+kd/2+(i-1)*(kd+5)/2+j*(kd+5), 
            SCREEN_HEIGHT-(kd-bw)/2-(i-1)*(bw+kd)/2, 
            kd/2, 
            bw/2, 
            false, 
            world, 
            ((())) 
          ); 
          (mrc); 
        } 
      } 
    } 
    mrc= 
    ( 
      5*kd+bs+20, 
      SCREEN_HEIGHT-(kd+bw)*4-kd, 
      bw/2, 
      kd/2, 
      false, 
      world, 
      ((())) 
    ); 
    (mrc); 
    //Create the ball    MyCircleColor ball=(SCREEN_WIDTH/2-24, kd, kd/2, world,((()))); 
    (ball); 
    (new Vec2(0,50)); 
    GameView gv= new GameView(this);
    setContentView(gv);
  }
}

(8) Display interface class GameView

package ;
import ; 
import ; 
import ; 
import ; 
import ; 
public class GameView extends SurfaceView implements Callback{ 
  MyBox2dActivity activity; 
  Paint paint; 
  DrawThread dt; 
  public GameView(MyBox2dActivity activity) { 
    super(activity); 
    =activity; 
    ().addCallback(this);  
    paint =new Paint(); 
    (true); 
    dt=new DrawThread(this); 
    (); 
  } 
  public void onDraw(Canvas canvas) { 
    if(canvas==null) { 
      return ; 
    } 
    (255, 255, 255, 255); //Set background color white    for (MyBody mb : ) { 
      (canvas, paint); 
    } 
  } 
  @Override 
  public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
  } 
  @Override 
  public void surfaceCreated(SurfaceHolder holder) { 
        repaint(); 
  } 
  @Override 
  public void surfaceDestroyed(SurfaceHolder holder) { 
  } 
  public void repaint() { 
    SurfaceHolder holder=(); 
    Canvas canvas=(); 
    try { 
      synchronized(holder){ 
        onDraw(canvas); 
      } 
    } catch(Exception e){ 
      (); 
    } finally { 
      if(canvas!=null) { 
        (canvas);  
      } 
    } 
  } 
}

(9) DrawThread

package ; 
import static .*; 
//Draw threadpublic class DrawThread extends Thread 
{ 
  GameView gv; 
  public DrawThread(GameView gv) 
  { 
    =gv; 
  } 
  @Override 
  public void run() 
  { 
    while(DRAW_THREAD_FLAG) 
    { 
      (TIME_STEP, ITERA);//Start the simulation      (); 
      try  
      { 
        (20); 
      } catch (InterruptedException e)  
      { 
        (); 
      } 
    } 
  } 
}

I hope this article will be helpful to everyone's jsp programming.