thombruce / verse

🚀 A universe in progress
Other
8 stars 0 forks source link

Ship is hit by own bullets #74

Closed thombruce closed 12 months ago

thombruce commented 12 months ago

Ship is occasionally hit by own bullets when ship speed is in excess of 500 m/s.

This is partially a system ordering issue: Ship movement and bullet spawning/movement are not ordered in a way which prevents this happening.

But this is why I've previously spawned bullets out in front of the ship X amount of metres too; it was a sloppy workaround to prevent collisions with self. We might consider going to the root of the problem instead... Ideally we want to introduce a rotating turret-mounted gun at some point, and the bullets from this would spawn within the collider of the player/enemy ships, so we need to resolve the problem eventually.

Suggested solutions:

  1. Add a spawner attribute to projectiles and ignore collision with the spawner (could also be used to toggle friendly-fire on/off)
  2. Add a timer prior to which the bullet should be inert (enough time to escape ship collider, little enough to hit enemies in front)
  3. Some combo of these

Reasons not to like:

  1. Prevents hitting self if we add gravitation to bullets, which could otherwise make for fun/complex gunplay
  2. Sloppy, and could miss targets immediately in front of the player
  3. _

So how would a combo work? We spawn a bullet with knowledge of its spawner, preventing it from hitting the spawner at the time it was spawned. Then, after a few hundred milliseconds, we remove that knowledge thus enabling friendly-fire and damage to self under the effects of gravity, etc.


Is this the best way to achieve this?

Is this the only way to achieve this?

Let's restate the problem:

I can't think of a better way about it than... yeah, the bullet needs to know who fired it initially.

But be wary of the concept of larger and larger ships. Eventually (maybe right now) we want to be able to mount weapons systems at different positions on the ship. For very large ships, this might mean turret-mounted guns the projectiles of which would need to traverse the breadth or length of the ship before leaving its collider...

...which means the easiest thing to do is to ignore the spawning entity entirely, rather than ignore it for a given length of time.

Can we... make the bullet "ACTIVE" after leaving the collider it was initially spawned in?

I mean, I'm just thinking about a possible setup later in development, where the player might set up two wormholes and fire bullets at themselves through them - they'd expect to hit themselves. We need friendly fire to be on by default. We just need to ignore the overlap initially.

thombruce commented 12 months ago

The way that we detect collisions right now is to detect events matching CollisionEvent::Started from the Rapier2d physics system.

But we can also check CollisionEvent::Stopped to determine when the bullet has left the spawner's collider.

At this point we would either...

  1. Remove the spawner entity from the bullet, modifying our systems to have this reflect that the bullet may now cause damage to self
  2. Make the bullet "ACTIVE" (not inert), meaning it can now cause damage to anything
  3. Again, some combo

Why some combo? Because it might be useful to keep the spawner detail around on the bullet, so that we know whose bullet hit what. This will be useful for determining whether or not to award points to the player (and while multiplayer is not planned, it would be crucial to know who destroyed who in a multiplayer setting).

So consider adding a Spawner or SpawnedBy entity component to the projectiles, and flagging the bullet as at least InertToSelf (maybe InertToAll) until the initial CollisionEvent has stopped.