If I know my current $v_x$ and $v_y$ and I supply a force and a direction, how do I update the velocities?

94 Views Asked by At

Known quantities: Initial position $x_i, y_i$, initial velocity components $V_{ix}$ and $V_{iy}$, and mass $m$.

I can apply a force $F$ pushing me towards arbitrary point $P$, and then I must update the velocities accordingly the next time unit. (This is all for a game AI).

My attempt:

If I choose some point $P$ with coordinates $x_f$ and $y_f$ then the angle $\theta$ formed is part of the expression $\displaystyle \cos(\theta) = \frac{\Delta x}{d} = \frac{x_f-x_i}{d}$ where $d = \sqrt{(x_f-x_i)^2 + (y_f-y_i)^2}$.

The force applied relates to $F = ma$ so I can solve for $a = \frac{F}{m}$.

Now somehow I have to update the velocity components. We have $\displaystyle \cos(\theta) = \frac{V_{fx}}{V_f}$ (velocity in the $x$ direction over velocity in the diagonal pointing to $P$). And in general we have $V_i + a = V_f$.

I'm a little bit lost for how to update the velocities, for example solving for $V_{fx}$.

If I set the cosine stuff equal to each other we get

$$\frac{\Delta x}{d} = \frac{V_{fx}}{V_f}$$

So that means $\displaystyle V_{fx} = \frac{V_f \cdot \Delta x}{d} = \frac{(V_i + a) \cdot \Delta x}{d}$

At this point I feel like I am getting way off track and I could use some help getting it right.

4

There are 4 best solutions below

0
On BEST ANSWER

At the risk of being downvoted and rebuffed as waffling, I believe it would be useful for the OP and anyone encountering this question as a result of a search on similar terms, to look at a quick summary on the simplest way to numerically simulate the effect of forces on a point-like object.

(Why point-like? So that we can ignore any internal structure.)

First thing to realize, is that in numerical simulations, time is discretized to short intervals. Often, the intervals are of fixed (game-time) duration, to simplify the math involved. The key thing, however, is to realize that each step does not correspond to one instance of simulated continuous time, but to an interval. In mathematical terms, we need to integrate the instantaneous effects over the interval, to calculate (approximate) the effects during that interval.

Let's assume that for each object, at the start of each time step, we know their location/position vector $\vec{x}$, their velocity vector $\vec{v}$, and the sum of all forces that apply to it for the duration of the time step (about to be simulated), $\vec{F}$. If you have more than one force, you simply sum their vector components together. Let's use $\tau$ to describe the duration of the time step.

The simulated object has momentum $\vec{p} = m \vec{v}$, where $m$ is the mass of the object. If the mass of the object and the force acting on the object stay constant during the time step, then according to Newton's second law of motion, $$m \vec{a} = \vec{F} \iff \vec{a} = \frac{1}{m} \vec{F}$$ where $\vec{a}$ is the acceleration during the time step. Noting that $$\vec{v}(t) = \int_{0}^{t} \vec{a}(\tau) d \tau = \vec{v}(0) + \frac{t}{m} \vec{F}$$ and that $$\vec{x}(t) = \int_{0}^{t} \vec{v}(\tau) d \tau = \vec{x}(0) + \vec{v}(0) t + \vec{F} \frac{t^2}{2 m}$$

In discrete time units, this means that $$\vec{x} \leftarrow \vec{x} + \vec{v} \tau + \vec{F} \frac{\tau^2}{2 m}$$ and $$\vec{v} \leftarrow \vec{v} + \frac{\tau}{m} \vec{F}$$

In pseudocode in 3D:

Timestep:
    Let  px, py, pz  be the location of the object
                     at the start of the timestep
    Let  vx, vy, vz  be the velocity of the object
                     at the start of the timestep
    Let  fx, fy, fz  be the sum of forces acting on
                     the object during the timestep
    Let  m           be the mass of the object
    Let  t           be the duration of the timestep

    Let  c1 = 0.5*t*t/m
    Let  c2 = t/m

    px = px + t*vx + c1*fx
    py = py + t*vy + c1*fy
    pz = pz + t*vz + c1*fz

    vx = vx + c2*fx
    vy = vy + c2*fy
    vz = vz + c2*fz

    # Timestep simulated for this object!

If the mass of the object changes during the timestep, for example like in a rocket or some kind of thruster, we should use $$m(t) \vec{a}(t) = \vec{F}(t) + \vec{u}(t) \frac{d m}{d t}$$ where $u$ is the velocity vector of the exhaust, and $\frac{d m}{d t}$ is the rate of change of mass, negative for exhaust. I do believe the continuous-time solution for $\vec{a}(t)$ (with constant force $\vec{F}$) is $$\vec{a}(t) = \frac{\vec{F} + w \vec{u}}{m_0 + w t}$$ where mass changes as $m(t) = m_0 + w t$; that integrates to $$\vec{v}(t) = \vec{v}(0) + \left ( \vec{u} + \frac{\vec{F}}{w} \right ) \log \left ( \frac{t w}{m_0} + 1 \right )$$ and again to $$\vec{x}(t) = \vec{x}(0) + \left( \vec{F} \left ( \frac{t}{w} + \frac{m_0}{w^2} \right ) + \vec{u} \left ( t + \frac{m_0}{w} \right ) \right ) \log \left ( \frac{t w}{m_0} + 1 \right ) + t \left ( \vec{v}(0) - \vec{u} \right ) - \vec{F}\frac{t}{w}$$ At the limit $w = 0$, these simplify to the constant-mass functions earlier above. So, the pseudocode to implement a time step when the mass of the object changes, would be

Timestep:
    Let  px, py, pz  be the location of the object
                     at the start of the timestep
    Let  vx, vy, vz  be the velocity of the object
                     at the start of the timestep
    Let  fx, fy, fz  be the sum of forces acting on
                     the object during the timestep
    Let  ux, uy, uz  be the velocity of the exhaust
    Let  m           be the mass of the object
                     at the start of the timestep
    Let  w           be the rate of change of mass
                     (negative if exhaust)
    Let  t           be the duration of the timestep

    c0 = t / w
    c1 = m / w
    c2 = log(1 + t*w/m)

    px = px + (fx*(c0 + c1/w + ux*(t + c1))*c2 + t*(vx - ux) + fx*c0
    py = py + (fy*(c0 + c1/w + uy*(t + c1))*c2 + t*(vy - uy) + fy*c0
    pz = pz + (fz*(c0 + c1/w + uz*(t + c1))*c2 + t*(vz - uz) + fz*c0

    vx = vx + (ux + fx/w)*c2
    vy = vy + (uy + fy/w)*c2
    vz = vz + (uz + fz/w)*c2

    m = m + t*w

    # Timestep simulated for this object!
8
On

This is quite straightforward. Seems like your acceleration is along $\theta$ (direction that you are aiming in $(x_f -x_i, y_f-y_i)$ and your $\Delta t=1$. So $$V_{fx}=V_{ix}+a\cos \theta$$ and

$$V_{fy}=V_{iy }+a\sin \theta$$

23
On

If your force is known, you can have the acceleration by Newton's Law $\vec{F} = m \vec{a}$. So $$ a_x = F_x/m \qquad \text{and} \qquad a_y=F_y/m $$ to update the velocity you can get that by relation $\vec{a} = \frac{d\vec{v}}{dt}$. So $$ \int_{v_{x_0}}^{v_{x}} dv_x = \int_{t_0}^{t} \frac{F_x}{m} \, dt \implies v_x(t) = v_{x_0} + \int_{t_0}^{t} \frac{F_x}{m} \, dt $$ And same for $v_y$. The difficult part is when you evaluate the integral. Its not always have a closed form.

If however you want to update the velocity without include time as parameter (for example your force is function of position), you can use the following form. $$ a_x = \frac{d v_x}{dt} = \frac{d v_x}{dx} \frac{dx}{dt} = \frac{d v_x}{dx} v_x \implies \int v_x dv_x = \int a_x dx \implies v_x^2 = v_{x_0}^2 + 2\int_{x_0}^{x} a_x dx $$ The formula become simple if your force is constant. In that case you'll have the form $$ v_x = v_{x_0} + a_x (t-t_0) \qquad \text{and} \qquad v_y = v_{y_0} + a_y (t-t_0) $$ and $$ v_x^2 = v_{x_0}^2 + 2a_x (x- x_0) \qquad \text{and} \qquad v_y^2 = v_{y_0}^2 + 2a_y (y- y_0) $$

$\textbf{EDIT :}$

In your particular case, when you hit the particle at any time $t=1,2,3,\dots$ you will change its momentum by giving it an impulse. This achieved by above equation by slighly different interpretation. We know that $$ \int dv_x = \int \frac{F_x}{m} dt \implies \int m dv_x = \int F_x dt $$ If the force is so large in the very short period of time, the equation above is become impulse momentum equation. The term in the right hand side called impulse and the left hand side is the change of momentum. After integrate we write, $$ m v_x = mv_{x_0} + \int_{t_0}^{t_0+\delta t} F_x dt $$ You can give any number you like for the term $\int_{t_0}^{t_0+\delta t} F_x dt$, interpret this as the "hit" we give to the AI. The updated velocity after hitted is the velocity $v_t$. This changes is intantenously.

After you updated the velocity you can use the above equations for track down the trajectory of the particle during the gap time before it get hitted again.

2
On

HINT

Resolve each component and integrate them separately:

$$ x= \int { [\int F_x\, dt/m + v_{xi}]} dt + x_i $$ $$ y= \int {[\int F_y\, dt/m + v_{yi}]} dt + y_i $$