I am trying to animate 2D mechanics problem (ball going into a vertical loop)
the equation of motion in my case is
v=sqrt{v_i^2+2gR(cos theta - cos theta_i)} and theta increases by value v* dt/R
I want theta(value tracker) to be used to calculate the velocity at this instant then a new theta should be calculated.
Here is my code
class vcircle(Scene):
def construct(self):
theta0=0
theta=ValueTracker(theta0)
R=2
g=9.8
v1=1
track=Circle(radius=R)
ball=Circle(radius=0.1,color=BLUE,fill_opacity=1)
ball.set_y(-2)
#ball_ref=ball.copy()
ball.rotate(theta.get_value(), about_point=ORIGIN)
rod=Line(ORIGIN,ball.get_center(),color=YELLOW)
self.bring_to_back(rod)
rod_ref=Line(ORIGIN,DOWN)
self.add(track,ball,rod)
self.wait()
def v(th):
term = v1**2 + 2 * g * R * (np.cos(th) - np.cos(theta0))
return np.sqrt(term) if term >= 0 else -np.sqrt(-term)
theta.add_updater(lambda obj, dt: obj.increment_value(v(theta.get_value()*dt/R)))
ball.add_updater(lambda ball, dt: ball.rotate(v(theta.get_value())*dt/R ,about_point=ORIGIN))
rod.add_updater(lambda rod: rod.become(Line(ORIGIN,ball.get_center(),color=YELLOW)))
self.wait(10)
The upper code didn't work: the circle kept moving on the circle but with a constant speed.
I tried to define theta from the angles between lines (rod)
#another way to define theta (didn't work too)
def update_th(self)
theta= rod.get_angle() -rod_ref.get_angle()
return theta
theta.add_updater(update_th)
and yet no change
I tried another approach which seemed promising,
ball.add_updater(lambda ball, dt: ball.rotate(v(rod_ref.get_angle()-rod.get_angle())*dt/R ,about_point=ORIGIN))
I removed the dependency on theta (value tracker) and used the difference between two rods angles. Velocity does change and decrease. However, the ball doesn't go backwards even though velocity is negative.