I am trying to create a 2-player velocity test, where each surface has a different speed, but when i added bounding box (which I have used before and worked fine) it collides only with the top left corner of the player, rather than how it should, and I cannot work out what is wrong.
I have attached a LOVE file.
Thanks for any help.
P.S. The bounding box was copied directly from the LOVE wiki, and i did not type it up myself.
Bounding Box not working
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- StormtrooperCat
- Prole
- Posts: 11
- Joined: Fri Mar 09, 2018 12:29 am
- Contact:
Bounding Box not working
- Attachments
-
- Duck.love
- (4.37 KiB) Downloaded 173 times
Re: Bounding Box not working
The function checkTile scans tiles until it finds one that the player is overlapping. But when the player is overlapping several tiles, the first one that is detected is the one returned by checkTile(). Consider this situation:
In that case, tile 1 will be returned by checkTile, because it's the first in left-to-right, up-to-down order that the player is overlapping, even though the player is penetrating a wall (tiles 2 and 4). Therefore, the wall won't be detected until the left side of the player is fully inside it, because checkTile won't return wall until the first tile the player is overlapping is a wall.
On a side note, your method of detecting overlap is very slow. With some arithmetic you should be able to detect the same thing without having to loop over every tile.
In that case, tile 1 will be returned by checkTile, because it's the first in left-to-right, up-to-down order that the player is overlapping, even though the player is penetrating a wall (tiles 2 and 4). Therefore, the wall won't be detected until the left side of the player is fully inside it, because checkTile won't return wall until the first tile the player is overlapping is a wall.
On a side note, your method of detecting overlap is very slow. With some arithmetic you should be able to detect the same thing without having to loop over every tile.
- StormtrooperCat
- Prole
- Posts: 11
- Joined: Fri Mar 09, 2018 12:29 am
- Contact:
Re: Bounding Box not working
Thank you very much for your reply. I will try and implement that soon.
What suggestions do you have to cut down the collision time?
What suggestions do you have to cut down the collision time?
Re: Bounding Box not working
Well, if the bounding box of your player has its top left corner at x and y, and a width and height of w and h, you can determine the tile where each corner is, as follows:
Then with e.g. grid[top_left_tile_x][top_left_tile_y] you have the tile under the top left corner of the player, and so on.
For terrain, I'd say to check all four tiles and calculate the slowest of them (using math.max or math.min as appropriate).
For walls, if your player is going right, you need to check one of the right corners; if it's going up, one of the top corners, and so on.
edited to fix typo: hace -> have
Code: Select all
local top_left_tile_x = math.floor(x / tile_size)
local top_left_tile_y = math.floor(y / tile_size)
local bottom_right_tile_x = math.floor((x + w) / tile_size)
local bottom_right_tile_y = math.floor((y + h) / tile_size)
local top_right_tile_x = bottom_right_tile_x
local top_right_tile_y = top_left_tile_y
local bottom_left_tile_x = top_left_tile_x
local bottom_left_tile_y = bottom_right_tile_y
For terrain, I'd say to check all four tiles and calculate the slowest of them (using math.max or math.min as appropriate).
For walls, if your player is going right, you need to check one of the right corners; if it's going up, one of the top corners, and so on.
edited to fix typo: hace -> have
Last edited by pgimeno on Sun Nov 11, 2018 1:14 am, edited 1 time in total.
- StormtrooperCat
- Prole
- Posts: 11
- Joined: Fri Mar 09, 2018 12:29 am
- Contact:
Re: Bounding Box not working
Thank you very much (again) I implemented your ideas, and it was actually a very easy fix for the collision (i just put the grid type inside the function). I also used your method to cut down the amount it has to check.
Thanks!
Thanks!
- Attachments
-
- duck.love
- (4.52 KiB) Downloaded 183 times
Re: Bounding Box not working
It seems to work pretty well, nice work!
Who is online
Users browsing this forum: Google [Bot] and 2 guests