1
\$\begingroup\$

I have a submarine model that I would like to shoot torpedoes.

I want these torpedoes' initial velocity to vary with the forward velocity of its shooting submarine (so they go faster if launched off a moving sub, slower if show from a stationary sub).

I have a few requirements I'm trying to achieve:

  1. Torpedos despawn when they swim too far away. No need to keep them in memory if they're too far too see.
  2. When a torpedo is updated, and collision detection is done, there should be a way to determine who shot the torpedo, so it can be determined who to award the kill.
  3. Torpedos should outlive the lifecycle of its parent sub, in the case the sub is destroyed between the torpedo was shot, and the time the torpedo hits or veers too far away. (Who doesn't love double kill mechanics :D?)
  4. I'm working in C++, so manual memory management is a concern.

I'm debating between two approaches:

  1. Have subs be the owners of their torpedoes. This makes requirements 1 and 2 easy, but complicated 3.
  2. Have the scene be the owners of torpedoes. This makes 3 easy, but complicates 1 and 2.

This seems like it would be a common problem. What's the preferred way to do this?

\$\endgroup\$
  • 1
    \$\begingroup\$ Are you sure you want the torpedo speed to be relative to the submarine speed? A torpedo doesn't work like a gun shot. Torpedoes have a rotor on the back maintaining speed, and some can even adjust the direction. \$\endgroup\$ – KaareZ Dec 11 '16 at 22:53
  • \$\begingroup\$ @KaareZ Good point! If I drop that requirement, I'll have to cap the speed of the sub to make sure it can never outrun the torpedo it just launched \$\endgroup\$ – Alexander Dec 11 '16 at 22:55
  • \$\begingroup\$ That's not why you'll cap the speed of the sub. You'll cap the speed of the sub because it can't got at an infinite speed. Same for the torpedo. It's just that the max speed of a torpedo is much higher than the one of the sub. And the speed of the torpedo should be pretty constant... \$\endgroup\$ – Vaillancourt Dec 12 '16 at 11:55
5
\$\begingroup\$

I'm assuming that you dropped the concept of the speed of the torpedo influenced by the speed of the sub-marine, and having them traveling at a constant speed.

Have a TorpedoFactory that will spawn the torpedoes and add them to the scene.

The parameters:

  • torpedo origin
  • torpedo orientation
  • time to live
  • submarine that spawned the torpedo

Each frame, update all of your torpedoes

  1. If the time alive is greater than the time to live, delete it.
  2. Move the torpedo.
  3. Check for collision
    1. If the torpedo hits a sub, and it the originator carry on.
    2. Otherwise create an explosion and destroy the sub and the torpedo.
  1. Torpedos despawn when they swim too far away. No need to keep them in memory if they're too far too see.

Check. Time to live stored in the torpedo.

  1. When a torpedo is updated, and collision detection is done, there should be a way to determine who shot the torpedo, so it can be determined who to award the kill.

Check. The data is in the torpedo object.

  1. Torpedos should outlive the lifecycle of its parent sub, in the case the sub is destroyed between the torpedo was shot, and the time the torpedo hits or veers too far away. (Who doesn't love double kill mechanics :D?)

Check. The torpedo is independent of the sub, it's an entity of its own.

  1. I'm working in C++, so manual memory management is a concern.

Not if you use smart pointers. Google about std::unique_ptr and std::shared_ptr. Prefer std::unique_ptr as much as possible, as described by Sean Middleditch here.

\$\endgroup\$
0
\$\begingroup\$

Both the torpedos and the submarines should be a separate object in the scene, and the torpedos should contain a reference to the parent submarine's position and to the owner of the submarine inside them.

\$\endgroup\$

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.