Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ball {
- ArrayList<Ball> others;
- PVector pos, vel, acc, siz;
- PImage img;
- color col = color(255, 0, 0);
- float startSpeed = 5;
- float maxSpeed = 10;
- int posMode = CENTER;
- boolean gravity = false;
- boolean bounded = true;
- boolean selfReflect = true;
- boolean speedEnforced = true;
- boolean offScreen = false;
- public Ball() {
- pos = new PVector(width/2, height/2);
- vel = new PVector(-5, 5);
- siz = new PVector(40, 40);
- vel.limit(startSpeed);
- }
- public Ball(PVector start) {
- pos = start;
- vel = new PVector(-5, 5);
- siz = new PVector(40, 40);
- vel.limit(startSpeed);
- }
- public Ball(PVector start, PVector startVel) {
- pos = start;
- vel = startVel;
- siz = new PVector(40, 40);
- vel.limit(startSpeed);
- }
- public Ball(PVector start, PVector startVel, color _col) {
- pos = start;
- vel = startVel;
- siz = new PVector(40, 40);
- vel.limit(startSpeed);
- col = _col;
- }
- public Ball(ArrayList<Ball> _others, PVector start, PVector startVel, color _col) {
- others = _others;
- pos = start;
- vel = startVel;
- siz = new PVector(40, 40);
- vel.limit(startSpeed);
- col = _col;
- }
- void update() {
- //vel.add(acc);
- pos.add(vel);
- }
- void check() {
- if (speedEnforced) {
- vel.limit(maxSpeed);
- }
- if (bounded) {
- if (abs(pos.x - width/2)>width/2-siz.x/2) {
- //undo last step
- pos.sub(vel);
- vel.x*=-1;
- pos.add(vel);
- }
- if (abs(pos.y - height/2)>height/2-siz.y/2) {
- //undo last step
- pos.sub(vel);
- vel.y*=-1;
- pos.add(vel);
- }
- }
- else{
- //if the ball is not bounded by the screen and it leaves far enough, remove it
- offScreen = (abs(pos.x - width/2)>width/2-siz.x/2)&&(abs(pos.y - height/2)>height/2-siz.y/2);
- //Move responibility to removal to outside of class for safety
- //if (offScreen &&others != null){
- // others.remove(this);
- //}
- }
- //check other balls
- if (selfReflect && others != null) {
- for (Ball b : others) {
- if (b != this) {//don't let it bounce with it self
- if (pos.dist(b.pos)< (siz.x+b.siz.x)/2) {
- PVector normal = PVector.sub(b.pos, pos);
- float theta = PVector.angleBetween(vel, normal);
- //draw an effect at the point of impact
- //hitEffectPos = temp;
- //starting size
- //hitEffectSize = 5;
- //remove the last step (?)
- //bPos.sub(bVel);
- //apply the angle of reflection
- vel.rotate(-2*theta);
- pos.add(vel);
- theta = PVector.angleBetween( normal,b.vel);
- b.vel.rotate(2*theta);
- b.pos.add(b.vel);
- //move the
- }
- }
- }
- }
- }
- void draw() {
- fill(col);
- ellipse(pos.x, pos.y, siz.x, siz.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement