Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- circleRect(circlePos, circleSize, rectPos, rectSize) {
- //its rectRect collision but it doesnt matter because reflection surface is always horizontal or vertical
- let r1 = {
- left: circlePos.x - circleSize.x/2,
- right: circlePos.x + circleSize.x/2,
- top: circlePos.y - circleSize.y/2,
- bottom: circlePos.y + circleSize.y/2
- };
- let r2 = {
- left: rectPos.x,
- right: rectPos.x + rectSize.x,
- top: rectPos.y,
- bottom: rectPos.y + rectSize.y
- };
- return !(r2.left > r1.right ||
- r2.right < r1.left ||
- r2.top > r1.bottom ||
- r2.bottom < r1.top);
- }
- isOnOpenTile(pos: Vector, size: Vector) {
- let openTiles = this.getTiles('open');
- let result = false;
- openTiles.forEach(element => {
- if( this.circleRect(pos,size,element.pos,element.size) ){
- result = element;
- return;
- }
- });
- return result;
- }
- circleRectGetCollisionNormal(c, r) {
- if(c.pos.y <= r.pos.y - (r.size.y/2)) return new Vector(0,-1);
- //Hit was from below the brick
- if(c.pos.y >= r.pos.y + (r.size.y/2)) return new Vector(0,1);
- //Hit was from above the brick
- if(c.pos.x < r.pos.x) return new Vector(1,0);
- //Hit was on left
- if(c.pos.x > r.pos.x) return new Vector(-1,0);
- //Hit was on right
- return false;
- }
- getNewMoveVector(moveVector, normalVector) {
- normalVector = this.normalize(normalVector);
- let dot = (moveVector.x * moveVector.y) + (normalVector.x * normalVector.y);
- let dotProduct = new Vector(dot, dot);
- return moveVector.sub(dotProduct.mult(normalVector).mult(new Vector(2,2)));
- }
- normalize(v) {
- let length = Math.sqrt((v.x*v.x) + (v.y*v.y));
- return new Vector(v.x/length,v.y/length);
- }
- getMoveVectorOnCollision(circle) {
- let coll = this.isOnOpenTile( circle.pos, circle.size );
- if( coll != false) {
- let vector = this.circleRectGetCollisionNormal(circle, coll);
- return this.getNewMoveVector(circle.moveVector, vector);
- } else return false;
- }
- sub(vector: Vector) {
- return new Vector(this.x - vector.x, this.y - vector.y);
- }
Add Comment
Please, Sign In to add comment