SwiftCast - Simple, fast and easy projectiles!

SwiftCast - Simple, fast and easy projectiles!
Download | Source | Github Download

Made by @keklol0401010101010, @Artem2316 and @23sinek345


Features
  • Fast and efficient
  • Easy to use
  • Very flexible
  • Supports BulkMoveTo and Transform movement methods
  • Comes with raycast / blockcast / shapecast / spherecast options
  • Supports custom raycast functions
  • Supports custom stepping / stop and start
  • Supports per projectile stepping
  • Deferred callback system (Allows safe projectile edits and removal)
  • Accurately detects multiple hits within a single frame
  • No external dependencies
  • Strictly typed

Showcase


Example
Setup
  1. Put the module in ReplicatedStorage
  2. Require it

Uncopylocked Place

Rocket Launcher Example

by @AnoB0t


Documentation
Projectile

A projectile is just a raw table

  • These properties are also properties of the projectile constructor

Each property is marked with

  • “r” for direct read permission
  • “w” for direct write permission
  • “b” for built-in function to edit
State:
[rw] Position: Vector3
[r] PreviousPosition: Vector3
[rw] Velocity: Vector3
[rw] Acceleration: Vector3
[r] Elapsed: number
[r] DistanceTravelled: number
Settings:
[r] MaxFlyTime: number 
[r] MaxFlyDistance: number
[r] MaxPiercesPerStep: number
[rw] RaycastParams: RaycastParams?
[rw] RaycastFunction: RaycastFunction
[rb] ProjectilePart: BasePart?
[rw] ProjectilePartRotation: CFrame? -- Rotation for the projectile part to be applied
Callbacks:
[rw] OnHit: (projectile: Projectile, result: RaycastResult, hasPierced: boolean) -> (),
[rw] OnStep: (projectile: Projectile, deltaTime: number) -> ()?,
[rw] OnPositionChange: (projectile: Projectile, newPosition: Vector3, oldPosition: Vector3) -> ()?,
[rw] OnDestroy: (projectile: Projectile, reason: string | "MaxTime" | "MaxDistance" | "Hit" | nil) -> ()?,
Transform Mode Callbacks:
--[[
	Due to the way Motor6D.Transform works we made a PostTransform
callback which is called right after .Transform is applied for the first time.
Otherwise the CFrame of the cosmetic part will appear as origin.
]]
[r] PostTransform: (projectile: Projectile) -> ()

--[[
	This is the 2nd PostTransform. Basically the second time .Transform is
applied. This is helpful to safely toggle vfx on or make parts visible to hide the
pre .Transform phase.
]]
[r] PostTransformSafe: (projectile: Projectile) -> ()

TIP: When using Transform movement mode devs are expected to turns off vfx
when creating them. Only enable it / trigger it on PostTransformSafe.

TIP: If the vfx is not emitted by the cosmetic part or anything welded to it users can trigger it like usual.

Optional data
[rw] BlockcastSize: Vector3?, -- Size for blockcast option
[rw] SpherecastRadius: number?, -- Size for blockcast option
[rw] ShapecastPart: BasePart?, -- Part for shapecast
[rw] UserData: any, -- Optional user data
ProjectileSettings

Projectile settings is a raw table with settings for projectiles

Construct:

SwiftCast.ProjectileSettings.new(parameterOverrideTable?)

Properties:
MaxFlyTime: number,
MaxFlyDistance: number,
MaxPiercesPerStep: number,
RaycastParams: RaycastParams?,
RaycastFunction: RaycastFunction,
CanPierce: boolean | (projectile: Projectile, result: RaycastResult) -> (boolean),

BlockcastSize: Vector3?,
SpherecastRadius: number?,
ShapecastPart: BasePart?,

TIP: Usage of projectile settings is completely optional. It is usually used to apply settings to a large amount of projectiles and avoid setting each parameter on projectile construction

RaycastFunctions

This is a sub table with premade raycast functions. There are currently 4 options you can choose from

SwiftCast.RaycastFunctions.Raycast
SwiftCast.RaycastFunctions.Blockcast
SwiftCast.RaycastFunctions.Spherecast
SwiftCast.RaycastFunctions.Shapecast
Config
-- Uses protected calls for event callbacks
Config.UseProtectedCalls: boolean

-- Starts the simulation on require
Config.StartOnRequire: boolean

-- Switches between BulkMoveTo and Transform movement methods
Config.MovementMethod: "BulkMoveTo" | "Transform"

-- The offset ray adds to the raycast position to search for the next hit
RaySearchOffset: number
Run Order
  1. Update projectiles
  2. Move projectiles
  3. Destroy marked projectiles
  4. User events
-- Creates a projectile and activates it
-- @return Projectile
SwiftCast.SpawnProjectile(constructorArgs: ProjectileConstructorArgs, projectileSettings: ProjectileSettings?)
-- Creates a projectile
-- @return Projectile
SwiftCast.CreateProjectile(constructorArgs: ProjectileConstructorArgs, projectileSettings: ProjectileSettings?)
-- Activates a projectile making it start simulating
SwiftCast.ActivateProjectile(projectile: Projectile)
-- Deactivates a projectile making it stop simulating
SwiftCast.DeactivateProjectile(projectile: Projectile)
-- Steps a projectile
SwiftCast.StepProjectile(projectile: Projectile, deltaTime: number)
-- Terminates a projectile firing OnDestroy callback
SwiftCast.TerminateProjectile(projectile: Projectile, reason: string?)
-- Checks if SwiftCast simulation is running
-- @return boolean
SwiftCast.IsRunning()
-- Starts SwiftCast simulation
SwiftCast.Start()
-- Stops SwiftCast simulation
SwiftCast.Stop()
-- Steps SwiftCast simulation
SwiftCast.Step(deltaTime: number)
-- Sets a projectile part for the projectile
SwiftCast.SetProjectilePart(projectile: Projectile, part: BasePart?)

Benchmarks

SwiftCast performs very well among its competitors. Swiftcast runs on a single thread with DoD flavor without any parallel scripting, this has few advantages and drawbacks. One of the most important advantage is being able to utilize WorldRoot:BulkMoveTo more efficiently. Its main drawback is calculating invisible projectiles where computation power (raycast cost) matters more than running in serial.
SwiftCast also does something that none of its competitors do, which is Motor6D.Transform based movement. This allows for extremely fast part movement even for thousands of displayed projectiles.

SwiftCast outperforms all big projectile libraries in instanced mode. Instanced mode is where each projectile has a BasePart associated with it. The BasePart is synced to projectile position every frame making a cosmetic visual for the projectile.

Diqualified Libraries Reason Exception
FlashCast Ratelimits -
SimpleCast Ratelimits Unlocked ratelimiting manually

Below are benchmarks done on my machine (i5-13400f CPU, RTX4060Ti, 16GB DDR5)
Live Client Benchmark:

Library Projectiles Average FPS
SwiftCast Transform 2000 237
SwiftCast BulkMoveTo 2000 194
SimpleCast 2000 149
FastCast2 8 Actors 2000 109
Library Projectiles Average FPS
SwiftCast Transform 4000 154
SwiftCast BulkMoveTo 4000 94
SimpleCast 4000 73
FastCast2 8 Actors 4000 36
Library Projectiles Average FPS
SwiftCast Transform 8000 78
SwiftCast BulkMoveTo 8000 44
SimpleCast 8000 34
Library Projectiles Average FPS
SwiftCast Transform 10000 61
SwiftCast BulkMoveTo 10000 37
SimpleCast 10000 27

Here we can see a notable performance difference among each library. Also this took notorious amounts of efforts to benchmark due to other libraries having weird quirks that makes it hard to get a fair reading. With that out of the way here’s a summary:
Fastest to slowest

  1. SwiftCast Transform
  2. SwiftCast BulkMoveTo
  3. SimpleCast
  4. FastCast2

For instanceless mode (invisible projectiles) FastCast2 outperforms all other libraries including SwiftCast on server with race being head to head till 15k projectiles.
On the other hand SwiftCast outperforms all other libraries on client.


Versions

1.0

  • Initial release

1.1

  • Fixed: NaN error on .Unit calculation
  • Added: OnPositionChange
  • Cleaned up unused variables
  • OnStep only uses projectile and deltaTime as arguments now

1.2

  • Fixed: PostTransformSafe not firing when PostTransform is not defined
  • Replaced OnRayPierce with OnHit. Now fires regardless of destruction status
  • Removed: OnRayPierce
  • Added: MaxPiercesPerStep
  • A lot of minor fixes

1.3

  • RaycastParams are now optional
  • Fixed event ordering
  • Fixed OnPositionChange and OnStep not firing for projectiles destroyed on OnHit
  • MaxFlyDistance is now math.huge by default
  • Fixed default ProjectilePartRotation argument

1.4

  • Added: Fixed delta time and more accurate stepping formula to preserve accuracy across different frame rates
  • Destroyed now fires after OnPositionChange
  • Added: Motor6D pooling for faster projectile creation in transform mode

Special Thanks To:

@haotian2006 for helping out with Transform mode
@Z0nito and @Thawux for demonstration vfx

DISCLAIMERS

  • Currently SwiftCast runs on a single core. Parallel support is planned in the future.
  • Use Github Download for the latest release. Roblox might deny / delay some updates
109 Likes

this is very peak. good job. benchmark tests are tuff.

5 Likes

Can’t wait for yariik to find this

4 Likes

Nice works bro.
Really Insane projectile library.

I wanna contributing to this library.

5 Likes

7 Likes

Roblox model is private

1 Like

Roblox flagged it as usual. Just anything but positive to the platform :grin:

2 Likes

Roblox heard someone was having fun that isn’t a yk what

4 Likes

cool stuff, havent seen a lot of people use Motor6D.Transform

2 Likes

thanks for the module mate!

dont mind if I “borrow” this code :slight_smile:

1 Like

this seems promising, gonna try this with my game since you included the use of Transform from Motor6D, i had no idea how to get it right when i first tried with FastCast

also does it cache any projectile after terminating projectiles?? this doesn’t mention anything abt it and i haven’t looked into this yet

1 Like

No it doesn’t cache any projectiles nor parts. Once a projectile is destroyed it’s forgotten about.

2 Likes

Hey, I wanna share my benchmarks with you.

FastCast2 client side test 6000~ projectiles:


fps: 11-12

SwiftCast client side test 8000 projectiles:


fps: 12-13

SwiftCast does a really impressive performance without any parallel scripting, Tested on lowend pc:

  • CPU: i5 gen2 2400
  • GPU: onboard
  • Storage: 300 gb~
  • Ram: ddr3(4 gb)
6 Likes

Cool benchmarks, but I want to clarify a few things. In the first screenshot, you’re measuring current FPS rather than the average. In the second, you can see other projectiles jumping in the background, which contaminates the results. Benchmarks are really cumbersome to get right, even the way you spawn projectiles matters due to factors like overdraw.

3 Likes

And roblox raycast optimizations aswell. So initial conditions for each projectile must be same

2 Likes

I prefer fps-based benchmarking, I can benchmarks average fps If I want.
Ty for feedbacks

1 Like

SwiftCast 1.1 is Here!

  • Fixed: NaN error on .Unit calculation
  • Added: OnPositionChange
  • Cleaned up unused variables
  • OnStep only uses projectile and deltaTime as arguments now

Use github download to get the latest release.

3 Likes

I’m just wondering why use this over something like fastcast2

2 Likes

6 Likes

transform mode performs allot better than bulkmoveto mode, is there a specific reason to choose bulkmoveto over transform

1 Like