简体   繁体   English

Java 2D Platformer引力

[英]Java 2D Platformer Gravity

I am currently making a 2D Platformer in Java. 我目前正在用Java开发2D Platformer。 I am a beginner so take it easy. 我是初学者,请放轻松。 I have a problem with the gravity in the game. 我在游戏中的重力问题。 I am using different variables for falling and jumping. 我对下降和跳跃使用了不同的变量。 I am using a tiled map. 我正在使用平铺的地图。 So lets get to the point. 因此,让我们说清楚。 My fall method works like this - 我的跌落方法是这样的-

if(collisionDown == false) {
    characterY += fall;
    fall ++;
{

fall is equal to 4. and If collisionDown is true it resets back to 4. fall等于4。如果collisionDown为true,则重置为4。

My jump method is almost the same: 我的跳转方法几乎相同:

if(key.E == true && collisionDown == true) {
   characterY -= jump;
   jump --;
}

jump is equal to 16. and If collisonDown is true it resets back to 16. jump等于16。如果collisonDown为true,它将重置为16。

Now problem is: imagine the character is jumping. 现在的问题是:想象角色正在跳跃。 its in the air and while going down characterY += fall; 它在空中,在下降的同时,Y + =下降; lets say characterY = 250 and fall is equal - 15 at that exact moment. 假设characterY = 250且跌落等于-在该确切时刻等于15。 The next solid tile below the character starts at Y position 255. character is at 250 and does NOT detect collision so next frame it adds 15 to characterY which is 250 + 15 = 265. At that point the character has "entered" the solid tile which was at positionY 255. 角色下方的下一个实体图块从Y位置255开始。角色在250处并且未检测到碰撞,因此下一帧将其添加到characterY 15,即250 + 15 =265。此时,角色已“进入”实体图块位置Y 255。

I have "fixed" that so the character gets back ON TOP of the solid tile (and that is visible and annoying.) That is not the perfect solution because it slows the character 1 frame each time it enters a solid tile(which is because it detects left and right collision and the character can't move). 我已经“修复”了,使角色重新回到实体图块的顶部(并且可见且令人讨厌。)这不是完美的解决方案,因为它会在每次进入实体图块时减慢角色一帧的速度(这是因为它检测到左右碰撞并且角色无法移动)。 The character visibly stutters if I can say it like that. 如果我可以这样说,这个角色显然会口吃。

I need a solution for that problem but can't think of any. 我需要一个解决方案,但是什么也想不出来。 So if you make a suggestion I would be happy. 因此,如果您提出建议,我会很高兴。 Thank you. 谢谢。

The way I usually handle this, is to pre-check movement: 我通常处理此问题的方法是预先检查运动:

private int moveDown(int fall){
        if (isSolidBlock(characterX, characterY + fall)){
            //Knowing the height of your block, calculate some kind of reduction. If your block height is 40, it's probably something like (characterY + fall)%40
            fall = 4;
            collisionDown = true;
            return maxFallUntilSolidBlockReached();
        }
        fall++;
        return fall;
    }

private boolean isSolidBlock(int x, int y){
        //Implement some kind of check if at (x,y) there's a solid block.
    }

Then just do this for the fall calculation: 然后,只需执行以下计算即可:

if(collisionDown == false) {
            characterY += moveDown(fall);
        }

I would probably use something like the following. 我可能会使用类似以下的内容。

if(collisionDown == false) {
    characterYnext = characterY + fall;          //Get Next Position
    if(NextMovementCollides()){                  //Basically if next position is too far. 
    characterYnext += difference_between(CharacterY,Ground);     //This should move the character to the ground state.
    fall = 0;                                                   //No longer falling so reset the value.
    }
    else{characterY += fall; fall++;}       //Otherwise continue falling like normal.
}

DISCLAIMER: I'm not a java programmer so my syntax might be a bit off. 免责声明:我不是Java程序员,所以我的语法可能有点偏离。

This should work, just plug in your game logic where it would make sense. 这应该可行,只要在有意义的地方插入游戏逻辑即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM