Anybody any suggestions as to why this is jittering so much?
My goal is to press the mouse whatever, 20, 30 40+ times to create balls, the balls should adhere to gravity, bounce off one another in a realistic way then settle in a realistic way, on top of each other. Same as if I dropped a load of balls into a container in real life. I'm semi happy with the way they adhere to gravity and semi happy with the way they bounce off one another but its just not right. they are not settling correctly.
Any suggestions???
(PS i dont intend on keeping the mouseDragged function, I will be using mouseClicked when the jitters are fixed :) )
float gravity = .9;
ArrayList balls;
void setup() {
size (800, 450);
smooth();
ellipseMode(RADIUS);
background(30);
balls = new ArrayList();
balls.add(new Ball(new PVector(random(10),random(10)), new PVector(random(10),random(10)), 20, int(random(255)), int(random(255)), int(random(255))));
I'm just about to sit down and write out some code to make balls act in a realistic way (i.e do not overlap and ,maybe a slight bounce/repulsion) but before I do, I was curious as to what you think of my code so far.
ie do you think it is structured okay (neat and tidy), and is it the best way of doing things (particularly my loops?).
I plan on developing this a lot in the next few weeks so I'd like to make sure the basics are okay before i go on.
Thanks(in advance) for any feedback
John
CODE:
float gravity = .9;
Ball[] Balls = new Ball[1];
void setup() {
size (800, 450);
smooth();
ellipseMode(RADIUS);
background(30);
PVector posX;
for (int i = 0; i<Balls.length; i++) {
posX = new PVector(1,1);
Balls[i] = new Ball(posX, new PVector(random(10),random(10)), 20, int(random(255)), int(random(255)), int(random(255)));
}
}
void draw () {
background(30);
for (int i = 0; i<Balls.length; i++) {
DetectCollisions (i, Balls);
Balls[i].Udpate();
}
}
void mousePressed() {
// A new ball object
Ball b = new Ball(new PVector(random(10),random(10)), new PVector(random(10),random(10)), 20, int(random(255)), int(random(255)), int(random(255)));
// Append to array
Balls = (Ball[]) append(Balls,b);
}
class Ball {
PVector direction;
PVector position;
float r;
float mass;
int R, G, B;
Ball (PVector tempposition, PVector tempdirection, int radios, int tempR, int tempG, int tempB){
R=tempR;
G=tempG;
B=tempB;
r=radios;
mass = 3.14*r*r;
direction = tempdirection;
position = tempposition;
}
void Udpate(){
direction.y = direction.y + gravity;
position.x += direction.x;
position.y += direction.y;
fill (R, G, B);
ellipse (position.x, position.y, 2*r, 2*r);
// here is where the balls bounce off the four walls
Hi all, can someone please tell me how to make all the balls in this program bounce off each other?? and also is it possible to rearrange them according to a pattern???
I'm stuck with a bit of code and am fairly sure my computer is laughing at me, or else I've been looking at the screen for so long now I'm gone demented...can someone please throw a fresh pair of eyes on this and help me out of a hole!!
the code will create a ball on a mousePress() at a random location, and is subject to gravity. but i want more control over each individual ball, for example id like each ball to be a different (random) color, I've tried a couple of different ways but each time I just get more and more confused.