Looking at the code, it would seem you’re still using centred collision. All you’ve really done is replace two separate constants with a single constant.
The player drawing is accounting for the camera properly now though, which is why your camera is closer to moving as it’s supposed to.
That is indeed a problem when dealing with collisions, however it won’t be with the way I opted to do collisions.
The ‘diamond’ technique does actually avoid the problem and did play a part in my decision to use it, though the fact it’s much simpler than a lot of the other techniques was also a big reason for preferring it.
(Technically it’s not actually a diamond-shape, it’s just that the four points chosen happen to form a diamond. You could also think of it as forming a cross shape, but really it’s only 4 points, so I suppose calling it ‘edge midpoint collision’ is more accurate.)
Anyway, the reason that technique doesn’t have the corner issue is that the sprite’s corners aren’t actually factored into the collision at all. It’s taking the midpoint of each edge and using that, hence why the player slides off a block as soon as the middle of the base edge of the sprite is off the block: because that’s the point that’s being used to check for downward collisions.
The resulting effect is a bit wonky, but it’s simple and does the job and generally avoids oddities.
There are ways it could be improved, but I’d strongly advise that you make sure you actually understand what the collision code is doing before attempting anything more complex.
Read through the original commented version, bearing in mind that:
-
entity.x
and entity.y
refer to the centre of the entity, not the top left
-
tileWidth
and tileHeight
are also the width and height of all entities
Work it through manually on paper with some test cases, doing all the conditions and calculations manually. (You can use a calculator, but the point is that you actually look at how the calculations behave and understand what they’re doing.)
If you’re struggling to see what it’s doing I’ll try to find the time to talk you through it, but it’s best understood with some actual drawings/diagrams.
In the original platformer I was actually ‘cheating’ with the movement: there was no friction, the player’s horizontal velocity was only affected by the player’s input, and the vertical input was only affected by gravity and jumping.
What happens when two things collide in the real world is that some of the energy is absorbed, with the amount depending on how much mass the objects are and how ‘elastic’ the objects are, and the rest is retained, which is why if you throw a ball at a wall it bounces back in the opposite direction. (It also usually occurs via acceleration rather than directly affecting velocity.)
But platformers (especially 2D ones) usually ignore a lot of that because it’s quite complicated and doesn’t necessarily achieve good gameplay.
Without getting too hung up on accuracy, the velocity should theoretically be diminishing or even completely cancelling out when a collision happens. If it didn’t, the force of gravity would just keep increasing the player’s velocity to ridiculous levels and then the player would never be able to jump properly.
It depends how you want the camera to behave.
If you’re aiming for what I did in my platformer, (clamping the camera so that it doesn’t look outside the level,), the simplest thing to do is to not give the camera its own velocity in the first place, but to instead make it always look at the player, and then after that check to see if it’s looking off-screen, and if it is looking off-screen, to adjust it slightly so that it’s no longer looking off-screen.
Again, this is the sort of thing that you’re better off working out on paper. Ideally the kind of gridded paper you probably use in maths for drawing graphs since you can pretend the squares are pixels or tiles and the rules you figure out will then apply to the real tiles/pixels.
Again, I’m happy to go into further detail if you want, but I think having a go at working it out on paper first will help you later on. The more you understand about what’s going on and how the different parts work in relation to each other, the easier it will be to solve future problems. If you end up with a load of code and you don’t really understand what it’s doing, you’ll struggle to fix it and/or add to it later down the line.