Hi,
I am using Fisica library for a game. I have different types of FCircle (circle2, circle2) of different size and one type of FBox rectangle1. I am able to generate actions when a circle collides with a rectangle (make the circle disappear or fill black for example). I would like to be able to differentiate the bodies involved in the collision. Now, I can only refer to body1 and body2 which disregards the class. I would like to fill black circle1 and remove circle2 when they are the body involved in the collision with the rectangle. Here is my script with an attempt where I commented the lines that are not working.
Thank you for your help, I am very new to coding.
import fisica.*;
FWorld world;
FCircle circle1;
FCircle circle2;
FCircle circleButton1;
FCircle circleButton2;
FBox rectangle1;
int gx = 0; // horizontal gravity;
int gy= 0; // vertical gravity;
ArrayList<FBox> rectangles = new ArrayList<FBox>();
ArrayList<FCircle> circles1 = new ArrayList<FCircle>();
ArrayList<FCircle> circles2 = new ArrayList<FCircle>();
//------------------------------------------
void contactStarted(FContact c) {
FBody circle1 = null;
if (c.getBody1() == rectangle1){
circle1 = c.getBody2();
}
else if ( c.getBody2() == rectangle1){
circle1 = c.getBody1();
}
if (circle1 ==null){
return;
}
world.remove(circle1);
}
//-----------------------------------
void setup() {
size(1500, 2000);
smooth();
Fisica.init(this);
world = new FWorld ();
world.setEdges();
circleButton1 = new FCircle(50);
circleButton1.setPosition(1000/6, height-1900);
circleButton1.setStatic(true);
circleButton1.setGrabbable(false);
circleButton1.setNoFill();
circleButton1.setStroke(0);
circleButton1.setGrabbable(false);
circleButton1.setStrokeWeight(3);
world.add(circleButton1);
circleButton2 = new FCircle(50);
circleButton2.setPosition(1000/5, height-1900);
circleButton2.setStatic(true);
circleButton2.setGrabbable(false);
circleButton2.setNoFill();
circleButton2.setStroke(0);
circleButton2.setStrokeWeight(3);
circleButton2.setGrabbable(false);
world.add(circleButton2);
rectangle1 = new FBox (225, 25);
rectangle1.setPosition(200, 600);
rectangle1.setVelocity(random(50, 250), random(50, 250));
rectangle1.setRestitution(1);
rectangle1.setDamping(0.0);
rectangle1.setStatic(true);
rectangle1.setGrabbable(false);
rectangle1.setStrokeWeight(3);
rectangle1.setFill(0);
rectangle1.setStroke(0);
rectangles.add(rectangle1);
world.add(rectangle1);
}
void draw() {
background(255);
world.setGravity(gx, gy);
if (keyPressed == true) {
if (key == 'a') {
gx = (-400);
gy = (100);
}
if (key == 'd') {
gx = (400);
gy = (100);
}
if (key == 's') {
gx = (0);
gy = (400);
}
if (key == 'w') {
gx = (0);
gy = (-400);
}
} else {
gx=0;
gy=1000;
}
world.draw();
world.step();
}
void mousePressed() {
FBody pressed = world.getBody(mouseX, mouseY);
if (pressed == circleButton1) {
FCircle circle1 = new FCircle(30);
circle1.setPosition(1000/6, height-1800);
circle1.setRotation(random(TWO_PI));
circle1.setVelocity(0, random(200, 500));
circle1.setNoFill();
circle1.setStroke(0);
circle1.setGrabbable(false);
circle1.setStrokeWeight(2);
circles1.add(circle1);
world.add(circle1);
}
if (pressed == circleButton2) {
FCircle circle2 = new FCircle(80);
circle2.setPosition(1000/6, height-1800);
circle2.setRotation(random(TWO_PI));
circle2.setVelocity(0, random(200, 500));
circle2.setNoFill();
circle2.setStroke(0);
circle2.setStrokeWeight(2);
circle2.setGrabbable(false);
circles2.add(circle2);
world.add(circle2);
}
}