We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Ellipse Recoil
Page Index Toggle Pages: 1
Ellipse Recoil (Read 532 times)
Ellipse Recoil
Jan 14th, 2010, 12:57pm
 
first of all im sorry for my english, it isnt that good.
im gonna catch up with my problem:

what i try to do is creating objects(ellipses) random on the stage.
these ellipses are growing from 0 to "maxSize".

what i want to do is, if one ellipse touches another it should recoil.

i tried so much. till now im trying to find a solution with PVector.

dont be to hard with me, im not a professional - just a student.


this is my code:
Code:
int stageWidth = 1200;
int stageHeight = 800;
int maxBalls = 50;
PVector[] v2 = new PVector[maxBalls];
MagneticBall[] balls = new MagneticBall[maxBalls];

void setup() {
size(stageWidth, stageHeight);
background(40);
smooth();
frameRate(60);
for(int i = 0; i < balls.length; i++) {
v2[i] = new PVector(random(-1, 1), random(-1, 1));
balls[i] = new MagneticBall(1, 1, random(-200, 200), random(-200, 200), random(40, 255), 0.1, random(20, 60));
balls[i].allBalls = balls;
}
}

void draw() {
background(40);
translate(stageWidth/2, stageHeight/2);
for(int i = 0; i<balls.length; i++) {

balls[i].drawMagneticBall(v2[i]);
};
}



and the BallsClass
Code:
class MagneticBall {
float ballSize;
float ballSizeGrow;
float ballSizeMax;

float ballPositionX;
float ballPositionY;


float ballSpeed;
float ballSpeed2;

float d;

float g1;

int moveDirection;
int moveThis;

MagneticBall[] allBalls;

float strokeWidth;
float strokeColor;


PVector v1;
PVector vPos;


MagneticBall(float tempBallSize, float tempStrokeWidth, float tempBallPositionX, float tempBallPositionY, float tempStrokeColor, float tempBallSizeGrow, float tempBallSizeMax) {
ballSizeMax = tempBallSizeMax;
ballSizeGrow = tempBallSizeGrow;
ballSize = tempBallSize;
strokeWidth = tempStrokeWidth;
ballPositionX = tempBallPositionX;
ballPositionY = tempBallPositionY;
strokeColor = tempStrokeColor;
vPos = new PVector(tempBallPositionX, tempBallPositionY);
}

void drawMagneticBall(PVector tempV2) {
g1++;
v1 = tempV2;
noFill();
stroke(strokeColor);
if(ballSize < ballSizeMax) {
ballSizeGrow += 0.01;
ballSize += ballSizeGrow;
}
strokeWeight(strokeWidth);
for(int i = 0; i<maxBalls; i++) {
if(ballPositionX != allBalls[i].ballPositionX) {
d = dist(ballPositionX, ballPositionY, allBalls[i].ballPositionX, allBalls[i].ballPositionY);
if(d < ballSize/2+allBalls[i].ballSize/2) {
v1 = allBalls[i].v1;
}

}

}
v1.div(1.1);

vPos.add(v1);



ellipse(vPos.x, vPos.y, ballSize, ballSize);
}





}















any questions and answers are welcome. thank you Smiley



blaufasan


Re: Ellipse Recoil
Reply #1 - Jan 15th, 2010, 3:27am
 
Have you seen the BouncyBubbles example?

File > Examples > Topics > Motion > BouncyBubbles

The example's Ball class has a function called collide() that sounds like it's just what you want...
Page Index Toggle Pages: 1