0.0.9 • Published 1 year ago

ptcl v0.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Particles

A lightweight physics engine for powering particle systems in threejs.

Quick start

npm install ptcl

Example

This code creates an animation of a particle being thrown up in the air and falling back down due to gravity.

import * as THREE from 'three';
import { Particles, updateMeshesArray } from 'ptcl';
    
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 10;
        
const scene = new THREE.Scene();
    
const clock = new THREE.Clock();

const maxParticles = 5;

const material = new THREE.MeshBasicMaterial();
const geometry = new THREE.SphereGeometry(0.1);
const mesh = new THREE.Mesh(geometry, material);

let meshes = [];
for (let i=0; i < maxParticles; i++){
	const m = mesh.clone();
	meshes.push(m);
	scene.add(m);
}

const particles = new Particles(maxParticles); 
particles._addGlobalConstantForce(0,-10,0);

for (let particle of particles){
	particle.setPosition(particle.pIndex-2,0,0);
	particle.setMass(1);
	particle.setVelocity(0,10,0);
}

const renderer = new THREE.WebGLRenderer({canvas: document.getElementById("game")});
renderer.setSize( window.innerWidth, window.innerHeight );

function animate() {
	requestAnimationFrame(animate);

	particles.integrate(clock.getDelta());
	updateMeshesArray(particles,meshes);

	renderer.render(scene, camera);
}
animate();

This should be what you get:

Running tests

npm run tests
0.0.9

1 year ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago