Projectiles
Use projectiles for arrows, spells, bullets, beams, and other temporary moving effects that need server-side impact validation without synchronizing their position every frame. The server is authoritative. It emits compact projectile spawn, impact, and destroy batches. The client predicts the visual movement locally, including a single local raycast at spawn time to avoid drawing projectiles past obvious client-side hitboxes while it waits for the server impact, and renders each projectile with a registered CanvasEngine component. The batches follow the sync rate of the map, not the number of shots: whatemit() queues is sent once per sync interval (throttleSync, 50 ms by
default), so the shots of one interval share one message and a shot is drawn up
to one interval after emit(). A projectile that stops on its hit sends its
impact once, in its destroy entry; the client still calls projectiles.onImpact
for it.
When a projectile uses a custom server-side canHit filter, the client keeps
predicting movement but skips local impact raycast clamping by default because
arbitrary server gameplay rules cannot be represented safely on the client. If
the filter still matches client-visible physics, opt back in with
collision.predictImpact: true.
Server Usage
Emit a projectile from a player:spreadDegrees applies a random direction offset of +/- spreadDegrees / 2.
You can use accuracy instead when you prefer a normalized precision value
from 0 to 1; 1 is perfectly accurate, 0 can deviate up to 30 degrees.
payload stays server-side and is useful for damage, states, knockback, or any
other gameplay data. params is sent to clients and should contain only visual
data used by the projectile component.
You can also emit from the map:
Shoot Toward The Pointer
Projectiles can also use a direction vector. For pointer-based attacks, send the target position through the normal action input flow, then validate and resolve the shot on the server.map:click with a world position or event:click with an event id. The
client sends intent and context; the server still owns validation, collisions,
damage, and impacts.
Aim At A Target
Givetarget instead of direction to aim at a point or at an entity (a
player or an event, aimed at its center). The server computes the direction.
arc lands on its target; range is then its maximum reach, so a target
that is too far makes it land short. landing makes the arc hit what stands
where it lands (see Behaviors). Other trajectories only take their
direction from the target and keep flying their range.
Repeats And Patterns
Userepeat for a compact burst. The server broadcasts one spawn batch with
per-projectile delays, so clients can render the whole burst without receiving a
position update every frame.
pattern for common shapes:
Trajectories
trajectory.type picks the path. The server simulates it and the client draws
it with the same function, so only its name and parameters are synchronized.
return aims at the current position of the launcher, on the
server and on the client.
Custom trajectories
Register a trajectory once in shared code (imported by both the server and the client). It is a pure function of the elapsed time:owner(), the current center of the launcher. z is the
height above the ground and phase is a free label sent to the component.
Behaviors
These options work with every trajectory:
An arc flies over everything, so
landing is how it hits: one target for an
arrow, an area with pierce for a bomb. Each entity it hits triggers onHit
and can still answer through onProjectileHit. A landing that hit something
does not linger on the ground, unless the target replied stick.
Hooks of one projectile, on the server:
Every hook receives
projectile, owner and map. Bounces, returns and
planted projectiles are sent to the clients as compact updates.
Targets Reacting To Impacts
Any event (or player) hit by a projectile can react itself withonProjectileHit, without changing the weapon. It runs on the server, before
the projectile onHit, and its reply decides what the projectile does:
type, ownerId, params, trajectory) and
{ owner, map, x, y, hit }. Module hooks can reply too: event.onProjectileHit
and player.onProjectileHit in defineModule() may return "pass", "stick"
or "stop" at once (a promise cannot decide: the projectile does not
wait). The first module that replies decides, and the event’s own
onProjectileHit has the last word. Scripts reply the same way with
event.projectileHit (see the scripts guide).
Impact Hooks
Use projectile hooks to apply gameplay effects when the server confirms an impact:canHit filter only narrows server gameplay targets and the normal map
physics hitboxes are still safe for visual prediction, enable local clamping:
Standard Look
A projectile type without a registered component is drawn by the standard projectile component of@rpgjs/client. The server only names the projectile
with its type; its look is declared once on the client, in projectiles.looks:
z, tilts it along its
climb and fall, leaves it planted when it sticks, and plays an impact that
depends on what it ended on: sparks on a target, sparks thrown back by a wall,
dust on the ground.
These fields are typed by
ProjectileLook, exported by @rpgjs/client. A look
can also be registered at runtime with engine.registerProjectileLook(type, look).
A preset of its own ({ emitters: [...] }, such as the parts of a Studio Visual
Effect) gives a projectile particles that no built-in preset has:
params override the look for one shot. Keep
that for what really changes per shot, such as the color of an elemental arrow;
everything else belongs to the client look, so it is not sent with every
projectile:
params.
Client Registration
Register a CanvasEngine component to draw a projectile type yourself:Projectile Component
Projectile components receive predicted movement props from the client runtime.id,type,ownerIdx,y,origin,direction,angleflightAngle, the angle to draw the sprite with:angletilted up while the projectile climbs and down while it fallsspeed,range,ttl,distance,elapsed,progressz, the height above the ground: draw the sprite aty - zand a shadow atyphase, the part of the path (out,back, orstuckonce the projectile is planted or lingering)trajectory, its name and parametersimpactProgressfrom0to1while the client keeps an impacted projectile visible for its impact animationindex,countfor repeated or patterned projectilesparamsfor visual customizationimpactwhen the server confirms a collision.impact.surfacetells what the projectile ended on:target,wall, orgroundfor an arc that landed without hitting anything. Whileimpactis present,x,y, anddistanceare pinned to the authoritative impact point. Before the server confirmation arrives, the client may also pinx,y, anddistanceto a locally predicted impact point;impactremains undefined until the server confirms the collision.
x, y, z, phase, angle, flightAngle, distance, elapsed,
progress, impact, impactElapsed, impactProgress, destroyed) are
updated on every frame without rebuilding the component. Before an impact,
impact is null and impactElapsed / impactProgress are 0.
Do not apply damage in the component. Components are visual only; gameplay
effects belong in server projectile hooks.
Recipe: an arrow with a trail
A component can keep its own state. This arrow remembers its last positions to draw a fading trail, stops the trail once planted (phase is stuck), and
reads its color from the params sent by the server:
projectiles: { components: { arrow: ArrowProjectile } }. For a particle trail,
use <Fx name="..." loop /> from @canvasengine/presets instead of the
Graphics line.