From 7a71387b4279ade1d4c8c0ec73a82d0e4ab40b47 Mon Sep 17 00:00:00 2001 From: rsninja722 Date: Sat, 18 Apr 2020 13:07:49 -0400 Subject: [PATCH] smoother movement --- docs/assets/js/player/player.js | 38 ++++++++++++++++++++------------- docs/assets/js/world/level.js | 4 ++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/docs/assets/js/player/player.js b/docs/assets/js/player/player.js index ea54969..f953331 100644 --- a/docs/assets/js/player/player.js +++ b/docs/assets/js/player/player.js @@ -33,7 +33,6 @@ Player.prototype.getLockedLeg = function(){ // leg has been selected, move leg towards mouse Player.prototype.moveLeg = function(){ - // Stops if we shouldn't move leg if(!this.shouldMoveLeg){ return 0; @@ -45,27 +44,30 @@ Player.prototype.moveLeg = function(){ // move selected leg towards mouse - // var angle = turn(pointTo(curLeg,{x:curLeg.x2,y:curLeg.y2}),pointTo(curLeg,target),0.1); - var angle = pointTo(curLeg,target); - curLeg.x2 = curLeg.x + curLeg.len * Math.cos(angle); - curLeg.y2 = curLeg.y + curLeg.len * Math.sin(angle); + // console.log(curLeg.angle.toPrecision(5),pointTo(curLeg,target).toPrecision(5)); + curLeg.angle = turn( curLeg.angle,pointTo(curLeg,target),0.1); + // var angle = pointTo(curLeg,target); + curLeg.x2 = curLeg.x + curLeg.len * Math.cos(curLeg.angle); + curLeg.y2 = curLeg.y + curLeg.len * Math.sin(curLeg.angle); + + // curLeg.angle = pointTo(curLeg,{x:curLeg.x2,y:curLeg.y2}); if(dist(curLeg,target) > curLeg.len) { // move towards mouse - this.x += Math.cos(angle) * dist(curLeg,target)/50; - - this.y += Math.sin(angle) * dist(curLeg,target)/50; + this.x += Math.cos(pointTo(curLeg,target)) * clamp(dist(curLeg,target)/50,1,3); + + this.y += Math.sin(pointTo(curLeg,target)) * clamp(dist(curLeg,target)/50,1,3); this.updateHips(); } // if leg is right update it accordingly if(this.legSelected === "R") { // set angle to the locked foot to the locked hip oppLeg = this.getLockedLeg(); - angle = pointTo({x:oppLeg.x2,y:oppLeg.y2},this.hipRight); + oppLeg.angle = pointTo({x:oppLeg.x2,y:oppLeg.y2},this.hipRight); // snap body to a position where the hip is attached to the leg - this.x = (oppLeg.x2 + oppLeg.len * Math.cos(angle)) - 5; - this.y = (oppLeg.y2 + oppLeg.len * Math.sin(angle)) - 10; + this.x = (oppLeg.x2 + oppLeg.len * Math.cos(oppLeg.angle)) - 5; + this.y = (oppLeg.y2 + oppLeg.len * Math.sin(oppLeg.angle)) - 10; this.updateHips(); @@ -75,14 +77,18 @@ Player.prototype.moveLeg = function(){ curLeg.x = this.hipLeft.x; curLeg.y = this.hipLeft.y; + console.log(oppLeg.angle) + // if leg is left update it accordingly } else { + console.log(curLeg.angle) // set angle to the locked foot to the locked hip oppLeg = this.getLockedLeg(); - angle = pointTo({x:oppLeg.x2,y:oppLeg.y2},this.hipLeft); + oppLeg.angle = pointTo({x:oppLeg.x2,y:oppLeg.y2},this.hipLeft); + // snap body to a position where the hip is attached to the leg - this.x = (oppLeg.x2 + oppLeg.len * Math.cos(angle)) + 5; - this.y = (oppLeg.y2 + oppLeg.len * Math.sin(angle)) - 10; + this.x = (oppLeg.x2 + oppLeg.len * Math.cos(oppLeg.angle)) + 5; + this.y = (oppLeg.y2 + oppLeg.len * Math.sin(oppLeg.angle)) - 10; this.updateHips(); @@ -113,12 +119,14 @@ Player.prototype.update = function() { if(collidingWithWorld({x:curLeg.x2,y:curLeg.y2,w:4,h:4})||mousePress[0]){ if(this.legSelected === "R"){ this.legSelected = "L"; + this.leftLeg.angle += pi; } else { this.legSelected = "R"; + this.rightLeg.angle += pi; } } } -var player = new Player(300,200); \ No newline at end of file +var player = new Player(250,200); \ No newline at end of file diff --git a/docs/assets/js/world/level.js b/docs/assets/js/world/level.js index 6e2af18..ae5e364 100644 --- a/docs/assets/js/world/level.js +++ b/docs/assets/js/world/level.js @@ -24,9 +24,9 @@ function drawWorldBlocks() { } function collidingWithWorld(objectWithXYWH) { - for (let i = 0, l = collisionRects.length; i < l; i++) { + for (var i = 0; i < collisionRects.length; i++) { if (rectrect(collisionRects[i], objectWithXYWH)) { - return i; + return true; } } return false;