Friday, July 15, 2011

Ballistics

When running experiments, I need to choose the initial properties of a ball in order to produce a desired end result. In this case, I know where the ball starts and I know where the ball ends. In a linear system, that would be enough. However, gravity makes the system quadratic. So we need an extra constraint. For my experiment, it was important that the ball height not exceed a certain limit. So I control the maximum height of the trajectory. Our givens are then $g$, $x_0$, $x_m$, and $x_f$. We want to calculate $t_f$ and $v$. We have the following general ballistic equation:
$$f(t)=\frac{1}{2}at^2+vt+x_0$$
What we need is the correct $v$. So we take our knowns and plug them through:
$$\begin{align}
x_m&=\frac{1}{2}at_m^2+vt_m+x_0\\
x_f&=\frac{1}{2}at_f^2+vt_f+x_0\\
0&=at_m+v
\end{align}$$
We can solve for $v=-at_m$ and plug that into the first equation to get $x_m=\frac{1}{2}at_m^2-at_m^2+x_0=-\frac{1}{2}at_m^2+x_0$; therefore, $t_m=\sqrt{\frac{-2(x_m-x_0)}{a}}$. In my case, a is negative (gravity) and so the function will be real-valued as long as $x_m>=x_0$. That's sensible since $x_m$ is the highest point in the trajectory.  Note that you can decide if $t_m$ if positive or negative.  If it's positive, the initial velocity will be going against the acceleration.  If it's negative, the initial velocity is with the direction of acceleration.
 Now we can find $t_f$ and $v$: $v=-at_m$, and $t_f = \frac{-v\pm\sqrt{v^2-2a(x_0-x_f)}}{a}$. Again, since $a$ is negative, using the subtraction branch selects the time farthest in the future and we should have a real-valued result as long as $x_f<=x_m$.
Given  $t_f$, which I assume has been calculated in terms of the dimension parallel to gravity, we can then calculate the velocities for the other dimensions.  This is done based on where you want the ball to be at time $t_f$: $v_{x0} = \frac{dx}{dt} = \frac{x_f - x_0}{t_f - t_0}$.  Note here, that this is assuming no acceleration in the dimension of interest.  Also note, if the trajectory involves bouncing off of a wall, you should "wrap" the bounce into the final endpoint so that you're accounting for total distance traveled in the dimension of interest.  This assumes no friction and elastic collisions.  If collisions are not perfectly elastic, you just need to boost the velocity according to the number of collisions expected before $t_f$.

Monday, July 11, 2011

Dynamical system simulation with ODE

After coding up a new joint-type in ODE, I started thinking that it would be interesting to have more force-limited constraints. With the current set of ODE constraints, you're either applying unlimited force to meet a constraint or zero force (as with a contact joint). The motors also provide an option for applying up-to a maximum force in order to achieve a certain desired velocity, but this is somewhat limited.

It would be interesting to implement a system that allows you to create potential-fields dynamic system within the ODE framework. Right now, you can set a global gravitational constant and then you can exclude selected bodies from feeling that.

My new joint makes it so that you can constrain a point relative to a body to be within a rectangular prism and you can also constrain that point to achieve a certain velocity using the limit-motors. Watching the simulation run made me think of simulating spaces inside of spaces. Normally, when a sphere collides with a box, you create a contact joint that forces it out. However, you could also create a contact joint that forces the sphere into the box whenever it tries to escape.



While we're doing that, why don't we set up a system that allows you to make gravity more flexible. Instead of applying a constant acceleration to everything affected by gravity, we could make the acceleration a function so that the acceleration accumulated depends on spatial relationships. We could also apply forces as a function of spatial relationships. It probably isn't a good idea, computationally, to try to implement a complex function that needs to be applied pairwise between all objects in the world. However, we can create spatial domains where the force/acceleration function is applied. So, when object A collides with object B, they instead of forming a contact joint, they might start applying a force to each other that pushes B into the center of A.