Introduction
There aren’t a lot of sources out there on creating proper server authoritative combat, I aim to address this and help roblox developers with creating more secure games.
Prerequisites / Features
- Good performance
- Synced Server-Client simulation
- Multi-threading / Parallel LuaU support
- Lag compensated hitboxes on the server
- Bullet drop, grenades bouncing, ricochets, wall penetration
Technical Deep Dive
Click me for a technical deep dive into how the system works.
Parallel LuaU has one big limitation we must solve if we want to have lag compensated hitboxes: the inability to modify the DataModel in parallel execution. This prevents us from being able to create hitbox parts for the lag compensation which means we must do the hit detection for players completely in Lua, thankfully we can achieve pretty good results due to the massive amount of work done by the LuaU team in making the VM very fast.
I took advantage of various algorithms to achieve this with ~410 microseconds when checking against 100 players, let’s explore those various algorithms below.
Fast Voxel Traversal
First we need to separate the world into a 3D voxel grid, we use the voxel grid to quickly filter out which players the ray might intersect.
~1 Microsecond per traversal
Red dots are players
Blue line is our ray
Green squares are the voxels which contain our ray
Axis-Aligned Bounding Box
Next we will employ a bounding box intersection test, we use this to quickly discard any players which the voxel traversal returned but are not actually possible to intersect. This is done so we don’t spend time running exact hitbox collision checks (15 per player) on players which we have no possible intersection with.
Blue boxes are the voxels traversed
Green box is our Axis Aligned Bounding Box
Oriented Bounding Box
Finally we use an oriented bounding box intersection to determine the precise point at which an intersection occurs, these checks are relatively expensive so we only want to use them as a last resort.
This is also worsened by relatively slow CFrame interpolation needed to compute the precise hitbox position, I assume this is due to CFrame being a non native type.
Images / Showcase
Benchmark / Performance
I conducted a benchmark on 2023-08-31T20:50:00Z using Native Luau code generation which consisted of ~630 projectiles intersecting 50 characters spread over 9 cores every frame, I was able to maintain 60 FPS on the server with less than half of the frame time being used up. I advise anyone looking to use this project to run their own set of benchmarks and teststo evaluate the performance of the module for their specific use case, for those of you who are not able to or are willing to trust my numbers I can say with 100% confidence that this module should work for 99% of use cases out there.
Download
NOTICE: This system hasn’t been used in a production environment, use at your own risk.
Downloading from Wally
secure-cast = "1axen/secure-cast"
Downloading from GitHub releases
Downloading the source from GitHub
Documentation
The documentation can be found at SecureCast API
Feedback / Suggestions
This is my second community resource, if you have any suggestions/feedback for me please don’t hesitate to share it with me. I would also love to hear any ideas on making the system even more performant.
License
MIT License
Copyright (c) 2023 Axen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.