r/processing • u/RafielPrime • Aug 03 '24
Beginner help request dumb stupid animation student need help
Hi i need help with some code i need to do for animation homework, basically, i have these balls that fly around the screen and are repelled from my cursor, but they never come to a stop. i just want them to slow down and come to an eventual stop and I've really pushed my brain to its limit so i cant figure this out. could someone please help
bonus points if anyone can have the balls spawn in on an organised grid pattern, and make it so when the cursor moves away from repelling them, they move back to their original spawn loacation but i dont know how hard that is
This is the code,
Ball[] balls = new Ball[100];
void setup()
{
size(1000, 1000);
for (int i=0; i < 100; i++)
{
balls[i] = new Ball(random(width), random(height));
}
}
void draw()
{
background(50);
for (int i = 0; i < 50; i++)
{
balls[i].move();
balls[i].render();
}
}
class Ball
{
float r1;
float b1;
float g1;
float d;
PVector ballLocation;
PVector ballVelocity;
PVector repulsionForce;
float distanceFromMouse;
Ball(float x, float y)
{
d = (30);
d = (30);
r1= random(50, 100);
b1= random(100, 100);
g1= random(50, 100);
ballVelocity = new PVector(random(0, 0), random(0, 0));
ballLocation = new PVector(x, y);
repulsionForce = PVector.sub(ballLocation, new PVector(mouseX, mouseY));
}
void render()
{
fill(r1, g1, b1);
ellipse(ballLocation.x, ballLocation.y, d, d);
}
void move()
{
bounce();
curs();
ballVelocity.limit(3);
ballLocation.add(ballVelocity);
}
void bounce()
{
if (ballLocation.x > width - d/2 || ballLocation.x < 0 + d/2)
{
ballVelocity.x = -ballVelocity.x;
ballLocation.add(ballVelocity);
}
if (ballLocation.y > height - d/2 || ballLocation.y < 0 + d/2)
{
ballVelocity.y = -ballVelocity.y;
ballLocation.add(ballVelocity);
}
}
void curs()
{
repulsionForce = PVector.sub(ballLocation, new PVector(mouseX, mouseY));
if (repulsionForce.mag() < 150) {
repulsionForce.normalize();
repulsionForce.mult(map(distanceFromMouse, 0, 10, 2, 0));
ballVelocity.add(repulsionForce);
}
}
}
5
u/gust334 Aug 03 '24
Within your class Ball,
void reduceSpeed() {ballVelocity.x *= 0.95;// try this, and you need to guess what goes here}and within your draw, add the method call:
balls[i].move();balls[i].render();balls[i].reduceSpeed(); // add me!4
u/MandyBrigwell Moderator Aug 03 '24
How much of your code do you understand? Are you able to explain what each part of your code is doing in words?
For example, your setup function creates an array of balls at random positions on the screen within the width and height of the canvas. If you understand that, then you understand where to focus your attention to make them spawn in a regular grid pattern.
If you don't understand that, then this also tells you where to go—tutorials and examples are a great place to start: https://openprocessing.org/sketch/941442/
[Mod hat on: I'm not keen on your post title, but people seem to be responding so we'll ignore it…]
3
u/Salanmander Aug 03 '24
If you just want the balls to slow down, rather than continuing to accelerate away from the cursor, you could apply some sort of damping that opposes its current motion.
Alternately, if you add a variable to the Ball class that always holds the original location, you could apply a force back towards that. You'd probably still want some damping if you don't want it to oscillate a ton.
Because you're setting this up in a physics-simulation way, you can probably just think "what could physically make the result I want happen?" and then approach it that way.
3
u/trumpetarebest Aug 03 '24
in your move() function, multiply the velocity by a damping factor(anywhere between 0 and 1) with ballVelocity.mult(dampingFactor); i found that 0.9 works
0
u/[deleted] Aug 03 '24
[deleted] — view deleted comment