Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sensorGroup and sensorMask combination for ships and bullets in Nape

I have multiple spaceships (SHIP_CB CbType), that can all shoot missiles (BULLET_CB CbType). How do I set up there sensorGroup, sensorMask and InteractionListeners so I get the following behaviour:

  1. Ships all collide and bounce off each other.
  2. Bullets don't react or sense each other at all.
  3. Bullets sense with Enemy Ships.
  4. Bullets do not sense with the Ship who shot the bullet.

Is it even possible to get all these behaviours just by setting the correct sensorGroup and sensorMask on each object?

like image 809
Adam Harte Avatar asked Jan 14 '23 18:01

Adam Harte


1 Answers

Assuming bullets are only ever going to sense, and so should not interact in any way with the ship that shot them, one way would be:

Starting with everything defaulted,

for each ship

ship.group = new InteractionGroup(true);

for each bullet created for a given ship 'ship'

Set bullet to have the same interaction group as the ship who shot it. that way, since ignore=true on group, any bullet fired from a given ship will be excluded from interacting with that ship.

bullet.group = ship.group;

for each shape of the bullet, probably only 1. make bullet shape sensor

bulletShape.sensorEnabled = true;

make bullet shapes sense with everything except for themselves.

bulletShape.sensorGroup = 2;
bulletShape.sensorMask = ~2;

ref: Nape Manual: InteractionGroups

You 'could' do this purely with sensorGroup/sensorMask. but you'd be restricted to 31 different ships and the logic would be a bit more complex.

You 'could' also use callback system to ignore interactins between a ship and the bullets it fires with some extra logic in the callback, but it'd be a lot heavier than using the InteractionGroup stuff.

like image 102
deltaluca Avatar answered Apr 26 '23 17:04

deltaluca