0

I have a simple projectile motion simulation built using Pygame. I limited the FPS to 60 and calculated the delta time for each frame. I then use this delta time to update the position of my projectile. However, the problem is, the update seems to be very slow.

Based on my paper computations for a given projectile trajectory, the projectile should complete its path in a little less than 5 seconds. However, much more time has elapsed before the projectile completes its path. Here is my code:

def update(self, dt):
    if not self.is_on_ground:
        self.time += dt # time is initialized to 0

        # calculate initial x and y velocities
        vx = self.vi * math.cos(self.angle)
        vy = self.vi * math.sin(self.angle)

        # kinematics
        self.dx = (vx * self.time)
        self.dy = (vy * self.time) + ((-9.8 * (self.time ** 2)) / 2)

        # update old positional values
        self.x = self.dx + self.init_x
        self.y = self.init_y - self.dy

        # update rectangle position
        self.rect.center = (self.x, self.y)

        # check if the ball has hit the ground
        self.check_collisions()

I would expect that the ball would hit the ground at the same time as the calculated time, however, it doesn't. I have ruled out the possibility that it is an FPS issue since printing dt always gives me a consistent 0.017 seconds (approximately 1/60). Am I missing something?

Here is a sample run:

t       x       y       vi      angle
0.5     687.51  751.21  21.93   1.15
t       x       y       vi      angle
1.0     692.02  744.88  21.93   1.15
t       x       y       vi      angle
1.51    696.63  740.95  21.93   1.15
t       x       y       vi      angle
2.02    701.14  739.59  21.93   1.15
t       x       y       vi      angle
2.53    705.78  740.77  21.93   1.15
t       x       y       vi      angle
3.04    710.38  744.5   21.93   1.15
t       x       y       vi      angle
3.55    715.0   750.83  21.93   1.15
t       x       y       vi      angle
4.07    719.62  759.75  21.93   1.15

Note - Lower y values means "physically" higher

Using the equations v_yf = v_yi + gt^2 and solving for t would give a time of approximately 2 seconds. Based on the results, the program takes twice as long to accomplish this.

EDIT: Added Logs

adrianp
  • 999
  • 1
  • 8
  • 13
  • Can you put in log instructions so that you can add a table of time and position for every 0.5 sec. As presented the code looks alright, the error has to be somewhere else, in the initialization or scaling of the computation. – Lutz Lehmann Jun 14 '18 at 11:05
  • @LutzL I've updated the post. – adrianp Jun 14 '18 at 11:31
  • 1
    The initial position is `x=683, y=760`? What is "ground level" for `y`? What are `v_yf, v_yi`, and if they are velocities, why is their equation not `v_yf = v_yi + g*t`? If you shoot from ground to ground so that the target level is also `y=760`, then I get the time as `4.085`. – Lutz Lehmann Jun 14 '18 at 12:02
  • I'll second what @LutzL said. Your code seems to work and results go according to [free fall calculations](https://www.angio.net/personal/climb/speed.html). Could you provide your initial values as well as your calculations? What is wrong is probably your estimate or your initial position/speed with regard to what you expect as trajectory. Also, in your log, you should display vx and vy which would be more usefull than vi and the initial angle. – Olivier Melançon Jun 14 '18 at 14:52

0 Answers0