Android Canvas - 2d physics for keeping Player on ground when falling -


im trying create 2d plat former game, have mapped out game ground , there smaller grounds in air player can jump on selected grounds. have managed set y axis when player hits ground player stays on ground landscape game, player keeps lagging.

i have built map this:

public ground[][] ground = new ground[mapheight][mapwidth];        ground[0][0] = new ground(ground.texturetype.unspecified, this);     ground[0][1] = new ground(ground.texturetype.unspecified, this);     ground[0][2] = new ground(ground.texturetype.unspecified, this);     ground[0][3] = new ground(ground.texturetype.unspecified, this);     ground[0][4] = new ground(ground.texturetype.unspecified, this);     ground[0][5] = new ground(ground.texturetype.unspecified, this);     ground[0][6] = new ground(ground.texturetype.unspecified, this);     ground[0][7] = new ground(ground.texturetype.unspecified, this);     ground[0][8] = new ground(ground.texturetype.unspecified, this);     ground[0][9] = new ground(ground.texturetype.unspecified, this);     ground[0][10] = new ground(ground.texturetype.unspecified, this);     ground[0][11] = new ground(ground.texturetype.unspecified, this);     ground[0][12] = new ground(ground.texturetype.unspecified, this);     ground[0][13] = new ground(ground.texturetype.unspecified, this);     ground[0][14] = new ground(ground.texturetype.unspecified, this);      ground[1][0] = new ground(ground.texturetype.tile2, this);     ground[1][1] = new ground(ground.texturetype.tile2, this);     ground[1][2] = new ground(ground.texturetype.tile2, this);     ground[1][3] = new ground(ground.texturetype.tile2, this);     ground[1][4] = new ground(ground.texturetype.unspecified, this);     ground[1][5] = new ground(ground.texturetype.unspecified, this);     ground[1][6] = new ground(ground.texturetype.tile2, this);     ground[1][7] = new ground(ground.texturetype.tile2, this);     ground[1][8] = new ground(ground.texturetype.unspecified, this);     ground[1][9] = new ground(ground.texturetype.unspecified, this);     ground[1][10] = new ground(ground.texturetype.unspecified, this);     ground[1][11] = new ground(ground.texturetype.tile2, this);     ground[1][12] = new ground(ground.texturetype.tile2, this);     ground[1][13] = new ground(ground.texturetype.unspecified, this);     ground[1][14] = new ground(ground.texturetype.tile2, this);      ground[2][0] = new ground(ground.texturetype.unspecified, this);     ground[2][1] = new ground(ground.texturetype.unspecified, this);     ground[2][2] = new ground(ground.texturetype.unspecified, this);     ground[2][3] = new ground(ground.texturetype.unspecified, this);     ground[2][4] = new ground(ground.texturetype.unspecified, this);     ground[2][5] = new ground(ground.texturetype.unspecified, this);     ground[2][6] = new ground(ground.texturetype.unspecified, this);     ground[2][7] = new ground(ground.texturetype.unspecified, this);     ground[2][8] = new ground(ground.texturetype.unspecified, this);     ground[2][9] = new ground(ground.texturetype.unspecified, this);     ground[2][10] = new ground(ground.texturetype.unspecified, this);     ground[2][11] = new ground(ground.texturetype.unspecified, this);     ground[2][12] = new ground(ground.texturetype.unspecified, this);     ground[2][13] = new ground(ground.texturetype.unspecified, this);     ground[2][14] = new ground(ground.texturetype.unspecified, this);      ground[3][0] = new ground(ground.texturetype.unspecified, this);     ground[3][1] = new ground(ground.texturetype.unspecified, this);     ground[3][2] = new ground(ground.texturetype.unspecified, this);     ground[3][3] = new ground(ground.texturetype.unspecified, this);     ground[3][4] = new ground(ground.texturetype.unspecified, this);     ground[3][5] = new ground(ground.texturetype.unspecified, this);     ground[3][6] = new ground(ground.texturetype.unspecified, this);     ground[3][7] = new ground(ground.texturetype.unspecified, this);     ground[3][8] = new ground(ground.texturetype.unspecified, this);     ground[3][9] = new ground(ground.texturetype.unspecified, this);     ground[3][10] = new ground(ground.texturetype.unspecified, this);     ground[3][11] = new ground(ground.texturetype.unspecified, this);     ground[3][12] = new ground(ground.texturetype.unspecified, this);     ground[3][13] = new ground(ground.texturetype.unspecified, this);     ground[3][14] = new ground(ground.texturetype.unspecified, this);      ground[4][0] = new ground(ground.texturetype.unspecified, this);     ground[4][1] = new ground(ground.texturetype.tile2, this);     ground[4][2] = new ground(ground.texturetype.tile2, this);     ground[4][3] = new ground(ground.texturetype.tile2, this);     ground[4][4] = new ground(ground.texturetype.unspecified, this);     ground[4][5] = new ground(ground.texturetype.unspecified, this);     ground[4][6] = new ground(ground.texturetype.unspecified, this);     ground[4][7] = new ground(ground.texturetype.tile2, this);     ground[4][8] = new ground(ground.texturetype.unspecified, this);     ground[4][9] = new ground(ground.texturetype.unspecified, this);     ground[4][10] = new ground(ground.texturetype.unspecified, this);     ground[4][11] = new ground(ground.texturetype.tile2, this);     ground[4][12] = new ground(ground.texturetype.tile2, this);     ground[4][13] = new ground(ground.texturetype.unspecified, this);     ground[4][14] = new ground(ground.texturetype.unspecified, this);      ground[5][0] = new ground(ground.texturetype.unspecified, this);     ground[5][1] = new ground(ground.texturetype.unspecified, this);     ground[5][2] = new ground(ground.texturetype.unspecified, this);     ground[5][3] = new ground(ground.texturetype.unspecified, this);     ground[5][4] = new ground(ground.texturetype.unspecified, this);     ground[5][5] = new ground(ground.texturetype.unspecified, this);     ground[5][6] = new ground(ground.texturetype.unspecified, this);     ground[5][7] = new ground(ground.texturetype.unspecified, this);     ground[5][8] = new ground(ground.texturetype.unspecified, this);     ground[5][9] = new ground(ground.texturetype.unspecified, this);     ground[5][10] = new ground(ground.texturetype.unspecified, this);     ground[5][11] = new ground(ground.texturetype.unspecified, this);     ground[5][12] = new ground(ground.texturetype.unspecified, this);     ground[5][13] = new ground(ground.texturetype.unspecified, this);     ground[5][14] = new ground(ground.texturetype.unspecified, this);      ground[6][0] = new ground(ground.texturetype.unspecified, this);     ground[6][1] = new ground(ground.texturetype.unspecified, this);     ground[6][2] = new ground(ground.texturetype.unspecified, this);     ground[6][3] = new ground(ground.texturetype.unspecified, this);     ground[6][4] = new ground(ground.texturetype.unspecified, this);     ground[6][5] = new ground(ground.texturetype.unspecified, this);     ground[6][6] = new ground(ground.texturetype.unspecified, this);     ground[6][7] = new ground(ground.texturetype.unspecified, this);     ground[6][8] = new ground(ground.texturetype.unspecified, this);     ground[6][9] = new ground(ground.texturetype.unspecified, this);     ground[6][10] = new ground(ground.texturetype.unspecified, this);     ground[6][11] = new ground(ground.texturetype.unspecified, this);     ground[6][12] = new ground(ground.texturetype.unspecified, this);     ground[6][13] = new ground(ground.texturetype.unspecified, this);     ground[6][14] = new ground(ground.texturetype.unspecified, this);      ground[7][0] = new ground(ground.texturetype.unspecified, this);     ground[7][1] = new ground(ground.texturetype.tile2, this);     ground[7][2] = new ground(ground.texturetype.tile2, this);     ground[7][3] = new ground(ground.texturetype.tile2, this);     ground[7][4] = new ground(ground.texturetype.unspecified, this);     ground[7][5] = new ground(ground.texturetype.unspecified, this);     ground[7][6] = new ground(ground.texturetype.tile2, this);     ground[7][7] = new ground(ground.texturetype.tile2, this);     ground[7][8] = new ground(ground.texturetype.unspecified, this);     ground[7][9] = new ground(ground.texturetype.unspecified, this);     ground[7][10] = new ground(ground.texturetype.unspecified, this);     ground[7][11] = new ground(ground.texturetype.tile2, this);     ground[7][12] = new ground(ground.texturetype.tile2, this);     ground[7][13] = new ground(ground.texturetype.tile2, this);     ground[7][14] = new ground(ground.texturetype.tile2, this);      ground[8][0] = new ground(ground.texturetype.unspecified, this);     ground[8][1] = new ground(ground.texturetype.unspecified, this);     ground[8][2] = new ground(ground.texturetype.unspecified, this);     ground[8][3] = new ground(ground.texturetype.unspecified, this);     ground[8][4] = new ground(ground.texturetype.unspecified, this);     ground[8][5] = new ground(ground.texturetype.unspecified, this);     ground[8][6] = new ground(ground.texturetype.unspecified, this);     ground[8][7] = new ground(ground.texturetype.unspecified, this);     ground[8][8] = new ground(ground.texturetype.unspecified, this);     ground[8][9] = new ground(ground.texturetype.unspecified, this);     ground[8][10] = new ground(ground.texturetype.unspecified, this);     ground[8][11] = new ground(ground.texturetype.unspecified, this);     ground[8][12] = new ground(ground.texturetype.unspecified, this);     ground[8][13] = new ground(ground.texturetype.unspecified, this);     ground[8][14] = new ground(ground.texturetype.unspecified, this);      ground[9][0] = new ground(ground.texturetype.unspecified, this);     ground[9][1] = new ground(ground.texturetype.unspecified, this);     ground[9][2] = new ground(ground.texturetype.unspecified, this);     ground[9][3] = new ground(ground.texturetype.unspecified, this);     ground[9][4] = new ground(ground.texturetype.unspecified, this);     ground[9][5] = new ground(ground.texturetype.unspecified, this);     ground[9][6] = new ground(ground.texturetype.unspecified, this);     ground[9][7] = new ground(ground.texturetype.unspecified, this);     ground[9][8] = new ground(ground.texturetype.unspecified, this);     ground[9][9] = new ground(ground.texturetype.unspecified, this);     ground[9][10] = new ground(ground.texturetype.unspecified, this);     ground[9][11] = new ground(ground.texturetype.unspecified, this);     ground[9][12] = new ground(ground.texturetype.unspecified, this);     ground[9][13] = new ground(ground.texturetype.unspecified, this);     ground[9][14] = new ground(ground.texturetype.unspecified, this);       ground[10][0] = new ground(ground.texturetype.tile34, this);     ground[10][1] = new ground(ground.texturetype.tile34, this);     ground[10][2] = new ground(ground.texturetype.tile34, this);     ground[10][3] = new ground(ground.texturetype.tile34, this);     ground[10][4] = new ground(ground.texturetype.tile34, this);     ground[10][5] = new ground(ground.texturetype.tile34, this);     ground[10][6] = new ground(ground.texturetype.tile34, this);     ground[10][7] = new ground(ground.texturetype.tile2, this);     ground[10][8] = new ground(ground.texturetype.tile34, this);     ground[10][9] = new ground(ground.texturetype.tile34, this);     ground[10][10] = new ground(ground.texturetype.tile34, this);     ground[10][11] = new ground(ground.texturetype.tile34, this);     ground[10][12] = new ground(ground.texturetype.tile34, this);     ground[10][13] = new ground(ground.texturetype.tile34, this);     ground[10][14] = new ground(ground.texturetype.tile34, this);         for(int = 0; < mapheight; i++){         for(int j = 0; j < mapwidth; j++){              if(ground[i][j] != null){                  ground[i][j].sety(topstart + (i*128));                 ground[i][j].setx(j*128);             }            }     } 

using matrix array.

here ground object

public class ground {               public enum texturetype {                 unspecified,                 tile2,                 tile34             }              public texturetype type;             private gamepanel context;             private int x, y, dy = 10;             private bitmap image;               public ground(texturetype type, gamepanel context){                 this.type = type;                 this.context = context;                 if(type == texturetype.tile2){                     image = bitmapfactory.decoderesource(((gamepanel)context).getresources(), r.drawable.tile2);                 }else if(type == texturetype.tile34){                     image = bitmapfactory.decoderesource(((gamepanel)context).getresources(), r.drawable.tile34);                 }else{                     image = null;                 }               }             public void update() {                     rect mysprite = getbounds();                 rect mycollisionobject = context.robber.getbounds();                 if(mysprite.intersect(mycollisionobject)) {                      if(type != ground.texturetype.unspecified) {                          context.robber.sety(gety() - 174);                         context.robber.isonground = true;                     }else{                      }                 }                   if(getx() == context.robber.getx() && type == texturetype.unspecified){                     context.robber.isonground = false;                 }                  /**                 }**/                  }                  public void draw(canvas canvas)             {                  if(type != texturetype.unspecified){                     canvas.drawbitmap(image,getx(),gety(),null);                 }              }              public void startingpoint(int x, int y){                  this.x =  x;                 this.y = y;              }             public void setx(float x){                  this.x = (int)x;              }             public void sety(float y){                  this.y = (int)y;              }             public int getwidth(){                  return image.getwidth();              }             public int getheight(){                  return image.getheight();              }             public int getx(){                  return x;              }             public int gety(){                  return y;              }              public rect getbounds(){                  if(type == texturetype.unspecified){                     return  new rect(x, y, x+0, y+0);                 }else{                     return  new rect(x, y, x+image.getwidth(), y+image.getheight());                 }             }              } 

here player object:

public class player {              private bitmap[] image;             private double x, y, dy = 0, dx = 4;             private bitmap spritesheet;             private double dya;             private boolean playing;             private animation animation = new animation();             private gamepanel gamepanel;             public boolean isonground = false;              private static final double gravity = 9.8;             private static final double terminal_velocity = 100;             private double speed;             private int ygroundtmp = -46 + 174;              public player(bitmap[] res, final gamepanel gamepanel)             {                 this.gamepanel = gamepanel;                      animation.setframes(res);                 animation.setdelay(100);                 //starttime = system.nanotime();              }             double velocityx = 0.0;             double velocityy = 0.0;             double gravity = 0.5;              public void update()             {                  x += 1;                 if(isonground){                  }else{                     velocityy += gravity;                     y += velocityy;                 }                   /**                 rect mysprite = getbounds();                   for(int = 0; < 11; i++){                     for(int j = 0; j < 15;j++){                         if(gamepanel.ground[i][j] != null){                             rect mycollisionobject = gamepanel.ground[i][j].getbounds();                             if(mysprite.intersect(mycollisionobject)) {                                  if(gamepanel.ground[i][j].type != ground.texturetype.unspecified) {                                      gamepanel.robber.sety(gamepanel.ground[i][j].gety() - 174);                                     isonground = true;                                  }                             }                         }                     }                 }                 **/                   animation.update();              }             public void draw(canvas canvas)             {                 canvas.drawbitmap(animation.getimage(),(int) getx(),(int)gety(),null);               }             public void startingpoint(int x, int y){                    //system.out.println(rx);                 this.x = x;                 this.y = y;              }             public void setx(float x){                  this.x = (int)x;              }             public void sety(float y){                  this.y = (int)y;                 system.out.println(y);              }             public int getwidth(){                  return animation.getimage().getwidth();              }             public int getheight(){                  return animation.getimage().getheight();              }             public double getx(){                  return x;              }             public double gety(){                  return y;              }              public void movedown(int val){                 y += val;             }              public void moveup(int val){                 velocityy -= val;             }              public rect getbounds(){                 return new rect((int)x, (int)y, (int)(x+animation.getimage().getwidth()), (int)(y+animation.getimage().getheight()));             }          } 

could please advise.


Comments