smoother movement

This commit is contained in:
rsninja722 2020-04-18 13:07:49 -04:00
parent a472506f56
commit 7a71387b42
2 changed files with 25 additions and 17 deletions

View File

@ -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);
var player = new Player(250,200);

View File

@ -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;