witch3r
witch3r

Reputation: 87

Show how a projectile (turtle) travels over time

I am new to Python, and currently having a rough time with turtle graphics. This is what I am trying to solve

On Turtellini (the planet where Python turtles live) the transportation system propels turtles with a giant slingshot. A particular turtle's original location (x0, y0) is (-180, -100). He is then shot upward at an initial vertical velocity (vy) of 88 units per second and a horizontal velocity (vx) of 20 units per second to the right. He travels for 16 seconds. The acceleration due to gravity (g) is 11 units per second squared. The the location of the turtle at a given second (t) is calculated as follows: x = x0 + vx * t and y = y0 + vy * t - g/2 * t2 . This program is to show how a turtle travels over this period of time.

The output should be like this:

Output Image

Here is what I should do;

My code so far:

import turtle

def main():
    wn = turtle.Screen()
    turtellini = turtle.Turtle()
    t = int(input("Blab blab blab: "))
    x0 = -180
    y0 = -100
    vx = 20
    vy = 88
    g = 11
    x = (float(x0 + vx * t))
    y = (float(y0 + vy * t - g / 2 * t**2))
    turtellini.color("black")
    turtellini.shape("turtle")
    turtellini.up()
    turtellini.goto(-180,-100)
    turtellini.down()
    for i in range(1,16,1):
        turtellini.stamp()
        turtellini.forward(i)
        turtellini.right(i)
    print(x)
    print(y)
if __name__ == "__main__":
    main()

I know I am doing bad; but can anyone help me to solve this problem?

Upvotes: 0

Views: 2969

Answers (1)

cdlane
cdlane

Reputation: 41872

You seem to have most of the parts and pieces. The biggest issue I see is you didn't put your x,y calculation in the loop. The loop iteration variable i is really t in your motion equations. Each time you calculate a new x,y you simply move the turtle to that position:

import turtle
from math import pi, atan

x0, y0 = -180, -100  # initial location

vx, vy = 20.0, 88.0  # initial velocity in units per second

travel_time = 16  # seconds

g = 11.0  # acceleration due to gravity in units per second squared

turtellini = turtle.Turtle(shape='turtle', visible=False)

turtellini.penup()
turtellini.radians()  # to make turtle compatible with math.atan()
turtellini.setheading(pi / 2)  # straight up
turtellini.goto(x0, y0)
turtellini.pendown()
turtellini.showturtle()
turtellini.stamp()

for t in range(1, travel_time + 1):

    x = x0 + vx * t
    y = y0 + vy * t - g / 2 * t**2

    turtellini.goto(x, y)

    print(x, y)

    angle = atan((vy * t - g * t**2) / (vx * t))  # a guess!
    turtellini.setheading(angle)

    turtellini.stamp()

turtle.exitonclick()

Unlike the gold standard image, I assumed the turtle was aerodynamic like a bullet and travelled head first through the flight. I don't know, and couldn't quickly find, the formula for the flight angle of a projectile so I guessed from the existing formulas:

enter image description here

Upvotes: 1

Related Questions