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