Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Collisions
-
Yes, it would. I’ll probably make that change. Thanks
-
I haven't tried you code yet, but I have recently been helping my son debug a similar issue on a javascript game he is trying to write.
What I suggested was to have a debugging mode, where you display a label on each wall block showing it's bbox. Likewise, attach a label to your player node,and display it's bbox. The color of the label for each wall should change depending on whether it interests the player bbox (and maybe a color to indicate whether it interests the top, bottom, left or right), or draw lines showing which edges are touching, etc)
One thing that you do need to watch out for is that, IIRC, bbox or frame is with respect to the parent node. So if your walls are all children to a ground node, but your player is child of the root scene, their coordinate systems might have an offset that you have to account for. Also, if you are using scale or other tranaforms, that can change what these return...
Is your character walking through all walls, or does it eventually stop, say after it is halfway or all the way inside the wall? There could be issues with where you have your anchor point set.
-
Actually, I think I see the issue:
for wall in self.wall_list: if new_x in wall.bbox: pass else: self.player1.position = (new_x, self.player1.position.y)
Suppose there are two walls. The player intersects wall 1, but not wall 2. Your code will pass when checking wall1, but the next loop it will not interest wall2, and thus sets the new position.
Thus, you need to check all walls first, and then if any intersect, disallow the move.
allow_move=True for wall in self.wall_list: if new_x in wall.bbox: allow_move=False break # short circuit once a hit is found if allow_move: self.player1.position = (new_x, self.player1.position.y)
There is probably an efficient way to do this using the
any
function and an iterator, which will do the proper short circuit logic, in a compact form.Something like (I haven't tried this, the syntax might not be quite right)
if not any(( w.bbox.intersects(newbbox) for w in self.walls) ): # move player
-
@timjhinton, also, if you have just four walls, would seem simpler to have a rectangle that defines the area inside the walls, and restrict player position to be within that rectangle (by making a ”tentative” change to the coordinates and using
Rect.contains_point
to check if it can be really applied). This has the additional benefit that the check code can be shared by all of the four buttons.(Thanks for a well-presented question!)
-
This post is deleted! -
@JonB Fantastic idea. I tried to implement your suggestion by putting in another method so i could run it through easy for each of the buttons like so:
def player_collision(self, new_xy): for wall in self.wall_list: if new_xy in wall.bbox: return False else: return True
Then in my update(self) section i put
if touch.location in self.left_button.bbox: new_x = self.player1.position.x - 3 if 50 <= new_x <= 1150: if self.player_collision(self.player1.position(new_x, self.player1.position.y)) == True: self.player1.position = (new_x, self.player1.position.y)
I thought that this should work but it doesn’t like it. It gives me a TypeError: ‘Point” object is not callable. I’m not exactly sure what that means, but it sounds like I need to use a bbox or frame instead of player1.position??
-
@mikael said:
@timjhinton, also, if you have just four walls, would seem simpler to have a rectangle that defines the area inside the walls, and restrict player position to be within that rectangle (by making a ”tentative” change to the coordinates and using
Rect.contains_point
to check if it can be really applied). This has the additional benefit that the check code can be shared by all of the four buttons.(Thanks for a well-presented question!)
Thanks!
I did set up the overall dimensions of the rectangle with only allowing the sprite to move in certain x and y limits. I have this sprite going through a small maze, so i have other walls present besides the outside 4 walls. This is where i am having the trouble. I dont want to have to hard limit every wall space on the screen. -
@JonB said:
Actually, I think I see the issue:
for wall in self.wall_list: if new_x in wall.bbox: pass else: self.player1.position = (new_x, self.player1.position.y)
Suppose there are two walls. The player intersects wall 1, but not wall 2. Your code will pass when checking wall1, but the next loop it will not interest wall2, and thus sets the new position.
Thus, you need to check all walls first, and then if any intersect, disallow the move.
allow_move=True for wall in self.wall_list: if new_x in wall.bbox: allow_move=False break # short circuit once a hit is found if allow_move: self.player1.position = (new_x, self.player1.position.y)
There is probably an efficient way to do this using the
any
function and an iterator, which will do the proper short circuit logic, in a compact form.Something like (I haven't tried this, the syntax might not be quite right)
if not any(( w.bbox.intersects(newbbox) for w in self.walls) ): # move player
Also tried to copy and paste your first suggestion, the part without using the any function, with no success. The sprite node is still walking through walls..do i have something fundamentally wrong with how I’m doing this?
-
Is
self.player1.position
aPoint
object? If so thenself.player1.position(x, y)
is going to raiseTypeError: ‘Point” object is not callable
-
def player_collision(self, new_xy): for wall in self.wall_list: if new_xy in wall.bbox: return True # new_xy is in a wall return False # new_xy is not in any wall # or as a one liner... return any(new_xy in wall.bbox for wall in self.wall_list)
-
@JonB said:
I haven't tried you code yet, but I have recently been helping my son debug a similar issue on a javascript game he is trying to write.
What I suggested was to have a debugging mode, where you display a label on each wall block showing it's bbox. Likewise, attach a label to your player node,and display it's bbox. The color of the label for each wall should change depending on whether it interests the player bbox (and maybe a color to indicate whether it interests the top, bottom, left or right), or draw lines showing which edges are touching, etc)
One thing that you do need to watch out for is that, IIRC, bbox or frame is with respect to the parent node. So if your walls are all children to a ground node, but your player is child of the root scene, their coordinate systems might have an offset that you have to account for. Also, if you are using scale or other tranaforms, that can change what these return...
Is your character walking through all walls, or does it eventually stop, say after it is halfway or all the way inside the wall? There could be issues with where you have your anchor point set.
I am now seeing this. Maybe I just wasn’t moving the sprite around enough to see it but there are definitely places where my left button wont work for large swathes of the screen and then it will start to work again once I get above a certain point. I have my sprite scaled down to 80%, but that can’t account for the position being off by 1/3 of the screen...how can i display the bbox’s? I’m not sure I know how to even do that...