#๐Ÿ”’ Coding a projectile

28 messages ยท Page 1 of 1 (latest)

vagrant scaffold
#

Hello friends,

I'm trying to code a projectile. For the last HOUR, I can't make this test to pass using formulas...

Here is the logic of the physics:

    def update_physics(self, delta_t:int) -> None:
        """Updates the physics of the ball.

        Args:
            delta_t (int): time since last call, in milliseconds.
        """
        # Speed update:
        self.speed += self.acceleration
        self.speed += self.gravity * delta_t

        # Position update.
        self.position += self.speed * delta_t
        if self.position.z < 0:
            self.position.z = 0
            self.speed.z = 0

        # Acceleration update:
        self.acceleration = Vec3(0,0,0)

And here is the test failing:

def test_speed_z_hauteur():
    test = Ball_m()  # Class for converting g to a m/s^2 instead of m/ms^2
    speed = 30
    time = 5
    gravity = 9.80665

    displacement = speed * time - (0.5 * gravity * time**2)  # Use a formula
    test.set_speed(Vec3(0,0, speed))
    test.update_physics(time)
    assert test.position.z == displacement, f'{test.position.z} == {displacement}'
ionic novaBOT
#

@vagrant scaffold

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vagrant scaffold
#

I have no clue why this test fails. I'm applying everything I know about physics correctly.

#

On this case, only the gravity is in effect. No initial acceleration.

#

According to the formula of projectiles, I should have a position of 27.42

#

My value is 0 somehow.

#

Ah I think I understood the issue.

latent jolt
#

Shouldn't this line:

        self.speed += self.acceleration

also involve delta_t ?

vagrant scaffold
#

I want the acceleration to be applied once for now.

#

Anyway the self.acceleration is 0.

latent jolt
#

Still, shouldn't it scale by time?

vagrant scaffold
#

Let's remove that for now.

latent jolt
#

Ir is it just an initial speed, in which case I'd personally use a different name,

latent jolt
vagrant scaffold
#

It wasn,t the issue.

#

I spoke too fast.

#

But htere is something wrong with my approach.

#

If I print the speeds before the update of positions

#

The speed is already negative.

#

So it never goes up, somehow, even if the initial speed is greater than the acceleration per milliseconds.

#

Tried to fix this by looping the milliseconds, still way far from the expected value.

#
    def update_physics(self, delta_t:int) -> None:
        """Updates the physics of the ball.

        Args:
            delta_t (int): time since last call, in milliseconds.
        """
        # Speed update:
        self.speed += self.gravity * delta_t

        # Position update.
        self.position += self.speed * delta_t
        if self.position.z < 0:
            self.position.z = 0
            self.speed.z = 0

here is the faulty code.

#

Either my approach nis very wrong and I should use the formulas, or I have a hidden bug.

#

The more I think about it, the more my approach is busted.

#

I'll think about it more.

#

!close

ionic novaBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.