Here is a small diversion from MockTurtle. The following code has a
bug somewhere that causes the plotted body under gravity to come down
faster than it goes up. Notice in the attached snapshot that the
spheres on the left (where the turtle ended) are bigger than those on
the right (where the turtle started). The spheres are drawn with
their diameters between successive turtle positions. It's close but
no cigar. What's wrong?
I'll find it eventually, but this seems like something that others
might find interesting. Let me know what you think.
"recursive script to draw the path of a cannon shot under gravity"
"a couple of constants"
gravity := -9.8.
dt := 0.1. "This should be based on TeaTime!"
"make a copy of the turtles current position and heading"
trans := localTransform copy.
trans1st := trans translation. "get just the position vector"
"move forward on the current heading by velocity * time"
trans3 := Matrix4x4 identity.
trans3 translationX: 0.0 y: 0.0 z: (aVelocity*dt) negated.
trans := trans composeWith: trans3.
trans2nd := trans translation. "get just the position vector"
"move down (-y) by gravity acceleration * delta time squared"
trans translation: ( trans2nd + (Vector3 x:0 y:(gravity*dt*dt) z:0) ).
trans3rd := trans translation. "get just the position vector"
"partly normalize the two moved-to positions to the origin and find
the angle between them"
trans2nd := trans2nd - trans1st.
trans3rd := trans3rd - trans1st.
trans2 := trans2nd rotationTo: trans3rd.
"rotate the potential new turtle position and heading by that angle"
trans2 := trans2 composeWith: trans.
"if the potential new turtle position is above y=0 then move to that
position and heading"
(trans2 translation y > 0) ifTrue: [
localTransform := trans2.
tracer ifNotNil: [ tracer addPoint: self globalPosition. ].
"find the new velocity based on the distance just moved and recurse"
newVelocity := (trans3rd length) / dt.
(self future: 100) cannon: newVelocity.