Skip to content

P4: Add a weapon flag to use the weapon's radius in ship collisions - #8073

Open
dustinspace217 wants to merge 3 commits into
scp-fs2open:masterfrom
dustinspace217:collision/weapon-radius-sphere
Open

dustinspace217 wants to merge 3 commits into
scp-fs2open:masterfrom
dustinspace217:collision/weapon-radius-sphere

Conversation

@dustinspace217

Copy link
Copy Markdown
Contributor

What?

Adds a weapon flag, "use radius for ship collisions". A flagged laser or missile hits a ship when any part of its radius touches the hull or shield, not only its center line. The radius is the weapon's object radius ($Collision Radius Override, the laser head radius, or the whole model's radius for POF weapons). Beams are unaffected. Weapons without the flag behave exactly as before.

Fixes #4386

Why?

A laser with a big radius only hits a ship if its center line touches, so a shot whose glow overlaps a freighter flies through it. Goober suggested an opt-in flag on the issue.

How?

With the flag, the hull and shield-mesh checks in ship_weapon_check_collision use the sphere sweep (MC_CHECK_SPHERELINE) that auto-spread shields already use, and the hit point is where the sphere touches. A sphere that already overlaps the hull at spawn skips that face, so flagged weapons also check the shot's line and keep the nearer face: a hit lands on the same face as without the flag, or one nearer the shooter.

The line is kept for a zero radius and for auto-spread ships (their shield is already a sphere sweep), even with their shields switched off. The collision-pair cull also counts a flagged weapon's radius against ships, or wide shots past small ships were dropped.

Testing?

This branch vs master 94d278a, Release, OpenGL and -vulkan. MediaVPs 5.0.3, SM1-01, and a one-table test mod:

#Primary Weapons
$Name:	Prometheus R
+nocreate
$Collision Radius Override:	20.0
$Flags:	( "use radius for ship collisions" )
#End

Rows of 81 shots from Lua at stationary ships:

Target master flag
Poseidon (hull) 37 hits 79
Hercules (shields up) 19 59
Fenris cruiser 37 57
From 5-15 m all hit all hit, same or nearer face

Same on both renderers. Without the flag, every row matched master, apart from two shield-seam shots that flicker on master too. Debug logs matched master's (the Debug baseline is f0d45f6, master's parent).

Not tested: surface shields, auto-spread ships, ships with $Collision LOD, moving targets, homing missiles, multiplayer.

Anything Else?

Flagged weapons pay a sphere check plus a line check per frame, so use the flag on a few weapons. In my test, 200 flagged shots a second at a destroyer and six fighters added about 1 ms per frame (12.9 to 13.9 ms), with four times as many hits. The flag needs a line on the wiki's weapons.tbl page.

I used Claude Code to help write and test this.

Ship-weapon collisions trace a thin line, so a weapon's collision radius
never matters against ships. The new "use radius for ship collisions"
flag makes the hull check and the shield-mesh check (normal and surface
shields) sweep a sphere of the weapon's radius instead. The collision
pair cull also counts that radius, so a wide shot passing a small ship
is still checked.

Ships with auto-spread shields keep the line, since their shield is
already a sphere sweep and a wider hull check could reach past it.
A flagged weapon with a zero radius also keeps the line. Weapons
without the flag are unchanged.
A sphere sweep skips faces the sphere already overlaps when the sweep
starts, so a flagged weapon fired from close range registered its hit
on polygons deeper inside the hull than the plain line would. Flagged
weapons now also check the shot's line: when the sphere hits, the face
ahead on the line is used if it is nearer (keeping the sphere's hit
time); when the sphere misses, a line hit within the frame still counts.
This covers the hull and shield meshes.

The collision cull now adds the weapon's radius only against ships, and
also on the path used by turning and accelerating weapons. The comment
at the auto-spread exclusion gives its second reason.
When the line's face is used in place of the sphere's hit, keep
whichever hit time is earlier, so the big-ship collision path never
schedules that hit later than the face it lands on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weapon Object Radius is Ignored for Ship-Weapon Collision Checks

1 participant