Original Post
/edit:
I actually figured out why I am getting wonky behavior just after posting this. God, I love print statements. Apparently there's an issue with the way I am instantiating tile positions, however, I can't seem to find said issue in my modules. Looks like I need to do s'more digging.
/end edit
Hello!
I am currently working on a side-scroller game in Python/Pygame and had a few questions.
I have a very basic physics system set up for levels. I know there are many routes to take when figuring out a collision algorithm in a tile-based map, however, I can't seem to find one that is efficient, elegant, and actually works right.
Here's the player's update function.
Keep in mind this function is not finished yet. (Also, the code block formatting on this forum is messed up; in my module the scopes are normal)
Now, in each level, I have a 2D list of internal Tile objects, each containing a 'collision' flag.
I am trying to figure out vertical collision when falling at this moment.
The player's transformed position in the level (the given p) is converted to a tile in the first two lines of this update method. (Edge cases not taken into consideration yet, but not too worried about that at this point).
In the commented out lines are where I have tried a couple different things. I tried just setting the velocity.y of the player to 0. That changed the behavior, but did not work so well. The current solution you see here does not work at all.
Should I be making some sort of collision map out of level.grid, instead of using flags like this? What am I doing wrong here (or just not doing)?
Any help is appreciated, thanks
-Adam
I actually figured out why I am getting wonky behavior just after posting this. God, I love print statements. Apparently there's an issue with the way I am instantiating tile positions, however, I can't seem to find said issue in my modules. Looks like I need to do s'more digging.
/end edit
Hello!
I am currently working on a side-scroller game in Python/Pygame and had a few questions.
I have a very basic physics system set up for levels. I know there are many routes to take when figuring out a collision algorithm in a tile-based map, however, I can't seem to find one that is efficient, elegant, and actually works right.
Here's the player's update function.
class Player(object):
height = 48
def update(self, level, p):
if self.velocity.x != 0 and self.velocity.y != 0:
self.current_tile = level.grid[int((p.x + Tile.width / 2) / Tile.width)][int(p.y / Tile.height)]
self.pos.x += self.velocity.x
self.pos.y += self.velocity.y
self.velocity.y += level.gravity
self.velocity.x *= 0.9
if abs(self.velocity.x) < 0.01:
self.velocity.x = 0
# if self.current_tile != None and self.current_tile.collides:
# ground_level = self.current_tile.pos.y
# else:
# ground_level = 500
ground_level = 500
if self.pos.y > ground_level:
self.pos.y = ground_level
self.velocity.y = 0Keep in mind this function is not finished yet. (Also, the code block formatting on this forum is messed up; in my module the scopes are normal)
Now, in each level, I have a 2D list of internal Tile objects, each containing a 'collision' flag.
I am trying to figure out vertical collision when falling at this moment.
The player's transformed position in the level (the given p) is converted to a tile in the first two lines of this update method. (Edge cases not taken into consideration yet, but not too worried about that at this point).
In the commented out lines are where I have tried a couple different things. I tried just setting the velocity.y of the player to 0. That changed the behavior, but did not work so well. The current solution you see here does not work at all.
Should I be making some sort of collision map out of level.grid, instead of using flags like this? What am I doing wrong here (or just not doing)?
Any help is appreciated, thanks
-Adam