从网格中的一个方块跳到另一个方格

时间:2014-09-28 17:39:13

标签: java math vector libgdx game-physics

我正在编写一个游戏,你必须从一个浮木跳到另一个浮木。你可以看到我在这里有什么:

enter image description here

木板在顶部产卵并向下滚动。你可以向上或向右跳一个瓦片和一个或两个瓦片。

每次调用draw(float delta)时,所有元素(木板和玩家,如果他没有跳跃并站在木板上)都会移动:

setY(getY() + FlotsamGroup.yVelocity * delta);

这很好用,但是如果玩家开始跳跃,我希望他降落在这个位置的木板上完全相同的坐标上。使用我的代码,他没有,并且时间看起来像这样:

enter image description here

您可以看到播放器周围的绿线与周围木板的高度不同,但两侧是正确的,所以看起来x轴工作正常。

我想我不得不使用上面的速度代码的修改版本,但我也想让玩家在木板向下滚动时跳得更快,有一天我想要包括,整个游戏变得更快已成功跳过的木板数量(减少FlotsamGroup.yVelocity-Value

protected class JumpAction extends Action {

    // Direction the player wants to jump to
    Direction direction;
    // The player actor
    Player player;
    // scalarVector is the vector, which has to be added to players position
    Vector2 scalarVector;
    // targetPosition is the players position add with the scalarVector
    Vector2 targetPosition;

    public JumpAction(Direction direction) {
        this.direction = direction;
        Gdx.app.log("", "Go to " + direction);

        /* Everytime we need to jump one tile up, so Y from scalarVector is everytime 1
           How much we have to go on x-axis dependens on the direction */
        scalarVector = new Vector2(0, 1);
        switch(direction) {
        case TWOLEFT:
            scalarVector.x = -2;
            break;
        case LEFT:
            scalarVector.x = -1;
            break;
        case UP:
            break;
        case RIGTH:
            scalarVector.x = 1;
            break;
        case TWORIGTH:
            scalarVector.x = 2;
            break;
        }
    }

    @Override
    public boolean act(float delta) {
        if(player == null) {
            player = (Player) getActor();
            player.isJumping = true;
            targetPosition = new Vector2(player.getX() + GameGrid.getSquareSize() * scalarVector.x, player.getY() + GameGrid.getSquareSize() * scalarVector.y);
            Gdx.app.log("", "Target Vector " + scalarVector.toString() + " | Target Position " + targetPosition.toString());
        }

        // Somehow modify players position to smoothly move him to his target           
        player.setX(player.getX() + ( scalarVector.x / -(FlotsamGroup.yVelocity * delta)) );
        player.setY(player.getY() + ( scalarVector.y / -(FlotsamGroup.yVelocity * delta)) );

        // Check if player has been moved enough to stand on the new plank
        switch(direction) {
        case TWOLEFT:
        case LEFT:
            if(player.getX() < targetPosition.x)
                player.isJumping = false;
            break;
        case UP:
            if(player.getY() > targetPosition.y)
                player.isJumping = false;
            break;
        case RIGTH:
        case TWORIGTH:
            if(player.getX() > targetPosition.x)
                player.isJumping = false;
            break;
        }

        if(!player.isJumping) {
            // Player isn't jumping anymore, now we can set his position to target position
            player.setX(MathUtils.roundPositive(targetPosition.x));
            player.setY(MathUtils.roundPositive(targetPosition.y));
            // Return true to remove this action from the player-actor
            return true;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我会改变这个:

   // Somehow modify players position to smoothly move him to his target           
    player.setX(player.getX() + ( scalarVector.x / -(FlotsamGroup.yVelocity * delta)) );
    player.setY(player.getY() + ( scalarVector.y / -(FlotsamGroup.yVelocity * delta)) );

到此(C ++):

   // needed change in position
   float dx=targetPosition.getX()-player.getX();
   float dy=targetPosition.getY()-player.getY();
   // make it unit vector
   float q=sqrt((dx*dx)+(dy*dy));
   float JumpSpeed=???; // here add some constant or variable jump speed you can use value of q for accelleration/breaking decision...
   if (q<1e-3) q=0.0; //here handle player reached target...  
   else q=1.0/q;
   dx*=q; dy*=q;
   // update position
   player.setX(player.getX()+(dx*JumpSpeed*delta));
   player.setY(player.getY()+(dy*JumpSpeed*delta));
   // here add the flow speed position update...
  • targetPosition应该保持实际目标位置(因此也用流速修改......)
  • 它只是修改了玩家和目标位置之间的线性插值...