0

I'm trying to use Turtle to make a ball bounce on the sides of a stadium-like surface. I have the right bouncing angles, but there's something wrong: sometimes, when the ball arrives against a rounded side, it doesn't directly bounce but it goes over the edges and continues for a while.

Here's the part of the program :

while nbrebonds>=0: #rebonds means bounce
    forward(1)

    if (xcor()<-150 and distance(-150,50)>100) or (xcor()>150 and distance(150,50)>100):
        trajectoire=heading() 
        normale = towards(0,0)                   #direction de la normale
        trajectoire = 2*normale-180-trajectoire
        print(trajectoire) #trajectoire du rebond
        setheading(trajectoire)
        forward(1)
        nbrebonds+=-1
        print(nbrebonds)


    if ycor()<-50 or ycor()>150:
        trajectoire=heading()
        trajectoire=360-trajectoire
        setheading(trajectoire)
        nbrebonds+=-1
        print(nbrebonds)            

1 Answers1

0

The way you compute the direction of normale is wrong: it should be the direction towards the center of the curve, not the (0, 0) point which has no special meaning in this context.

So, you could change your code to:

while nbrebonds >= 0: #rebonds means bounce
    forward(1)

    if (xcor() < -150 and distance(-150, 50) > 100) or (xcor() > 150 and distance(150,50) > 100):
        trajectoire = heading() 
        if xcor() < -150:
            normale = towards(-150, 50)  #direction de la normale : vers le centre de la courbe
        else:
            normale = towards(150, 50)
        trajectoire = 2*normale-180-trajectoire
        print(trajectoire)  # trajectoire du rebond
        setheading(trajectoire)
        forward(1)
        nbrebonds -= 1  # plutôt que += -1
        print(nbrebonds)


    if ycor() < -50 or ycor() > 150:
        trajectoire = heading()
        trajectoire = 360-trajectoire
        setheading(trajectoire)
        nbrebonds -= 1
        print(nbrebonds)
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • I would add that `if ycor() < -50 or ycor() > 150:` might really want to be `elif ycor() < -50 or ycor() > 150:` since both types of rebound shouldn't happen in the same update. – cdlane Jun 08 '18 at 15:58