TL;DR:
Stepping function:
$$
\text{Theta}_{n+1} = \theta_{n+1} = \theta_n + \dot{\theta_n}\Delta t
$$
$$ \text{Angular acceleration}_n = \alpha = \ddot{\theta}_n = - \frac{mg}{I} ( \frac{x}{2} \cos \theta_n - \frac{y}{2} \sin \theta_n ) $$
$$ \text{Angular velocity}_{n+1} = \omega = \dot{\theta}_{n+1} = \dot{\theta_n} + \ddot{\theta_n}\Delta t $$
Free body diagram:
We will start by considering a box, that has an arbitrary starting "angular velocity":
$$ \omega = \frac{d \theta}{dt} = \dot{\theta} = 0.1 $$
and an arbitrary "angle" that we will call theta.
$$ \theta = 0 $$
Please also note some constants (with my arbitrary values):
I = 100
,m = 1
,x = 2
,y = 2
:
Natural laws:
- Here's the equation for Force:
$$ \text{F} = ma $$
- Here's the above equation in relation to Torque (
I
= moment of inertia, $\alpha$ = angular acceleration):
$$ \tau = I\alpha $$
- $\ddot{\theta}$ - The second derivative of $\theta$ with respect to time "t" is equal to angular acceleration $\alpha$.
- $\dot{\theta}$ - The first derivative of $\theta$ with respect to t is equal to angular velocity $\omega$.
Solving the free body diagram:
d
is the distance perpendicular to the axis:
$$ d = \frac{x}{2} \cos \theta - \frac{y}{2} \sin \theta $$
If d > 0, then the box is tipping over.
Here is the equation for torque that takes into consideration the components of our box tipping problem:
$$ \tau_{net} = mgd = mg(\frac{x}{2} \cos \theta - \frac{y}{2} \sin \theta) $$
Torque equation with respect to our problem's components:
$$ \tau_{net} = mg(\frac{x}{2} \cos \theta - \frac{y}{2} \sin \theta) = -I \frac{d^2 \theta}{dt^2} $$
Note that the second derivative of theta is angular acceleration. We need this on its own, so we must re-work the above torque equation by dividing the left side by -I
:
$$ \alpha = - \frac{mg}{I} ( \frac{x}{2} \cos \theta - \frac{y}{2} \sin \theta ) = \frac{d^2 \theta}{dt^2} $$
We now have an equation for angular acceleration.
Theta and angular velocity:
Consider a graph that draws theta, angular velocity and angular acceleration one above each other:
- The second derivative is equal to the slope of the first derivative. This equation is known up front because we know theta up front. Thus here is the first step in our stepping function:
$$ \text{Angular acceleration}_n = \alpha = \ddot{\theta}_n = - \frac{mg}{I} ( \frac{x}{2} \cos \theta_n - \frac{y}{2} \sin \theta_n ) $$
- The first derivative is the slope of theta. It can be worked out by adding the second derivative to the first derivative. Note the additional multiplication of $\Delta t$:
$$ \text{Angular velocity}_{n+1} = \omega = \dot{\theta}_{n+1} = \dot{\theta_n} + \ddot{\theta_n}\Delta t $$
- The next theta on the next loop/step can be worked out by adding the first derivative
$$ \text{Theta}_{n+1} = \theta_{n+1} = \theta_n + \dot{\theta_n}\Delta t $$
Now continue this loop - Here is this stepping demonstrated in code:
To change the pivot, if theta < 0 and the elapsed time is > 0, then set theta to 0 and negate the first derivative.
if theta < 0 and elapsedTime > 0:
theta = 0
...
if theta == 0 and elapsedTime > 0:
firstDerivativeOfTheta = -firstDerivativeOfTheta
More coming soonAdding friction or resistance:
- Energy dampening due to "inelastic collision"
- Air resistance
Energy dampening due to "inelastic collision" or Air resistance (for example) can be added by simply negating the value with a constant.
$$ \text{...-b} $$
Thanks:
- Thank you to the person who helped me along the way. You know who you are... :)