# Simulating a rocket out of control in Box2D

In the void of space, a spaceship releases a rectangle shaped space-torpedo. In half a second, it starts its rocket engine and goes straight until something get in its way, and then boom! The code looks something like this on update():

if (Game::gameplay_timer_.milliseconds() - launch_time_ > thrust_time_) {
body_->ApplyLinearImpulseToCenter(0.006f * body_>GetLinearVelocity(), true);
}


But, what if something disturbs the projectile before igniting the rocket, giving it some rotation? It should enter in an uncontrolled spiral (think KSP or tethered gas canisters in Just Cause 2).

How can I achieve this effect (spiraling uncontrollably)? I tried to apply the impulse in places other than the center using ApplyLinearImpulse(), but I can only make the projectile rotate on itself keeping the direction perfectly straight.

Edit: for visualization purposes, I mean something like this. But in 2D.

• I might know someone let me return with an answer Aug 30 at 17:42
• Could you add an animated gif or a link to a YouTube video that shows the effect you're after? What would help us visualize what you want. Aug 31 at 0:31
• If the new code solved your problem, you should share it and the animation of it in action as an Answer below, not as an edit to the question. Keep the question post focused on the original problem. Sep 1 at 15:45

Your update method accelerates the rocket along the direction of its velocity vector (as returned by body_->GetLinearVelocity) but it should accelerate it along the direction it's facing.
Currently, there's nothing in the update method that can change the rocket's velocity vector orientation.

Box2D allows you to get the current world angle of your body with the function b2Body::GetAngle, allowing you to apply the thrust in the desired direction by projecting it along the axis.
(Pasting OP's own answer using this projection for completness):

if (Game::gameplay_timer_.milliseconds() - launch_time_ > thrust_time_) {
delta_.x +=  SDL_sinf(body_->GetAngle()) * speed_ * delta_time;
delta_.y += -SDL_cosf(body_->GetAngle()) * speed_ * delta_time;
body_->ApplyLinearImpulseToCenter({delta_.x, delta_.y}, true);
}

• This answer would be even better if it gave an example of how to accelerate in the facing direction instead. Sep 1 at 15:16
• Now I see the problem. Updated the code and now it looks closer to what I want. Sep 1 at 15:18

Improved the code thanks to @Sacha's clues.

if (Game::gameplay_timer_.milliseconds() - launch_time_ > thrust_time_) {
delta_.x +=  SDL_sinf(body_->GetAngle()) * speed_ * delta_time;
delta_.y += -SDL_cosf(body_->GetAngle()) * speed_ * delta_time;
body_->ApplyLinearImpulseToCenter({delta_.x, delta_.y}, true);
}


Now the projectile rotates and it's trajectory is modified. See it here:

But still isn't perfect. It continues to follow a semi-straight direction and I'd like it to be more "chaotic". Any suggestion for improvement will be appreciated.

Edit: If you set the angularDamping value of the b2Body to 1, the effect is even more interesting.

• You should edit your question with this new information instead of posting it as an answer. Sep 1 at 18:55
• @DMGregory doesn't agree. Look at his comment on my OP. Sep 1 at 19:25
• My bad. I still feel that your original question is not yet fully answered and that you're only half-way to what you're wanting to achieve though. Sep 1 at 19:36
• Anyway I think that your rocket's turning too fast, the discrete acceleration at each step might be cancelling out. Did you try reducing its intensity? Sep 2 at 22:55
• Following your advice @Sacha, I've tuned the angularDamping value of the b2Body and now the effect is more interesting. Sep 7 at 12:10