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:
- import pbox2d.*;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.joints.*;
- import org.jbox2d.collision.shapes.*;
- import org.jbox2d.collision.shapes.Shape;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.*;
- import org.jbox2d.dynamics.contacts.*;
- import processing.net.*;
- PBox2D box2d;
- Tank tank;
- Server s;
- Client c;
- String input;
- int data[];
- //ArrayList boundaries;
- Surface surface;
- ArrayList<Particle> particles;
- void setup() {
- size(1000,500);
- smooth();
- box2d = new PBox2D(this);
- box2d.createWorld();
- tank = new Tank(200,200);
- /*boundaries = new ArrayList();
- boundaries.add(new Boundary(width/2,height-5,width,10,0));
- boundaries.add(new Boundary(width/2,5,width,10,0));
- boundaries.add(new Boundary(width-5,height/2,10,height,0));
- boundaries.add(new Boundary(5,height/2,10,height,0));*/
- particles = new ArrayList<Particle>();
- surface = new Surface();
- s = new Server(this, 7787);
- }
- void mousePressed(){
- Vec2 temp_pos = tank.getCenter();
- float temp_deg = getAngle(int(temp_pos.x),int(temp_pos.y)-15,mouseX,mouseY,90);
- particles.add(new Particle(temp_pos.x,temp_pos.y-15,cos(radians(temp_deg))*30,sin(radians(temp_deg))*30,3));
- }
- void keys(){
- if(keyPressed){
- if(key == 'a' || key == 'A'){
- tank.setSpeed(PI*3);
- }else if(key == 'd' || key == 'D'){
- tank.setSpeed(-PI*3);
- }else{
- tank.setSpeed(0);
- }
- }else{
- tank.setSpeed(0);
- }
- }
- void draw() {
- boolean repeat = true;
- while(repeat){
- c = s.available();
- if(c != null){
- input = c.readString();
- //input = input.substring(0, input.indexOf("\n")); // Only up to the newline
- data = int(split(input, ' ')); // Split values into an array
- if(data[0] == 0){
- String senddata = "0";
- ArrayList<Vec2> temp_senddata = surface.getPosData();
- for(int i = 0; i < temp_senddata.size(); i++){
- senddata = senddata + " " + temp_senddata.get(i).x + " " + temp_senddata.get(i).y;
- }
- s.write(senddata);
- }else if(data[0] == 1){
- println("server recived data request");
- String senddata = "1";
- for (int i = particles.size()-1; i >= 0; i--) {
- Particle p = particles.get(i);
- Vec2 temp_pos = p.getPostion();
- Vec2 temp_velocity = p.getLinearVelocity();
- senddata = senddata + " " + temp_pos.x + " " + temp_pos.y + " " + temp_velocity.x + " " + temp_velocity.y + " " + p.getAngularVelocity() + " " + p.getLinearDamping();
- }
- s.write(senddata);
- }
- }else{
- repeat = false;
- }
- }
- keys();
- background(255);
- box2d.step();
- tank.display();
- /*
- for (int i = 0; i < boundaries.size(); i++) {
- Boundary wall = (Boundary) boundaries.get(i);
- wall.display();
- }*/
- surface.display();
- fill(0);
- for (int i = particles.size()-1; i >= 0; i--) {
- Particle p = particles.get(i);
- p.display();
- // Particles that leave the screen, we delete them
- // (note they have to be deleted from both the box2d world and our list
- if (p.done()) {
- particles.remove(i);
- }
- }
- fill(0);
- text("Use the mouse to aim and fire\nPress space to toggle motor",12,22);
- }
- float getDistance(int x1,int y1,int x2,int y2){
- return sqrt(sq(abs(x1-x2))+sq(abs(y1-y2)));
- }
- float getAngle(int x1,int y1,int x2,int y2,int offset){
- float output;
- if(x2>x1){
- output=map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180);
- }else{
- output=(180-map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180))+180;
- }
- output=output-offset;
- output=mapToDeg(output);
- return output;
- }
- float mapToDeg(float input){
- while(input<0){
- if(input<0){
- input=input+360;
- }
- }
- while(input>=360){
- if(input>=360){
- input=input-360;
- }
- }
- return input;
- }
client main tab:
- import pbox2d.*;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.joints.*;
- import org.jbox2d.collision.shapes.*;
- import org.jbox2d.collision.shapes.Shape;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.*;
- import org.jbox2d.dynamics.contacts.*;
- import processing.net.*;
- PBox2D box2d;
- Tank tank;
- Client c;
- String input;
- float data[];
- int reconnection;
- boolean wait;
- int waitcount;
- int nettimer;
- NumberBox ipa = new NumberBox(3);
- NumberBox ipb = new NumberBox(3);
- NumberBox ipc = new NumberBox(3);
- NumberBox ipd = new NumberBox(3);
- //ArrayList boundaries;
- Surface surface;
- ArrayList<Particle> particles;
- int menumode = 0;
- void setup() {
- size(1000,500);
- smooth();
- box2d = new PBox2D(this);
- box2d.createWorld();
- tank = new Tank(200,200);
- /*boundaries = new ArrayList();
- boundaries.add(new Boundary(width/2,height-5,width,10,0));
- boundaries.add(new Boundary(width/2,5,width,10,0));
- boundaries.add(new Boundary(width-5,height/2,10,height,0));
- boundaries.add(new Boundary(5,height/2,10,height,0));*/
- particles = new ArrayList<Particle>();
- }
- void draw(){
- switch(menumode){
- case 0:
- renderMenu();
- break;
- case 1:
- tryconnection();
- break;
- case 2:
- renderGame();
- break;
- }
- }
- void keyPressed(){
- if(menumode == 2){
- ipa.keyin();
- ipb.keyin();
- ipc.keyin();
- ipd.keyin();
- }
- }
- void tryconnection(){
- if (c.available() > 0) {
- input = c.readString();
- //input = input.substring(1, input.indexOf("\n")); // Only up to the newline
- data = float(split(input, ' ')); // Split values into an array
- println(data);
- if(data[0] == 0 && data[data.length-1] == 0 && data.length > 300){
- ArrayList<Vec2> surfacein = new ArrayList<Vec2>();
- for(int i = 1; i < data.length-2; i = i + 2){
- surfacein.add(new Vec2(data[i],data[i+1]));
- }
- surfacein.add(new Vec2(0,0));
- surface = new Surface(surfacein);
- menumode = 2;
- }else{
- wait = true;
- }
- }
- if(wait){
- waitcount = waitcount + 1;
- }
- if(waitcount >= 40){
- waitcount = 0;
- wait = false;
- c.write("0");
- }
- }
- void renderMenu(){
- background(255);
- ipa.render(width/2-55,height/2-7,25,15);
- ipb.render(width/2-27,height/2-7,25,15);
- ipc.render(width/2+2,height/2-7,25,15);
- ipd.render(width/2+30,height/2-7,25,15);
- text("Enter server Ip:",width/2-40,height/2-10);
- if(button(width/2-23,height/2+12,46,12,"connect")){
- menumode = 1;
- c = new Client(this, ipa.getData() + "." + ipb.getData() + "." + ipc.getData() + "." + ipd.getData(), 7787);
- c.write("0\n");
- }
- if(button(width/2-27,height/2+26,54,12,"localhost")){
- menumode = 1;
- c = new Client(this,"LOCALHOST", 7787);
- c.write("0");
- }
- }
- void mousePressed(){
- if(menumode == 2){
- Vec2 temp_pos = tank.getCenter();
- float temp_deg = getAngle(int(temp_pos.x),int(temp_pos.y)-15,mouseX,mouseY,90);
- //particles.add(new Particle(temp_pos.x,temp_pos.y-15,cos(radians(temp_deg))*30,sin(radians(temp_deg))*30,3));
- }
- }
- void keys(){
- if(keyPressed){
- if(key == 'a' || key == 'A'){
- tank.setSpeed(PI*3);
- }else if(key == 'd' || key == 'D'){
- tank.setSpeed(-PI*3);
- }else{
- tank.setSpeed(0);
- }
- }else{
- tank.setSpeed(0);
- }
- }
- void renderGame() {
- if (c.available() > 0) {
- input = c.readString();
- //input = input.substring(1, input.indexOf("\n")); // Only up to the newline
- data = float(split(input, ' ')); // Split values into an array
- println("data revived");
- if(data[0] == 0 && data[data.length-1] == 0 && data.length > 300){
- ArrayList<Vec2> surfacein = new ArrayList<Vec2>();
- for(int i = 1; i < data.length-2; i = i + 2){
- surfacein.add(new Vec2(data[i],data[i+1]));
- }
- surfacein.add(new Vec2(0,0));
- surface = new Surface(surfacein);
- }else if(data[0] == 1 && data.length > 1){
- println("client recived update ");
- //newparticles.clear();
- particles.clear();
- for (int i = 1; i <= data.length-6; i = i + 6){
- particles.add(new Particle(data[i],data[i+1],data[i+2],data[i+3],3,data[i+4],data[i+5]));
- }
- }
- }
- keys();
- background(255);
- box2d.step();
- tank.display();
- if(nettimer == 20){
- c.write("1 ");
- }else if(nettimer >= 40){
- nettimer = 0;
- }
- nettimer = nettimer + 1;
- /*
- for (int i = 0; i < boundaries.size(); i++) {
- Boundary wall = (Boundary) boundaries.get(i);
- wall.display();
- }*/
- if(surface != null){
- surface.display();
- }
- fill(0);
- //if(particles != null){
- for (int i = particles.size()-1; i >= 0; i--) {
- Particle p = particles.get(i);
- p.display();
- // Particles that leave the screen, we delete them
- // (note they have to be deleted from both the box2d world and our list
- if (p.done()) {
- particles.remove(i);
- }
- }
- //}
- fill(0);
- text("Use the mouse to aim and fire\nPress A and D to move",12,22);
- }
- float getDistance(int x1,int y1,int x2,int y2){
- return sqrt(sq(abs(x1-x2))+sq(abs(y1-y2)));
- }
- float getAngle(int x1,int y1,int x2,int y2,int offset){
- float output;
- if(x2>x1){
- output=map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180);
- }else{
- output=(180-map(acos((y2-y1)/getDistance(x1,y1,x2,y2)),0,PI,0,180))+180;
- }
- output=output-offset;
- output=mapToDeg(output);
- return output;
- }
- float mapToDeg(float input){
- while(input<0){
- if(input<0){
- input=input+360;
- }
- }
- while(input>=360){
- if(input>=360){
- input=input-360;
- }
- }
- return input;
- }
- boolean button(int x, int y, int w, int h, String textin){
- stroke(0);
- if(mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h){
- if(mousePressed){
- return true;
- }
- fill(255,255,210);
- }else{
- fill(255);
- }
- strokeWeight(1);
- rect(x,y,w,h);
- fill(0);
- text(textin,x+1,y+h-1);
- return false;
- }
1