i working on 2d side scroller , have implemented techniques use in this article grapple hook, , works well. problem want player able swing around rope little bit gain bit of momentum, can't stop player moving way 90 degrees either side. techniques can applied force limit?
i have tried using separate player speed swinging slows process down can still swing 90 deg each side.
here's update function in player
public void update(float dt){ //handle friction , air resistance if(dx !=0){ if(touchingground) { // apply friction if (dx > 0) { dx -= retardation; } else { dx += retardation; } } else { //applied air resistance if (dx > 0) { dx -= airresistance; } else { dx += airresistance; } } } // handle gravity dy -= constants.gravity * dt; if(dy < -terminalvelocity){ dy = -terminalvelocity; } /* handle player movement */ if(right){ if(dx <= maxspeed){ dx += acceleration; } dx = maxspeed; } if(left){ if(dx <= -maxspeed){ dx -= acceleration; } dx = -maxspeed; } if(isgrappling){ //if collide need stop grappling if(hascollided){ isgrappling = false; } else { // algorithm here: // http://gamedev.stackexchange.com/questions/61596/player-rope-swing float currentd = (float) math.sqrt(((grapplex - x) * (grapplex - x)) + ((grappley - y) * (grappley - y))); float prevx = getx(), prevy = gety(); if (currentd > grappleradius) { vector2 hookpos = new vector2(grapplex, grappley); vector2 testpos = (new vector2(x, y).sub(hookpos)).nor(); y = (hookpos.y + testpos.y * grappleradius); x = (hookpos.x + testpos.x * grappleradius); // s = d / t dx += (x - prevx) / dt; dy += (y - prevy) / dt; } } } /* collision detection, handle last always! */ float oldx = getx(), oldy = gety(); boolean collisionx = false, collisiony = false; // move on x x += dx * dt; // calculate increment step in #collidesleft() , #collidesright() increment = collisionlayer.gettilewidth(); increment = getwidth() < increment ? getwidth() / 2 : increment / 2; if(dx < 0) // going left collisionx = collidesleft(); else if(dx > 0) // going right collisionx = collidesright(); // react x collision if(collisionx) { setx(oldx); dx = 0; } // move on y y += dy * dt; // calculate increment step in #collidesbottom() , #collidestop() increment = collisionlayer.gettileheight(); increment = getheight() < increment ? getheight() / 2 : increment / 2; if(dy < 0) { touchingground = collisiony = collidesbottom(); // can jump 2 times before have touch floor again if(collisiony){ numberofjumps = 2; } } else if(dy > 0) { collisiony = collidestop(); } // react y collision if(collisiony) { sety(oldy); dy = 0; } hascollided = collisionx || collisiony; }
as not using physics engine chose emulate physics limiting angle @ player can apply force swing.
// check if angle permits movement if(grappleangle < math.pi/9 && grappleangle > -math.pi/9) { // handle momentum gaining on rope if (right) { dx += swingacceleration * dt; } if (left) { dx -= swingacceleration * dt; } }
Comments
Post a Comment