0
\$\begingroup\$

I'm using a tile based collision, but when the game is lagging (the lag isn't the problem) the collision fails and the player falls through tiles.

This is the movement/collision detection code of my Player class:

gravity.y = gspeed;

speed.y+=gravity.y;

position.set(position.x + direction.x * speed.x * deltaSeconds, position.y + direction.y * speed.y * deltaSeconds);

for (int i = (int) Math.round(position.x / 32) - 2 * t; i < (int) Math.round(position.x / 32) + 3 * t; i++)
            {
                for (int j = (int) Math.round(position.y / 32); j < (int) Math.round((position.y + height + 64) / 32); j++)
                {
                    checkCollision(i, j, deltaSeconds);
                }
            }

public void checkCollision(int i, int j, float deltaSeconds)
    {
        bbox.setBounds((int) position.x, (int) position.y, (int) width, (int) height);
        Tile t = null;
        t = Map.getTile(i, j);
        if (t != null)
        {
            if (t.isSolid())
            {
                if (t.getTop().intersects(bbox))
                {
                    if (position.y + height < t.y * 32 + 32)
                    {
                        if (speed.y >= 0)
                        {
                            position.y = t.y * 32 - height;
                            speed.y = 0;
                            gravity.y = 0;
                            jumpState = 0;
                        }

                    }

                }
                if (t.getBottom().intersects(bbox))
                {
                    if (position.y < t.y * 32 + 32)
                    {
                        position.y = t.y * 32 + 32;
                        speed.y = 0;
                    }
                }
                else
                {
                    if (t.getLeft().intersects(bbox))
                    {
                        if (position.x + width > t.x * 32)
                        {
                            position.x = t.x * 32 - width;
                            speed.x = 0;
                        }
                    }
                    if (t.getRight().intersects(bbox))
                    {
                        if (position.x < t.x * 32 + 32)
                        {
                            position.x = t.x * 32 + 32;
                            speed.x = 0;
                        }
                    }
                }
            }

        }
    }

Is it possible to fix my code, if yes how? Or is it possible to tell if the game is lagging?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You have a variable called deltaSeconds, which I presume is declared something like: float deltaSeconds in your Player update code.

You can work out a threshold that deltaSeconds can cross, at which you can consider your game to be lagging. For example, if your game runs at 30fps normally, deltaSeconds should be roughly ~0.033f. If the fps degrades to 20fps, it will be 0.05f.

So you need to work out a good value for deltaSeconds to be greater than to be considered lag.

As to what you can do to fix lag - you could clamp how far an object can move in one frame, or you could clamp the deltaSeconds to your threshold.

I/others can elaborate more if needed.

EDIT (I can't comment yet): Even if you want unlimited fps, I'd imagine you still have a minimum desired rate. You don't want your game running at less than a certain fps, but you'd like it to run anywhere faster than that. That should be your lag threshold.

\$\endgroup\$
3
  • \$\begingroup\$ That's a really nice solution, but what about unlimited fps? \$\endgroup\$ Oct 17, 2013 at 9:44
  • \$\begingroup\$ (Oh whoops I forgot I can comment on my own posts...) @user1990950, set your threshold to you minimum. You could run some tests to find that minimum. \$\endgroup\$
    – ThorinII
    Oct 17, 2013 at 9:51
  • \$\begingroup\$ Ok I'll try that \$\endgroup\$ Oct 17, 2013 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .