1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ I might know someone let me return with an answer \$\endgroup\$
    – LiamBeran
    Aug 30 at 17:42
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Vaillancourt
    Aug 31 at 0:31
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Sep 1 at 15:45
1
\$\begingroup\$

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);
}
\$\endgroup\$
2
  • \$\begingroup\$ This answer would be even better if it gave an example of how to accelerate in the facing direction instead. \$\endgroup\$
    – DMGregory
    Sep 1 at 15:16
  • \$\begingroup\$ Now I see the problem. Updated the code and now it looks closer to what I want. \$\endgroup\$
    – Alex CB
    Sep 1 at 15:18
0
\$\begingroup\$

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:

projectile spiraling

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.

\$\endgroup\$
5
  • \$\begingroup\$ You should edit your question with this new information instead of posting it as an answer. \$\endgroup\$
    – Sacha
    Sep 1 at 18:55
  • 1
    \$\begingroup\$ @DMGregory doesn't agree. Look at his comment on my OP. \$\endgroup\$
    – Alex CB
    Sep 1 at 19:25
  • \$\begingroup\$ 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. \$\endgroup\$
    – Sacha
    Sep 1 at 19:36
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Sacha
    Sep 2 at 22:55
  • \$\begingroup\$ Following your advice @Sacha, I've tuned the angularDamping value of the b2Body and now the effect is more interesting. \$\endgroup\$
    – Alex CB
    Sep 7 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.