Well, after have been down-voted twice (without knowing why, cause' I tough my question was well made (AND I SEARCHED FOR ANSWERS BEFORE ASKING...))
I finally ended up with a partial answer, that, did nots really answered but at least fixed the problem !
In my code by doing that :
import pygame
import pymunk
import pymunk.pygame_util
class Player:
def __init__(self, space):
self.mass = 3
self.body = pymunk.Body(self.mass, pymunk.moment_for_box(self.mass, (50, 100)))
self.shape = pymunk.Poly.create_box(self.body, (50, 100))
self.shape.friction = 1
self.shape.elasticity = 0.0
space.add(self.body, self.shape)
self.direction = pygame.Vector2(0, 0)
def move(self, dir_vec):
self.direction = dir_vec
def update(self, dt):
if self.direction.length() < 0.2:
return
force = self.direction * 500 # À ajuster selon le poids de l'objet
self.body.apply_force_at_local_point(tuple(force))
pygame.init()
WIDTH, HEIGHT = 1920//2, 1080//2
window = pygame.display.set_mode((WIDTH, HEIGHT))
players = []
def draw(space, window, draw_options):
window.fill("white")
space.debug_draw(draw_options)
pygame.display.update()
def run(window, width, height):
run = True
clock = pygame.time.Clock()
FPS = 120
# Create a pymunk space
space = pymunk.Space()
space.damping = .5
space.iterations = 30
space.collision_slop = 0.1
space.collision_bias = 0.1
players.append(Player(space))
players.append(Player(space))
draw_options = pymunk.pygame_util.DrawOptions(window)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
keys = pygame.key.get_pressed()
dir_p1 = pygame.Vector2(0, 0)
dir_p2 = pygame.Vector2(0, 0)
if keys[pygame.K_z]:
dir_p1.y -= 1
if keys[pygame.K_q]:
dir_p1.x -= 1
if keys[pygame.K_s]:
dir_p1.y += 1
if keys[pygame.K_d]:
dir_p1.x += 1
if keys[pygame.K_UP]:
dir_p2.y -= 1
if keys[pygame.K_DOWN]:
dir_p2.y += 1
if keys[pygame.K_LEFT]:
dir_p2.x -=1
if keys[pygame.K_RIGHT]:
dir_p2.x += 1
players[0].move(dir_p1)
players[1].move(dir_p2)
substeps = 100
for _ in range(substeps):
for player in players:
player.update(1/FPS/substeps)
space.step(1/FPS/substeps)
draw(space, window, draw_options)
clock.tick(FPS)
pygame.quit()
return
if __name__ == "__main__":
run(window, WIDTH, HEIGHT)
Which mean by substeping (a lot) and adding inertia, well, it fixed everything.
I really think that the bug was comming from the player update method because it was moving to strogly and so the detections couldn't be calculated.
Thank's to all the people who juste tried to think about my question!