简体   繁体   中英

Box2d processing

I'm new to processing and I'm getting an error while playing with Daniel Shiffman Box2D library. I'm trying to create a chain shape with vectors that I've defined in the main program. Since i need to create multiple surfaces , i made a Vec2 [x][y] x for the objects and y for the Vec2 points to create the chain but I keep coming with the "Type mismatch "main.Surface" does not match with "processing.core.PSurface"

Here is my entire code:

class Surface {
      ArrayList<Vec2> surface;
      int id;


      Surface(int id_, int largo, Vec2 [] r) {
        id=id_;

        surface = new ArrayList<Vec2>(largo);
        for(int i = 0 ; i < largo ; i++){
          surface.add(new Vec2(r[i]));
        }

        ChainShape chain = new ChainShape();

        Vec2[] vertices = new Vec2[surface.size()];
        for (int i = 0; i < vertices.length; i++) {
          vertices[i] = box2d.coordPixelsToWorld(surface.get(i));
        }

        chain.createChain(vertices, vertices.length);

        BodyDef bd = new BodyDef();
        Body body = box2d.world.createBody(bd);
        body.createFixture(chain, 1);
      }

    }

    import shiffman.box2d.*;
    import org.jbox2d.collision.shapes.*;
    import org.jbox2d.common.*;
    import org.jbox2d.dynamics.*;
    PImage background;

    Box2DProcessing box2d;

    Vec2[][] base;


    void setup() {
      fullScreen();
      //size(1600,900);
      background = loadImage("background.png");

      box2d = new Box2DProcessing(this);
      box2d.createWorld();

      ///////////////////////////////////////
          //Platforms positions vectors
      ///////////////////////////////////////

      base[0][0] = new Vec2(0 , 890.15);
      base[0][1] = new Vec2(85.933 , 890.15);
      base[0][2] = new Vec2(102.54 , 879.727);
      base[0][3] = new Vec2(126.003 , 879.727);
      base[0][4] = new Vec2(132.67 , 872.661);
      base[0][5] = new Vec2(189.729 , 872.661);
      base[0][6] = new Vec2(196.395 , 880.048);
      base[0][7] = new Vec2(228.898 , 880.048);
      base[0][8] = new Vec2(254.74 , 863.668);
      base[0][9] = new Vec2(283.438 , 863.668);
      base[0][10] = new Vec2(286.771 , 867.001);

      base[1][0] = new Vec2 (319.063 , 850.122);
      base[1][1] = new Vec2 (375.73 , 850.122);

      base[2][0] = new Vec2 (409.408 , 818.401);
      base[2][1] = new Vec2 (416.298 , 811.511);
      base[2][2] = new Vec2 (468.999 , 811.511);
      base[2][3] = new Vec2 (493.691 , 833.396);
      base[2][4] = new Vec2 (540.313 , 833.396);

      base[3][0] = new Vec2 (576.493 , 788.066);
      base[3][1] = new Vec2 (646.003 , 788.066);

      base[4][0] = new Vec2 (685.219 , 746.4);
      base[4][1] = new Vec2 (746.885 , 746.4);

      base[5][0] = new Vec2 (528.993 , 709.684);
      base[5][1] = new Vec2 (652.474 , 709.684);

      base[6][0] = new Vec2 (600.219 , 657.233);
      base[6][1] = new Vec2 (663.993 , 657.233);
      base[6][2] = new Vec2 (688.16 , 628.066);
      base[6][3] = new Vec2 (723.16 , 628.066);

      base[7][0] = new Vec2 (767.327 , 664.733);
      base[7][1] = new Vec2 (894.827 , 664.733);

      base[8][0] = new Vec2 (886.836 , 616.547);
      base[8][1] = new Vec2 (942.807 , 616.547);

      base[9][0] = new Vec2 (923.993 , 581.4);
      base[9][1] = new Vec2 (969.827 , 581.4);

      base[10][0] = new Vec2 (716.052 , 531.4);
      base[10][1] = new Vec2 (769.385 , 531.4);
      base[10][2] = new Vec2 (795.219 , 555.566);
      base[10][3] = new Vec2 (866.052 , 555.566);

      base[11][0] = new Vec2 (525.104 , 500.566);
      base[11][1] = new Vec2 (666.215 , 500.566);

      base[12][0] = new Vec2 (347.882 , 487.233);
      base[12][1] = new Vec2 (395.104 , 487.233);
      base[12][2] = new Vec2 (422.882 , 507.233);
      base[12][3] = new Vec2 (471.771 , 507.233);

      base[13][0] = new Vec2 (235.104 , 510.566);
      base[13][1] = new Vec2 (306.215 , 510.566);

      base[14][0] = new Vec2 (75.66 , 472.789);
      base[14][1] = new Vec2 (198.993 , 472.789);

      base[15][0] = new Vec2 (115.66 , 436.122);
      base[15][1] = new Vec2 (181.771 , 436.122);
      base[15][2] = new Vec2 (209.271 , 408.622);
      base[15][3] = new Vec2 (243.993 , 408.622);
      base[15][4] = new Vec2 (273.021 , 379.594);
      base[15][5] = new Vec2 (316.215 , 379.594);
      base[15][6] = new Vec2 (333.021 , 362.789);
      base[15][7] = new Vec2 (377.327 , 362.789);

      base[16][0] = new Vec2 (98.944 , 307.233);
      base[16][1] = new Vec2 (153.993 , 307.233);
      base[16][2] = new Vec2 (181.215 , 325.37);
      base[16][3] = new Vec2 (247.882 , 325.37);
      base[16][4] = new Vec2 (286.771 , 337.789);
      base[16][5] = new Vec2 (320.66 , 337.789);

      base[17][0] = new Vec2 (33.16 , 236.678);
      base[17][1] = new Vec2 (60.938 , 236.678);
      base[17][2] = new Vec2 (78.16 , 258.066);
      base[17][3] = new Vec2 (102.049 , 258.066);

      base[18][0] = new Vec2 (67.604 , 208.066);
      base[18][1] = new Vec2 (98.944 , 208.066);
      base[18][2] = new Vec2 (113.715 , 193.017);
      base[18][3] = new Vec2 (136.215 , 193.017);

      base[19][0] = new Vec2 (175.104 , 193.017);
      base[19][1] = new Vec2 (205.66 , 193.017);
      base[19][2] = new Vec2 (222.604 , 177.789);
      base[19][3] = new Vec2 (247.604 , 177.789);

      base[20][0] = new Vec2 (320.66 , 177.789);
      base[20][1] = new Vec2 (568.993 , 177.789);

      base[21][0] = new Vec2 (606.787 , 220.959);
      base[21][1] = new Vec2 (685.219 , 220.959);

      base[22][0] = new Vec2 (767.327 , 337.789);
      base[22][1] = new Vec2 (875.415 , 337.789);

      base[23][0] = new Vec2 (900.66 , 301.4);
      base[23][1] = new Vec2 (1049.827  , 301.4);
      base[23][2] = new Vec2 (1084.688 , 336.261);
      base[23][3] = new Vec2 (1118.16 , 336.261);

      base[24][0] = new Vec2 (1158.16 , 458.066);
      base[24][1] = new Vec2 (1290.66 , 458.066);

      base[25][0] = new Vec2 (1374.827 , 573.066);
      base[25][1] = new Vec2 (1599.827 , 573.066);
      //////////////////////////////////////////

      for(int i = 0 ; i < base.length ; i++){
        surface = new Surface (i, base[i].length , base[i]) ;
      }

    }

    void draw() {
      box2d.step();
      imageMode(CENTER);
      image(background,width/2,height/2,1600,900);
    }

    ///////////////end/////////////

So, the problem that i'm getting is between line 6 and 169. The problem is when I try to send to the Surface constructor (which is supposed to receive a Vec2 [] r) the base[i] from void setup(){}

I would really appreciate any help!!! The error I believe that comes from the constructor in the class Surface and the list type that I'm trying to push to the constructor when i create a new surface. This is for a homework at college and I'm a bit against the clock, so any help would be REALLY useful!!!

Thank you very much!!

Processing 3 added a surface variable of type PSurface .

You shouldn't have your own variable named surface because it conflicts with the existing surface variable.

To fix the first error, just change the name of your surface variables to something else.

After you do that, you have another problem: you never initialize a surface variable at the sketch level. But that will become more obvious when you change the variable name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM