Question about pisica
in
Contributed Library Questions
•
11 months ago
I want to create letters stick together by pisica. but I dont know how to add it to my code~~ how to change balls to letters~~
Just like this:
import fisica.*;
FWorld world;
color bodyColor = #6E0595;
int spiderCount = 10;
int mainSize = 40;
ArrayList balls = new ArrayList();
void setup() {
size(400, 400);
smooth();
Fisica.init(this);
world = new FWorld();
world.setGravity(0, 0);
for (int i=0; i<spiderCount; i++) {
createSpider();
}
}
void draw() {
background(255);
world.step();
world.draw();
}
void createSpider() {
float posX = random(mainSize/2, width-mainSize/2);
float posY = random(mainSize/2, height-mainSize/2);
FCircle balls = new FCircle(mainSize);
balls.setPosition(posX, posY);
balls.setVelocity(random(-20,20), random(-20,20));
balls.setFillColor(bodyColor);
balls.setNoStroke();
balls.setGroupIndex(2);
world.add(balls);
}
Here is my code:
import rita.*;
import ddf.minim.*;
import fisica.*;
ArrayList balls;
String[] a = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
int global_counter = 0;
int global_counter_limit = 40;
//float easing = 0.005;
int pause_counter_limit = 200;
float START_FRAMES = 150;
boolean clicked = false;
//RiText rt;
FWorld world;
PFont font;
Minim minim;
Fisica.init(this);
world = new FWorld();
world.setGravity(0, 0);
}
void draw() {
background(255);
int i = floor(random(0, a.length));//string
global_counter++;
float r = 15;
if (global_counter >= global_counter_limit) {
balls.add(new Ball(random(width), r, a[i]));
global_counter = 0;
}
for (int j=0; j< balls.size(); j++) {//arraylist
Ball ball = (Ball)balls.get(j);
ball.Ball_jump(4);
ball.Ball_display();
ball.Ball_move();
ball.Ball_bounce();
ball.Ball_interact();
}
//world.step();
//world.draw();
drawGameStart();
}
void drawGameStart() {
float progress = (START_FRAMES - frameCount) / START_FRAMES;
float opacity = max(0, 255 * progress);
fill(0, 0, 0, opacity/1);
noStroke();
rect(0, 0, width * 2, height * 2);
font = loadFont("Papyrus-Condensed-40.vlw");
textFont(font);
textSize(70);
int count = floor(progress * 3) + 1;
if (count < 1) {
fill(255, 255, 255, 0);
text(count, 520, 170);
}
else
{
fill(255, 255, 255);
text(count, 520, 170);
}
//interact();
}
Class Ball:
class Ball {
float x = width/2;
float y = random(10, 60);
float y_old;
float r = 15;
float v = 0;
String str = "0";
//int c;
float acc = 0;
float da = .6;
float viscosity = .3;
float pause_counter;
int dx = 0;
int dy = 0;
Ball(float x, float r, String str) {
this.x = x;
this.r = r;
this.str = str;
pause_counter = 0;
}
void Ball_jump(int magnitude) {
dx = (int)random(-magnitude/2, magnitude/2);
dy = (int)random(-magnitude/2, magnitude/2);
}
void Ball_display() {
fill(0);
pushMatrix();
translate(x, y);
if(y < 60) {
translate(dx, dy);
}
font = loadFont("YuppyTC-Regular-40.vlw");
textFont(font);
textSize(r);
text(str, 0, 0);
popMatrix();
}
void Ball_move() {
/*
// attract to opposite charges
int n = balls.size();
for (int i=0; i<n; i++) {
Ball b = (Ball)balls.get(i);
if (b==this) continue;
float d = dist(x, y, b.x, b.y);
float dd = min(500 / d, 1);
float f = (c != b.c) ? dd : -dd;
float dir = atan2(b.y-y, b.x-x);
x += f * cos(dir) * easing;
y += f * sin(dir) * easing;
}
*/
float posX = random(r/2, width-r/2);
float posY = random(r/2, height-r/2);
FCircle balls = new FCircle(r);
balls.setPosition(posX, posY);
balls.setVelocity(random(-20, 20), random(-20, 20));
balls.setNoStroke();
balls.setGroupIndex(2);
world.add(balls);
pause_counter++;
}
void Ball_bounce() {
y_old = y;
if (pause_counter > pause_counter_limit) {
acc = acc + da;
v = v + acc;
v = v * viscosity;
y = y + v;
}
if (y+r/2 > height) {
if (y_old+r/2 >height) {
y = height - r/2;
}
}
if (y + r/2 > height) {
v = v * -.45;
acc = acc * -.45;
}
}
void Ball_interact() {
if ((clicked == true) && (y == height - r/2)) {
text(str, mouseX, mouseY, r, r);
}
//ball.Ball_display();
//ball.Ball_move();
//ball.Ball_bounce();
}
void mousePressed() {
if (clicked == true)
{
clicked = false;
}
else {
clicked = true;
}
}
}
Thank you!!
Just like this:
import fisica.*;
FWorld world;
color bodyColor = #6E0595;
int spiderCount = 10;
int mainSize = 40;
ArrayList balls = new ArrayList();
void setup() {
size(400, 400);
smooth();
Fisica.init(this);
world = new FWorld();
world.setGravity(0, 0);
for (int i=0; i<spiderCount; i++) {
createSpider();
}
}
void draw() {
background(255);
world.step();
world.draw();
}
void createSpider() {
float posX = random(mainSize/2, width-mainSize/2);
float posY = random(mainSize/2, height-mainSize/2);
FCircle balls = new FCircle(mainSize);
balls.setPosition(posX, posY);
balls.setVelocity(random(-20,20), random(-20,20));
balls.setFillColor(bodyColor);
balls.setNoStroke();
balls.setGroupIndex(2);
world.add(balls);
}
Here is my code:
import rita.*;
import ddf.minim.*;
import fisica.*;
ArrayList balls;
String[] a = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
int global_counter = 0;
int global_counter_limit = 40;
//float easing = 0.005;
int pause_counter_limit = 200;
float START_FRAMES = 150;
boolean clicked = false;
//RiText rt;
FWorld world;
PFont font;
Minim minim;
Fisica.init(this);
world = new FWorld();
world.setGravity(0, 0);
}
void draw() {
background(255);
int i = floor(random(0, a.length));//string
global_counter++;
float r = 15;
if (global_counter >= global_counter_limit) {
balls.add(new Ball(random(width), r, a[i]));
global_counter = 0;
}
for (int j=0; j< balls.size(); j++) {//arraylist
Ball ball = (Ball)balls.get(j);
ball.Ball_jump(4);
ball.Ball_display();
ball.Ball_move();
ball.Ball_bounce();
ball.Ball_interact();
}
//world.step();
//world.draw();
drawGameStart();
}
void drawGameStart() {
float progress = (START_FRAMES - frameCount) / START_FRAMES;
float opacity = max(0, 255 * progress);
fill(0, 0, 0, opacity/1);
noStroke();
rect(0, 0, width * 2, height * 2);
font = loadFont("Papyrus-Condensed-40.vlw");
textFont(font);
textSize(70);
int count = floor(progress * 3) + 1;
if (count < 1) {
fill(255, 255, 255, 0);
text(count, 520, 170);
}
else
{
fill(255, 255, 255);
text(count, 520, 170);
}
//interact();
}
Class Ball:
class Ball {
float x = width/2;
float y = random(10, 60);
float y_old;
float r = 15;
float v = 0;
String str = "0";
//int c;
float acc = 0;
float da = .6;
float viscosity = .3;
float pause_counter;
int dx = 0;
int dy = 0;
Ball(float x, float r, String str) {
this.x = x;
this.r = r;
this.str = str;
pause_counter = 0;
}
void Ball_jump(int magnitude) {
dx = (int)random(-magnitude/2, magnitude/2);
dy = (int)random(-magnitude/2, magnitude/2);
}
void Ball_display() {
fill(0);
pushMatrix();
translate(x, y);
if(y < 60) {
translate(dx, dy);
}
font = loadFont("YuppyTC-Regular-40.vlw");
textFont(font);
textSize(r);
text(str, 0, 0);
popMatrix();
}
void Ball_move() {
/*
// attract to opposite charges
int n = balls.size();
for (int i=0; i<n; i++) {
Ball b = (Ball)balls.get(i);
if (b==this) continue;
float d = dist(x, y, b.x, b.y);
float dd = min(500 / d, 1);
float f = (c != b.c) ? dd : -dd;
float dir = atan2(b.y-y, b.x-x);
x += f * cos(dir) * easing;
y += f * sin(dir) * easing;
}
*/
float posX = random(r/2, width-r/2);
float posY = random(r/2, height-r/2);
FCircle balls = new FCircle(r);
balls.setPosition(posX, posY);
balls.setVelocity(random(-20, 20), random(-20, 20));
balls.setNoStroke();
balls.setGroupIndex(2);
world.add(balls);
pause_counter++;
}
void Ball_bounce() {
y_old = y;
if (pause_counter > pause_counter_limit) {
acc = acc + da;
v = v + acc;
v = v * viscosity;
y = y + v;
}
if (y+r/2 > height) {
if (y_old+r/2 >height) {
y = height - r/2;
}
}
if (y + r/2 > height) {
v = v * -.45;
acc = acc * -.45;
}
}
void Ball_interact() {
if ((clicked == true) && (y == height - r/2)) {
text(str, mouseX, mouseY, r, r);
}
//ball.Ball_display();
//ball.Ball_move();
//ball.Ball_bounce();
}
void mousePressed() {
if (clicked == true)
{
clicked = false;
}
else {
clicked = true;
}
}
}
Thank you!!
1