2

I am trying to make a Tank Battle game like Pocket Tanks using Pygame and Pymunk in Python. I have a Dynamic body which is the Tank and a Static Body which is the ground. Everything works fine until after a while, the Tank move left on its own and sinks in to the Ground Static body and keeps bouncing up and down while "no-cliping" through the ground.

All my code can be found here: https://github.com/MysteryCoder456/PyTanks.io

Here's a video of what happens(Keep in mind that only press the 'D' key once after the Tank lands on the Ground): https://streamable.com/2fuzz

Edit: I'm using Python 3.7.6, Pygame 2.0.0dev6, MacOS 10.15.3. I don't know which version of Pymunk.

MysteryCoder456
  • 460
  • 5
  • 21

1 Answers1

1

One thing which can cause the behavior is to not put center of gravity (CoG ) in the actual center (or close to it). In your code the way both the ground and the tank poly shapes are built means that their CoG will be at the first corner of the shape, at (0,0). Try instead to build the poly vertices as:

 self.vertices = (
        (-width/2, -height/2),
        (width/2, - height/2),
        (width/2, height/2),
        (-width/2, height/2)
    )

It's similar to the problem here Pymunk/Chipmunk2d - dynamic bodies colliding with static bodies generating collsion impulses I will clarify this in the docs of pymunk, seems to be an easy mistake to make.

viblo
  • 4,159
  • 4
  • 20
  • 28