1

I already did a jumping system which is working, but i have to do one using mecanics. At the moment my character never come back to the ground. Here is my code :

def jump(self):
        if self.isJumping == False:
            self.isJumping = True

            self.velx = math.cos(self.alpha) * self.v0
            self.vely = math.sin(self.alpha) * self.v0

        else:  
            self.time += 0.01               
            distX =  self.speed * self.velx * self.time
            distY =  self.speed * self.vely * self.time - (((self.time ** 2) * self.gravity) / 2)

            self.rect.x = round(distX + self.rect.x)
            self.rect.y = round(self.rect.y + distY)

And there is a part of the init of my player class:

        self.isJumping  = False
        self.v0 = 1
        self.alpha = 5
        self.time = 0
        self.velx = 0
        self.vely = 0
        self.gravity = 1

Thanks for helping !

Robert
  • 11
  • 1

1 Answers1

0

The player never comes back on the ground because the expression

distY =  self.speed * self.vely * self.time - (((self.time ** 2) * self.gravity) / 2)

is a quadratic function of the form "y = b*x - a * x^2". - a * x^2 goes to -infinity over time. Since the top left of the PyGame coordinate system is (0, 0), the player always goes higher and higher.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174