Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Point=PIXI.ObservablePoint;
Object.assign(Number.prototype, {
map(in_min, in_max, out_min, out_max) {
return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
},
constrain(low, high) {
return Math.max(Math.min(this, high), low);
}
});
Object.assign(PIXI.ObservablePoint.prototype, {
add(o) {
this.set(this.x += o.x, this.y += o.y);
},
radd(o) {
return new PIXI.ObservablePoint(() => 0, window, this.x + o.x, this.y + o.y);
},
sub(o) {
this.set(this.x -= o.x, this.y -= o.y);
},
rsub(o) {
return new PIXI.ObservablePoint(() => 0, window, this.x - o.x, this.y - o.y);
},
mult(o) {
this.x *= o;
this.y *= o;
},
rmult(o) {
return new PIXI.ObservablePoint(() => 0, window, this.x * o, this.y * o);
},
getAngleBetween(o){
return Math.atan2(o.y-this.y,o.x-this.x);
},
getAngle() {
//in radians
return Math.atan2(this.y, this.x);
},
getMag() {
return Math.hypot(this.x, this.y);
},
setMag(l) {
let angle = this.getAngle();
this.set(Math.cos(angle) * l, Math.sin(angle) * l);
},
setAngle(a) {
/*in radians;*/
this.set(Math.cos(a) * this.mag(), Math.sin(a) * this.mag());
},
lerp(to, amount) {
this.x += (to.x - this.x) * amount;
this.y += (to.y - this.y) * amount;
},
distanceTo(O) {
let d = O.rsub({x: this.x, y: this.y});
d = d.getMag();
return d;
},
copy(){
return new PIXI.ObservablePoint(()=>0,window,this.x,this.y);
}
});
class Img extends PIXI.Sprite {
constructor(asset, x = Math.random() * app.renderer.width, y = Math.random() * app.renderer.height) {
super(PIXI.Texture.fromImage(asset));
this.position.set(x, y);
this.angle = 0;
this.scale.set(0.5);
// this.scale.x=-0.5;
// this.tint = Math.random() * 0xffffff;
this.anchor.set(.5);
this.len = Math.random() * 0.5;
this.angle = Math.random() * 2 * Math.PI;
this.vel = new PIXI.ObservablePoint(() => 0, this, Math.cos(this.angle) * this.len, Math.sin(this.angle) * this.len);
this.acc = new PIXI.ObservablePoint(() => 0, this, 0, 0);
this.rotation = 2 * Math.random() ;
this.heading = Math.atan2(this.vel.y, this.vel.x);
this.target = new PIXI.ObservablePoint(() => 0, this, app.renderer.width * Math.random(), app.renderer.height * Math.random());
this.amount = Math.random() / 100;
}
move(o) {
this.vel.add(this.acc);
this.position.add(this.vel);
this.heading = this.vel.getAngle();
this.rotation = this.heading;
// this.position.lerp(o,this.amount);
}
edge() {
(this.x > app.renderer.width) && (this.x = 0);
(this.y > app.renderer.height) && (this.y = 0);
(this.x < 0) && (this.x = app.renderer.width);
(this.y < 0) && (this.y = app.renderer.height);
}
attract(F) {
this.force = F.rsub(this.position);
this.d = this.force.mag();
this.d = this.d.constrain(1, 25);
this.G = Math.random();
this.strength = this.G / (this.d * this.d);
this.force.setLength(this.strength);
if (this.d < 10) {
this.force.mult(-1);
}
this.acc.add(this.force);
}
follow(o){
this.heading = this.position.getAngleBetween(this.target);
this.rotation = this.heading;
this.position.lerp(this.target,0.02);
(this.position.distanceTo(this.target)<10)&&(this.target.set(app.renderer.width * Math.random(), app.renderer.height * Math.random()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.