We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm going through Nature Of Code at the moment and this exercise from Chapter 2 :
So, I have the ball and stuff and I've made a boarder, but I can't seem to get anything working properly for a boundary that repels the ball. I'm gradually descending into an atrocious piece of code without any real solution.
I created a method called 'ping' that took as it's input a pixel size which would represent the border that would repel the ball object.
This was set to 100, so when the ball got to 100 pixels away from the edge of the screen it should start to be repelled by this ping method.
What I do either doesn't work or it gradually makes the ball shoot out faster and faster, because the ping method is increasing the balls acceleration in an opposite but greater amount to when it came in... Which keeps happening, hence the speed up.
I thought that maybe I could map the border size to the value of the incoming acceleration and only effect it by that amount, but I didn't get anywhere with that.
I've currently tried to make it so that the ping method will only affect the value of the acceleration while it's less than the opposite of what it came in as. So if it came into the border with an acceleration value of 4, it could only leave with a value of -4, r at least the ping would only work to that point.
That didn't really work though, and I'm getting kind of lost with it now.
Here's the code, sorry it's a bit urjhj
cheers :)
void setup() {
size(350, 350);
background(0);
b = new Balloon();
}
Balloon b;
float nx, ny, x, y;
void draw() {
background(0);
drawBoarder();
float dev = 0.1;
x = random(-noise(nx)*dev, noise(nx)*dev);
y = random(-noise(ny)*dev, noise(ny)*dev);
PVector helium = new PVector(random(-dev, dev), random(-dev, dev));
b.update();
b.display();
b.checkEdges();
b.ping(100);
b.applyForce(helium);
helium.x = 0.0;
helium.y = 0.0;
}
class Balloon {
PVector acceleration;
PVector location;
PVector velocity;
float mass = 1;
float ping;
Balloon() {
location = new PVector(width/2, height/2);
acceleration = new PVector(0, 0);
velocity = new PVector(0, 0);
}
void display() {
fill(200, 10, 60);
noStroke();
ellipse(location.x, location.y, 40, 40);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void ping(float b) {
float p = 0.02;
PVector ca = new PVector(acceleration.x, acceleration.y);
PVector push = PVector.div(velocity, b);
if (location.x < b) { //negative initial
acceleration.x += p;
} else if (location.x > width -b) {
acceleration.x -= p;
}
if (location.y < b) { //negative initial
acceleration.y += p;
} else if (location.y > height-b) { // positive initial
acceleration.y -= p;
}
}
void checkEdges() {
if (location.y < 0) {
location.y = 0;
velocity.y *= -1;
} else if (location.y > height) {
location.y = height;
velocity.y *= -1;
}
if (location.x > width) {
location.x = width;
velocity.x *= -1;
} else if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
}
}
}
// ----------------------------------------------
void drawBoarder() {
int size = 100;
fill(255, 30);
rect(0, 0, size, height);
rect(0, 0, width, size);
rect(width-size, 0, width, height);
rect(0, height-size, width, height-size);
}
Comments