I’m trying to make a chat input space where the user can type in a string of text and when submitted it would appear as an object that is affected by physics. I am stuck because whenever I send a new string of text, the previous instances of text inputs would change to the new text input.
Heres what I have:
import shiffman.box2d.*;
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.*;
Box2DProcessing box2d;
ArrayList boundaries;
ArrayList<Box> boxes;
PFont f;
String typing = "";
String saved = "";
void setup() {
size(1500,1000);
smooth();
f = createFont("arial",40);
box2d = new Box2DProcessing(this);
box2d.createWorld();
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));
boxes = new ArrayList<Box>();
}
void draw() {
background(255);
int indent = 25;
textFont(f);
fill(0);
text("Click in this window and type. \nHit enter to save. ", indent, 40);
text("Input: " + typing,indent,190);
box2d.step();
for (int i = 0; i < boundaries.size(); i++) {
Boundary wall = (Boundary) boundaries.get(i);
wall.display();
}
for (Box b: boxes) {
b.display();
}
}
void keyPressed(){
if(key==ENTER){
Box p = new Box(mouseX,mouseY);
boxes.add(p);
saved = typing;
typing = "";
}
else {
typing = typing + key;
}
}
Boundary class
class Boundary {
float x;
float y;
float w;
float h;
Body b;
Boundary(float x_,float y_, float w_, float h_, float a) {
x = x_;
y = y_;
w = w_;
h = h_;
PolygonShape sd = new PolygonShape();
// Figure out the box2d coordinates
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
sd.setAsBox(box2dW, box2dH);
BodyDef bd = new BodyDef();
bd.type = BodyType.STATIC;
bd.angle = a;
bd.position.set(box2d.coordPixelsToWorld(x,y));
b = box2d.createBody(bd);
b.createFixture(sd,1);
}
void display() {
noFill();
stroke(0);
strokeWeight(10);
rectMode(CENTER);
float a = b.getAngle();
pushMatrix();
translate(x,y);
rotate(-a);
rect(0,0,w,h);
popMatrix();
}
}
Box class
class Box {
Body body;
float w,h;
Box(float x, float y) {
w = 50;
h = 50;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(x,y));
body = box2d.createBody(bd);
PolygonShape sd = new PolygonShape();
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
sd.setAsBox(box2dW, box2dH);
FixtureDef fd = new FixtureDef();
fd.shape = sd;
fd.density = 10;
fd.friction = 0.3;
fd.restitution = 0.5;
body.createFixture(fd);
}
void display() {
Vec2 pos = box2d.getBodyPixelCoord(body);
float a = body.getAngle();
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(0);
stroke(0);
textAlign(CENTER);
text(saved,0,0);
popMatrix();
}
}
Can i get some guidance as to how to get the outcome I am looking for?