lillapojkenpåön wrote: ↑Sun Jul 12, 2020 1:18 amWell I saw comments in that example like "if this then you're probably stuck in a wall"
and the eject code in this example is pretty huge
I don't know what code you're using for reference, but I don't see any reason why ejecting would be any more complex than snapping.
also I don't get how you would know what way to eject and how many times reliably?
getting sucked into walls in mario comes to mind
Say that the player moved past a wall when going right:
Code: Select all
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
00000000000000001111111111111111
0123456789ABCDEF0123456789ABCDEF
+--------------+
| WALL |
| |
| |
| |
+-------M |
| | | |
| +---|----------+
| | | |
--> | | | |
| | | |
| | | |
| | | |
| | | |
------------+---+---N----------+
Points M and N represent the segment that must be checked for collisions in this case. If there are any solid blocks in that line, ejection is necessary. The X coordinate of these points is $14, but the edge of the wall is at $10. You can easily calculate that the difference is 4, and you can add 1 to that since you want the player to *touch* the edge of the wall, not overlap it, so 5 is the amount of pixels you need to push the player back.
The same is true when going left (or up):
Code: Select all
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
00000000000000001111111111111111
0123456789ABCDEF0123456789ABCDEF
+--------------+
| WALL |
| |
| |
| |
| M-------+
| | | |
+----------|---+ |
| | | |
| | | | <--
| | | |
| | | |
| | | |
| | | |
+----------N---+---+------------
The relevant edge of the player is at column $0B, but the edge of the wall is at column $0F. The difference between $0B and $0F is 4, and again you need to add 1 more so the edges are touching and not overlapping.
In actual code, these calculations are really simple (assuming a block size of 16x16 pixels):
Code: Select all
/* When going up: */ EjectY = 0x10 - (PointY & 0x0f)
/* When going down: */ EjectY = PointY & 0x01 + 1
/* When going left: */ EjectX = 0x10 - (PointX & 0x0f)
/* When going right: */ EjectX = PointX & 0x0f + 1
EjectX and EjectY are the amounts of pixels to eject, which you add/subtract to an object's coordinates according to the direction of the movement.
Snapping to the edge of the tile in that case is the intuitive way for me, same visual result, without all the invisible moving back and forth
Like I said before, there's very little difference between one way or the other. If one way feels more intuitive to you and it's working, then by all means stick with it.