Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
mirco.piludu
mirco.piludu's Profile
1
Posts
6
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Bouncing Balls with mobile objects
[12 Replies]
15-Sep-2013 09:36 AM
Forum:
Programming Questions
Hi everyone,
I'm new in sketching with Processing and very very bad in English.
So, here's my sketch: I have a mobile square generating some points bouncing everywhere.
The problem is that bouncing is incorrect, I hope someone can find why of this.
Here's the code:
Mover mover;
// INIZIAZIONE VARIABILI
int width = 600; // schermo
int height = 600;
float dim = 20; // punti
float posX = width/2; // quadratino
float posY = height/2;
int boxSize = 15;
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
float pos2x = 20; // secondo quadratino
float pos2y = 20;
int box2size = 15;
boolean overBox2 = false;
boolean locked2 = false;
float xOffset2 = 0.0;
float yOffset2 = 0.0;
void setup() {
size (width, height);
smooth();
rectMode (RADIUS);
mover = new Mover();
}
void draw() {
background (255);
// PUNTAMENTO PRIMO QUADRATO
if (mouseX > posX - boxSize && mouseX < posX + boxSize &&
mouseY > posY - boxSize && mouseY < posY + boxSize) {
overBox = true;
if (!locked) {
stroke(255, 0, 0);
strokeWeight(3);
}
} else {
stroke (0);
strokeWeight(2);
overBox = false;
}
strokeWeight(2);
rect (posX, posY, boxSize, boxSize);
// PUNTAMENTO SECONDO QUADRATO
if (mouseX > pos2x - box2size && mouseX < pos2x + box2size &&
mouseY > pos2y - box2size && mouseY < pos2y + box2size) {
overBox2 = true;
if (!locked2) {
stroke(0);
}
} else {
overBox2 = false;
}
stroke (0);
strokeWeight (2);
rect (pos2x, pos2y, box2size, box2size);
mover.update();
mover.checkEdges();
mover.display();
}
void mousePressed() {
if (overBox) { // pressione primo quadrato
locked = true;
xOffset = mouseX - posX;
yOffset = mouseY - posY;
} else {
locked = false;
}
if (overBox2) { // pressione secondo quadrato
locked2 = true;
xOffset2 = mouseX - pos2x;
yOffset2 = mouseY - pos2y;
} else {
locked2 = false;
}
}
void mouseDragged() {
if (locked) {
posX = mouseX - xOffset;
posX = constrain (posX, 16, width - 17);
posY = mouseY - yOffset;
posY = constrain (posY, 16, height - 17);
}
if (locked2) {
pos2x = mouseX - xOffset2;
pos2x = constrain (pos2x, 31, width - 32);
pos2y = mouseY - yOffset;
pos2y = constrain (pos2y, 31, height - 32);
}
}
void mouseReleased() {
locked = false;
locked2 = false;
}
class Mover {
PVector location1; // dichiarazione dei vettori
PVector location2;
PVector location3;
PVector location4;
PVector location5;
PVector location6;
PVector location7;
PVector location8;
PVector velocity1;
PVector velocity2;
PVector velocity3;
PVector velocity4;
PVector velocity5;
PVector velocity6;
PVector velocity7;
PVector velocity8;
Mover() {
location1 = new PVector (posX, posY); // dichiarazione del valore dei vettori
location2 = new PVector (posX, posY);
location3 = new PVector (posX, posY);
location4 = new PVector (posX, posY);
location5 = new PVector (posX, posY);
location6 = new PVector (posX, posY);
location7 = new PVector (posX, posY);
location8 = new PVector (posX, posY);
velocity1 = new PVector (0.5, 0);
velocity2 = new PVector (0.35, 0.35);
velocity3 = new PVector (0, 0.5);
velocity4 = new PVector (-0.35, 0.35);
velocity5 = new PVector (-0.5, 0);
velocity6 = new PVector (-0.35, -0.35);
velocity7 = new PVector (0, -0.5);
velocity8 = new PVector (0.35, -0.35);
}
void update() {
location1.add(velocity1); // somma posizioni e velocità corrispondenti
location2.add(velocity2);
location3.add(velocity3);
location4.add(velocity4);
location5.add(velocity5);
location6.add(velocity6);
location7.add(velocity7);
location8.add(velocity8);
}
void display() {
stroke (0);
strokeWeight(dim);
point(location1.x,location1.y);
point(location2.x,location2.y);
point(location3.x,location3.y);
point(location4.x,location4.y);
point(location5.x,location5.y);
point(location6.x,location6.y);
point(location7.x,location7.y);
point(location8.x,location8.y);
}
void checkEdges() {
frameRate(150);
// CONTROLLO RIMBALZI PERIMETRO
if ((location1.x >= width) || (location1.x <= 0)) { // controllo rimbalzo
velocity1.x = velocity1.x * -1;
}
if ((location1.y >= height) || (location1.y <= 0)) {
velocity1.y = velocity1.y * -1;
}
if ((location2.x >= width) || (location2.x <= 0)) {
velocity2.x = velocity2.x * -1;
}
if ((location2.y >= height) || (location2.y <= 0)) {
velocity2.y = velocity2.y * -1;
}
if ((location3.x >= width) || (location3.x <= 0)) {
velocity3.x = velocity3.x * -1;
}
if ((location3.y >= height) || (location3.y <= 0)) {
velocity3.y = velocity3.y * -1;
}
if ((location4.x >= width) || (location4.x <= 0)) {
velocity4.x = velocity4.x * -1;
}
if ((location4.y >= height) || (location4.y <= 0)) {
velocity4.y = velocity4.y * -1;
}
if ((location5.x >= width) || (location5.x <= 0)) {
velocity5.x = velocity5.x * -1;
}
if ((location5.y >= height) || (location5.y <= 0)) {
velocity5.y = velocity5.y * -1;
}
if ((location6.x >= width) || (location6.x <= 0)) {
velocity6.x = velocity6.x * -1;
}
if ((location6.y >= height) || (location6.y <= 0)) {
velocity6.y = velocity6.y * -1;
}
if ((location7.x >= width) || (location7.x <= 0)) {
velocity7.x = velocity7.x * -1;
}
if ((location7.y >= height) || (location7.y <= 0)) {
velocity7.y = velocity7.y * -1;
}
if ((location8.x >= width) || (location8.x <= 0)) {
velocity8.x = velocity8.x * -1;
}
if ((location8.y >= height) || (location8.y <= 0)) {
velocity8.y = velocity8.y * -1;
}
// CONTROLLO RIMBALZI QUADRATINO
if ((location1.x > pos2x - box2size) && (location1.x < pos2x + box2size)) { // 1°
velocity1.x = velocity1.x * -1;
}
if ((location1.y > pos2y - box2size) && (location1.y < pos2y + box2size)) {
velocity1.y = velocity1.y * -1;
}
if ((location2.x > pos2x - box2size) && (location2.x < pos2x + box2size)) { // 2°
velocity2.x = velocity2.x * -1;
}
if ((location2.y > pos2y - box2size) && (location2.y < pos2y + box2size)) {
velocity2.y = velocity2.y * -1;
}
if ((location3.x > pos2x - box2size) && (location3.x < pos2x + box2size)) { // 3°
velocity3.x = velocity3.x * -1;
}
if ((location3.y > pos2y - box2size) && (location3.y < pos2y + box2size)) {
velocity3.y = velocity3.y * -1;
}
if ((location4.x > pos2x - box2size) && (location4.x < pos2x + box2size)) { // 4°
velocity4.x = velocity4.x * -1;
}
if ((location4.y > pos2y - box2size) && (location4.y < pos2y + box2size)) {
velocity4.y = velocity4.y * -1;
}
if ((location5.x > pos2x - box2size) && (location5.x < pos2x + box2size)) { // 5°
velocity5.x = velocity5.x * -1;
}
if ((location5.y > pos2y - box2size) && (location5.y < pos2y + box2size)) {
velocity5.y = velocity5.y * -1;
}
if ((location6.x > pos2x - box2size) && (location6.x < pos2x + box2size)) { // 6°
velocity6.x = velocity6.x * -1;
}
if ((location6.y > pos2y - box2size) && (location6.y < pos2y + box2size)) {
velocity6.y = velocity6.y * -1;
}
if ((location7.x > pos2x - box2size) && (location7.x < pos2x + box2size)) { // 7°
velocity7.x = velocity7.x * -1;
}
if ((location7.y > pos2y - box2size) && (location7.y < pos2y + box2size)) {
velocity7.y = velocity7.y * -1;
}
if ((location8.x > pos2x - box2size) && (location8.x < pos2x + box2size)) { // 8°
velocity8.x = velocity8.x * -1;
}
if ((location8.y > pos2y - box2size) && (location8.y < pos2y + box2size)) {
velocity8.y = velocity8.y * -1;
}
// DECREMENTO DIMENSIONE PUNTI
dim = dim - 0.01;
dim = constrain (dim, 1, 20);
// RIAVVIO AUTOMATICO
if (dim == 1) {
location1.x = posX;
location1.y = posY;
location2.x = posX;
location2.y = posY;
location3.x = posX;
location3.y = posY;
location4.x = posX;
location4.y = posY;
location5.x = posX;
location5.y = posY;
location6.x = posX;
location6.y = posY;
location7.x = posX;
location7.y = posY;
location8.x = posX;
location8.y = posY;
dim = 20;
}
}
}
The code is very bad organised cause I haven't improved myself yet in using arrays..!
Thanks everyone.
«Prev
Next »
Moderate user : mirco.piludu
Forum