Reputation: 151
I'm trying to place borders in a game made in pygame using pymunk 5.7.0 to prevent the player from leaving the map. When I do this however, only some of the lines acting as borders have collision. This is the code that I use:
screen_size = (360, 360)
static_body = space.static_body
static_lines = [
# North West corner to South West corner
pymunk.Segment(static_body, (0, 0), (0, screen_size[1]), 0),
# NW - NE
pymunk.Segment(static_body, (0, 0), (screen_size[0], 0), 0),
# SW - SE
pymunk.Segment(static_body, (0, screen_size[1]), (screen_size[0], screen_size[1]), 0),
# NE - SE
pymunk.Segment(static_body, (screen_size[0], 0), (screen_size[0], screen_size[1]), 0),
]
for line in static_lines:
line.elasticity = 1
line.friction = 0
space.add(*static_lines)
The static lines connected to the north west corner have collision, but the ones connected to the south east don't. I've tried everything between subtracting 1 from the coordinates that use the screen size, changing the elasticity, friction, and radius, rearranging the order of the lines being created, and even just writing in the number itself. None of this worked. I also created another line as a test from NW to SE, and that had collision. What can I do to give all of the lines collision?
Upvotes: 1
Views: 331
Reputation: 151
I solved this myself. The problem is that the dimensions of the space are not the same as the dimensions of the screen. My screen size was (360, 360), but the dimensions of the space was (9, 9), as the map of my game was a 9x9 array. That is why the lines connected to the southeast didn't seem to have collision, while the lines connected to the northwest did. The southeast lines were simply too far away.
The functional code looks like this:
static_body = space.static_body
static_lines = [
pymunk.Segment(static_body, (0, 0), (0, current_map.height), 0),
pymunk.Segment(static_body, (0, current_map.height), (current_map.width, current_map.height), 0),
pymunk.Segment(static_body, (current_map.width, 0), (current_map.width, current_map.height), 0),
pymunk.Segment(static_body, (0, 0), (current_map.width, 0), 0),
]
for line in static_lines:
line.elasticity = 0
line.friction = 0
space.add(*static_lines)
current_map
is an instance of a class called Map
which has the attributes width
and height
, along with the layout. All of the map tiles are added to the space.
Upvotes: 1