ProjectileWeapon
new ProjectileWeapon(views : Object, type : string, index : number, mesh : THREE.InstancedMesh, physics : Physics, properties : Object)ProjectileWeapon - Projectile weapon proxy extension
Extends the base Weapon class with projectile-specific properties like speed, gravity, drag, lifetime, and AoE radius. Projectiles follow physics-based trajectories with automatic visual mesh syncing.
Mesh instances automatically sync with active projectiles via Three.js onBeforeRender
callbacks with smooth interpolation. Requires InstancedMesh created via physics.createMeshPool().
Constructor Parameters
Example
import { Physics, WeaponType, WeaponEvent, ProjectilePreset } from '@three-blocks/pro';
// Grenade launcher (slow-firing, small pool)
const grenadeTemplate = new THREE.Mesh(sphereGeometry, material);
const grenadePool = physics.createMeshPool(grenadeTemplate, 2, scene);
const grenade = await physics.addWeapon(WeaponType.PROJECTILE, grenadePool, {
damage: 100,
...ProjectilePreset.GRENADE,
});
grenade.weapon.addEventListener(WeaponEvent.EXPLODE, (event) => {
spawnExplosion(event.position, event.aoeRadius);
});
grenade.weapon.fireFromCamera(); // Meshes sync automatically!
// Tower defense (rapid-fire, larger pool)
const arrowTemplate = new THREE.Mesh(arrowGeometry, arrowMaterial);
const arrowPool = physics.createMeshPool(arrowTemplate, 20, scene);
const tower = await physics.addWeapon(WeaponType.PROJECTILE, arrowPool, {
damage: 35,
...ProjectilePreset.ARROW,
});
tower.weapon.fire(towerBodyIndex, { direction: [...] }); // Automatic syncing!
Properties
# .meshAutoUpdate : boolean
Automatically sync mesh position with projectile physics. Set to false to manually control mesh position.
# .speed : number
Get projectile speed (m/s).
# .speed : number
Set projectile speed (m/s).
# .gravity : number
Get gravity multiplier (1.0 = normal gravity).
# .gravity : number
Set gravity multiplier (1.0 = normal gravity).
# .drag : number
Get drag coefficient (air resistance).
# .drag : number
Set drag coefficient (air resistance).
# .lifetime : number
Get projectile lifetime in seconds.
# .lifetime : number
Set projectile lifetime in seconds.
# .aoeRadius : number
Get area-of-effect radius (0 = no AoE).
# .aoeRadius : number
Set area-of-effect radius (0 = no AoE).