Loading...
Logo
Processing Forum

Jbox2D server?

in Contributed Library Questions  •  11 months ago  
      So I have been working on getting a server - client jbox2D based sketch  going and I got it mostly working.  However, I have been having some strange bugs that I can't work out.  Right now, I have projectile syncing working but for some reason, gravity seems to stop working after a while.  Anyone know how to fix this?

Due to the amount of code and how I wrote it, I feel it would be unreasonable to put the entire thing in here so what I did was I uploaded my sketch to mediafire.  Here is the link:  http://www.mediafire.com/?6yy9votk4q7ivpd

However, I do have the main page of the server and the client here in-case there is any obvious bugs that are easy to spot:

server main tab:

Copy code
  1. import pbox2d.*;
  2. import org.jbox2d.common.*;
  3. import org.jbox2d.dynamics.joints.*;
  4. import org.jbox2d.collision.shapes.*;
  5. import org.jbox2d.collision.shapes.Shape;
  6. import org.jbox2d.common.*;
  7. import org.jbox2d.dynamics.*;
  8. import org.jbox2d.dynamics.contacts.*;
  9. import processing.net.*;

  10. PBox2D box2d;

  11. Tank tank;

  12. Server s;
  13. Client c;
  14. String input;
  15. int data[];

  16. //ArrayList boundaries;

  17. Surface surface;

  18. ArrayList<Particle> particles;

  19. void setup() {
  20.   size(1000,500);
  21.   smooth();
  22.   box2d = new PBox2D(this);
  23.   box2d.createWorld();
  24.   tank = new Tank(200,200);
  25.   /*boundaries = new ArrayList();
  26.   boundaries.add(new Boundary(width/2,height-5,width,10,0));
  27.   boundaries.add(new Boundary(width/2,5,width,10,0));
  28.   boundaries.add(new Boundary(width-5,height/2,10,height,0));
  29.   boundaries.add(new Boundary(5,height/2,10,height,0));*/
  30.   particles = new ArrayList<Particle>();
  31.   surface = new Surface();
  32.   s = new Server(this, 7787);
  33.   
  34. }

  35. void mousePressed(){
  36.   Vec2 temp_pos = tank.getCenter();
  37.   float temp_deg = getAngle(int(temp_pos.x),int(temp_pos.y)-15,mouseX,mouseY,90);
  38.   particles.add(new Particle(temp_pos.x,temp_pos.y-15,cos(radians(temp_deg))*30,sin(radians(temp_deg))*30,3));
  39. }
  40. void keys(){
  41.   if(keyPressed){
  42.     if(key == 'a' || key == 'A'){
  43.       tank.setSpeed(PI*3);
  44.     }else if(key == 'd' || key == 'D'){
  45.       tank.setSpeed(-PI*3);
  46.     }else{
  47.       tank.setSpeed(0);
  48.     }
  49.   }else{
  50.     tank.setSpeed(0);
  51.   }
  52. }
  53. void draw() {
  54.   boolean repeat = true;
  55.   while(repeat){
  56.     c = s.available();
  57.     if(c != null){
  58.       input = c.readString();
  59.       //input = input.substring(0, input.indexOf("\n")); // Only up to the newline
  60.       data = int(split(input, ' ')); // Split values into an array
  61.       if(data[0] == 0){
  62.         String senddata = "0";
  63.         ArrayList<Vec2> temp_senddata = surface.getPosData();
  64.         for(int i = 0; i < temp_senddata.size(); i++){
  65.           senddata = senddata + " " + temp_senddata.get(i).x + " " + temp_senddata.get(i).y;
  66.         }
  67.         s.write(senddata);
  68.       }else if(data[0] == 1){
  69.         println("server recived data request");
  70.         String senddata = "1";
  71.         for (int i = particles.size()-1; i >= 0; i--) {
  72.           Particle p = particles.get(i);
  73.           Vec2 temp_pos = p.getPostion();
  74.           Vec2 temp_velocity = p.getLinearVelocity();
  75.           senddata = senddata + " " + temp_pos.x + " " + temp_pos.y + " " + temp_velocity.x + " " + temp_velocity.y + " " + p.getAngularVelocity() + " " + p.getLinearDamping();
  76.         }
  77.         s.write(senddata);
  78.       }
  79.     }else{
  80.       repeat = false;
  81.     }
  82.   }
  83.   keys();
  84.   background(255);
  85.   box2d.step();
  86.   tank.display();
  87.   /*
  88.   for (int i = 0; i < boundaries.size(); i++) {
  89.     Boundary wall = (Boundary) boundaries.get(i);
  90.     wall.display();
  91.   }*/
  92.   surface.display();
  93.   fill(0);
  94.   for (int i = particles.size()-1; i >= 0; i--) {
  95.     Particle p = particles.get(i);
  96.     p.display();
  97.     // Particles that leave the screen, we delete them
  98.     // (note they have to be deleted from both the box2d world and our list
  99.     if (p.done()) {
  100.       particles.remove(i);
  101.     }
  102.   }
  103.   fill(0);
  104.   text("Use the mouse to aim and fire\nPress space to toggle motor",12,22);
  105. }
  106. float getDistance(int x1,int y1,int x2,int y2){
  107.   return sqrt(sq(abs(x1-x2))+sq(abs(y1-y2)));
  108. }
  109. float getAngle(int x1,int y1,int x2,int y2,int offset){
  110.   float output;
  111.   if(x2>x1){
  112.     output=map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180);
  113.   }else{
  114.     output=(180-map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180))+180;
  115.   }
  116.   output=output-offset;
  117.   output=mapToDeg(output);
  118.   return output;
  119. }
  120. float mapToDeg(float input){
  121.   while(input<0){
  122.     if(input<0){
  123.       input=input+360;
  124.     }
  125.   }
  126.   while(input>=360){
  127.     if(input>=360){
  128.       input=input-360;
  129.     }
  130.   }
  131.   return input;
  132. }

client main tab:

Copy code
  1. import pbox2d.*;
  2. import org.jbox2d.common.*;
  3. import org.jbox2d.dynamics.joints.*;
  4. import org.jbox2d.collision.shapes.*;
  5. import org.jbox2d.collision.shapes.Shape;
  6. import org.jbox2d.common.*;
  7. import org.jbox2d.dynamics.*;
  8. import org.jbox2d.dynamics.contacts.*;
  9. import processing.net.*;

  10. PBox2D box2d;

  11. Tank tank;

  12. Client c;

  13. String input;
  14. float data[];
  15. int reconnection;
  16. boolean wait;
  17. int waitcount;
  18. int nettimer;

  19. NumberBox ipa = new NumberBox(3);
  20. NumberBox ipb = new NumberBox(3);
  21. NumberBox ipc = new NumberBox(3);
  22. NumberBox ipd = new NumberBox(3);

  23. //ArrayList boundaries;

  24. Surface surface;

  25. ArrayList<Particle> particles;

  26. int menumode = 0;

  27. void setup() {
  28.   size(1000,500);
  29.   smooth();
  30.   box2d = new PBox2D(this);
  31.   box2d.createWorld();
  32.   tank = new Tank(200,200);
  33.   /*boundaries = new ArrayList();
  34.   boundaries.add(new Boundary(width/2,height-5,width,10,0));
  35.   boundaries.add(new Boundary(width/2,5,width,10,0));
  36.   boundaries.add(new Boundary(width-5,height/2,10,height,0));
  37.   boundaries.add(new Boundary(5,height/2,10,height,0));*/
  38.   particles = new ArrayList<Particle>();
  39. }

  40. void draw(){
  41.   switch(menumode){
  42.     case 0:
  43.       renderMenu();
  44.       break;
  45.     case 1:
  46.       tryconnection();
  47.       break;
  48.     case 2:
  49.       renderGame();
  50.       break;
  51.   }
  52. }
  53. void keyPressed(){
  54.   if(menumode == 2){
  55.     ipa.keyin();
  56.     ipb.keyin();
  57.     ipc.keyin();
  58.     ipd.keyin();
  59.   }
  60. }
  61. void tryconnection(){
  62.   if (c.available() > 0) {
  63.     input = c.readString();
  64.     //input = input.substring(1, input.indexOf("\n")); // Only up to the newline
  65.     data = float(split(input, ' ')); // Split values into an array
  66.     println(data);
  67.     if(data[0] == 0 && data[data.length-1] == 0 && data.length > 300){
  68.       ArrayList<Vec2> surfacein = new ArrayList<Vec2>();
  69.       for(int i = 1; i < data.length-2; i = i + 2){
  70.         surfacein.add(new Vec2(data[i],data[i+1]));
  71.       }
  72.       surfacein.add(new Vec2(0,0));
  73.       surface = new Surface(surfacein);
  74.       menumode = 2;
  75.     }else{
  76.       wait = true;
  77.     }
  78.   }
  79.   if(wait){
  80.     waitcount = waitcount + 1;
  81.   }
  82.   if(waitcount >= 40){
  83.     waitcount = 0;
  84.     wait = false;
  85.     c.write("0");
  86.   }
  87. }
  88. void renderMenu(){
  89.   background(255);
  90.   ipa.render(width/2-55,height/2-7,25,15);
  91.   ipb.render(width/2-27,height/2-7,25,15);
  92.   ipc.render(width/2+2,height/2-7,25,15);
  93.   ipd.render(width/2+30,height/2-7,25,15);
  94.   text("Enter server Ip:",width/2-40,height/2-10);
  95.   if(button(width/2-23,height/2+12,46,12,"connect")){
  96.     menumode = 1;
  97.     c = new Client(this, ipa.getData() + "." + ipb.getData() + "." + ipc.getData() + "." + ipd.getData(), 7787);
  98.     c.write("0\n");
  99.   }
  100.   if(button(width/2-27,height/2+26,54,12,"localhost")){
  101.     menumode = 1;
  102.     c = new Client(this,"LOCALHOST", 7787);
  103.     c.write("0");
  104.   }
  105. }

  106. void mousePressed(){
  107.   if(menumode == 2){
  108.     Vec2 temp_pos = tank.getCenter();
  109.     float temp_deg = getAngle(int(temp_pos.x),int(temp_pos.y)-15,mouseX,mouseY,90);
  110.     //particles.add(new Particle(temp_pos.x,temp_pos.y-15,cos(radians(temp_deg))*30,sin(radians(temp_deg))*30,3));
  111.   }
  112. }
  113. void keys(){
  114.   if(keyPressed){
  115.     if(key == 'a' || key == 'A'){
  116.       tank.setSpeed(PI*3);
  117.     }else if(key == 'd' || key == 'D'){
  118.       tank.setSpeed(-PI*3);
  119.     }else{
  120.       tank.setSpeed(0);
  121.     }
  122.   }else{
  123.     tank.setSpeed(0);
  124.   }
  125. }
  126. void renderGame() {
  127.   if (c.available() > 0) {
  128.     input = c.readString();
  129.     //input = input.substring(1, input.indexOf("\n")); // Only up to the newline
  130.     data = float(split(input, ' ')); // Split values into an array
  131.     println("data revived");
  132.     if(data[0] == 0 && data[data.length-1] == 0 && data.length > 300){
  133.       ArrayList<Vec2> surfacein = new ArrayList<Vec2>();
  134.       for(int i = 1; i < data.length-2; i = i + 2){
  135.         surfacein.add(new Vec2(data[i],data[i+1]));
  136.       }
  137.       surfacein.add(new Vec2(0,0));
  138.       surface = new Surface(surfacein);
  139.     }else if(data[0] == 1 && data.length > 1){
  140.       println("client recived update ");
  141.       //newparticles.clear();
  142.       particles.clear(); 
  143.       for (int i = 1; i <= data.length-6; i = i + 6){     
  144.         particles.add(new Particle(data[i],data[i+1],data[i+2],data[i+3],3,data[i+4],data[i+5]));
  145.       }
  146.     }
  147.   }
  148.   keys();
  149.   background(255);
  150.   box2d.step();
  151.   tank.display();
  152.   if(nettimer == 20){
  153.     c.write("1 ");
  154.   }else if(nettimer >= 40){
  155.     nettimer = 0;
  156.   }
  157.   nettimer = nettimer + 1;
  158.   /*
  159.   for (int i = 0; i < boundaries.size(); i++) {
  160.     Boundary wall = (Boundary) boundaries.get(i);
  161.     wall.display();
  162.   }*/
  163.   if(surface != null){
  164.     surface.display();
  165.   }
  166.   fill(0);
  167.   //if(particles != null){
  168.     for (int i = particles.size()-1; i >= 0; i--) {
  169.       Particle p = particles.get(i);
  170.       p.display();
  171.       // Particles that leave the screen, we delete them
  172.       // (note they have to be deleted from both the box2d world and our list
  173.       if (p.done()) {
  174.         particles.remove(i);
  175.       }
  176.     }
  177.   //}
  178.   fill(0);
  179.   text("Use the mouse to aim and fire\nPress A and D to move",12,22);
  180. }
  181. float getDistance(int x1,int y1,int x2,int y2){
  182.   return sqrt(sq(abs(x1-x2))+sq(abs(y1-y2)));
  183. }
  184. float getAngle(int x1,int y1,int x2,int y2,int offset){
  185.   float output;
  186.   if(x2>x1){
  187.     output=map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180);
  188.   }else{
  189.     output=(180-map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180))+180;
  190.   }
  191.   output=output-offset;
  192.   output=mapToDeg(output);
  193.   return output;
  194. }
  195. float mapToDeg(float input){
  196.   while(input<0){
  197.     if(input<0){
  198.       input=input+360;
  199.     }
  200.   }
  201.   while(input>=360){
  202.     if(input>=360){
  203.       input=input-360;
  204.     }
  205.   }
  206.   return input;
  207. }
  208. boolean button(int x, int y, int w, int h, String textin){
  209.   stroke(0);
  210.   if(mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h){
  211.     if(mousePressed){
  212.       return true;
  213.     }
  214.     fill(255,255,210);
  215.   }else{
  216.     fill(255);
  217.   }
  218.   strokeWeight(1);
  219.   rect(x,y,w,h);
  220.   fill(0);
  221.   text(textin,x+1,y+h-1);
  222.   return false;
  223. }