Boids ii

Developer
Size
3,358 Kb
Views
10,120

How do I make an boids ii?

What is a boids ii? How do you make a boids ii? This script and codes were developed by Khalkeus on 11 November 2022, Friday.

Boids ii Previews

Boids ii - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>boids ii</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <body id = "b">
</body> <script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Boids ii - Script Codes CSS Codes

body{	width: 100%;	height: 100%;	margin: 0px;	padding: 0px;	overflow: hidden;
}

Boids ii - Script Codes JS Codes

var mouseDown = false;
var dx = 0;
var dy = 0;
var zoomD = 0;
var mouse = [0,0];
var mouseNormalized = new THREE.Vector2(-1, -1);
var mouseSlowFactor = 333;
var colors = [0xffd800, 0x1ae3ce, 0xff0084, 0xb4ff00]
window.onresize = function(){	renderer.setSize(window.innerWidth, window.innerHeight);	camera.aspect = window.innerWidth / window.innerHeight;	camera.updateProjectionMatrix();
}
var scene, camera, renderer, canvas, ctx;
window.onload = function(){	init();
}
function initThree(){	renderer = new THREE.WebGLRenderer({antialias: false});	renderer.shadowMapEnabled = true;	scene = new THREE.Scene();	camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);	renderer.setSize(window.innerWidth, window.innerHeight);	document.body.appendChild(renderer.domElement);	camera.position.z = 100;	var dirLight = new THREE.SpotLight( 0xffffff, 2, 5000);	dirLight.position.set(0, 100, 0);	dirLight.shadow.mapSize.width = 512;	dirLight.shadow.mapSize.height = 512;	dirLight.castShadow = true;	scene.add(dirLight)	var plane = new THREE.Mesh(new THREE.CircleGeometry(100, 10), new THREE.MeshLambertMaterial({color: 0xffffff, emissive: colors[Math.floor(Math.random() * colors.length)]}));	plane.rotation.x = -Math.PI/2;	plane.position.y = -boundingBox;	plane.receiveShadow = true;	scene.add(plane);	scene.background = plane.material.color;	canvas = document.querySelector('#b canvas');
}
var render = function () {	flock.updateFlock();	requestAnimationFrame(render);	renderer.render(scene, camera);
}
var flock, g;
function init() {	initThree();	flock = new flockOfBoids(20);	render();
}
var maxSpeed = .5;
var boundingBox = 40;
var maxDist = 1;
var radius = 5;
function flockOfBoids(size){	this.obj = new THREE.Group();	this.flock = [];	for(var i = 0; i < size; i++){	this.flock.push(new boid(0, 0, 0));	this.obj.add(this.flock[i].obj);	}	scene.add(this.obj);	this.empty = new THREE.Vector3(0, 0, 0);	this.updateFlock = function(){	var v1, v2, v3, v4;	var c = this.empty.clone();	for(var i = 0; i < this.flock.length; i++){	c.x += this.flock[i].obj.position.x;	c.y += this.flock[i].obj.position.y;	c.z += this.flock[i].obj.position.z;	}	c = new THREE.Vector3(c.x/this.flock.length, c.y/this.flock.length, c.z/this.flock.length);	for(var i = 0; i < this.flock.length; i++){	v1 = new THREE.Vector3(c.x - this.flock[i].obj.position.x, c.y - this.flock[i].obj.position.y, c.z - this.flock[i].obj.position.z)	v2 = this.empty.clone();	v3 = this.empty.clone();	var n = 0;	for(var j = 0; j < this.flock.length; j++){	if(i != j){	if(Math.abs(this.flock[i].obj.position.x - this.flock[j].obj.position.x) +	Math.abs(this.flock[i].obj.position.y - this.flock[j].obj.position.y) +	Math.abs(this.flock[i].obj.position.z - this.flock[j].obj.position.z) < maxDist	){	//don't collide	v2.x += (this.flock[i].obj.position.x - this.flock[j].obj.position.x)*50;	v2.y += (this.flock[i].obj.position.y - this.flock[j].obj.position.y)*50;	v2.z += (this.flock[i].obj.position.z - this.flock[j].obj.position.z)*50;	}	if(this.flock[i].obj.position.distanceTo(this.flock[j].obj.position) < radius){	v3.x += this.flock[j].velocity.x;	v3.y += this.flock[j].velocity.y;	v3.z += this.flock[j].velocity.z;	n+= 1;	}	}	}	//match velocity w/ nearby	if(n > 0){	v3.x = (v3.x/n)/8;	v3.y = (v3.y/n)/8;	v3.z = (v3.z/n)/8;	}	//stay within bounding sphere	v4 = this.empty.clone()	if(this.flock[i].obj.position.distanceTo(this.empty) > boundingBox){	v4.x = -this.flock[i].obj.position.x	v4.y = -this.flock[i].obj.position.y	v4.z = -this.flock[i].obj.position.z	}	this.flock[i].holdVel.x += v1.x/100 + v2.x * 100 + v3.x + v4.x/(boundingBox * 10);	this.flock[i].holdVel.y += v1.y/100 + v2.y * 100 + v3.y + v4.y/(boundingBox * 10);	this.flock[i].holdVel.z += v1.z/100 + v2.z * 100 + v3.z + v4.z/(boundingBox * 10);	//bound speed	if(this.flock[i].holdVel.length() > this.flock[i].maxSpeed){	this.flock[i].holdVel = this.flock[i].velocity.normalize();	this.flock[i].holdVel.x *= this.flock[i].maxSpeed;	this.flock[i].holdVel.y *= this.flock[i].maxSpeed;	this.flock[i].holdVel.z *= this.flock[i].maxSpeed;	}	}	for(var i = 0; i < this.flock.length; i++){	this.flock[i].velocity = this.flock[i].holdVel;	this.flock[i].obj.position.x += this.flock[i].velocity.x;	this.flock[i].obj.position.y += this.flock[i].velocity.y;	this.flock[i].obj.position.z += this.flock[i].velocity.z;	var aim = new THREE.Vector3();	aim.copy(this.flock[i].obj.position).add(this.flock[i].velocity);	this.flock[i].obj.lookAt(aim);	this.flock[i].updateTrail();	}	}
}
var mat = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true})
var colors = [0xffd800, 0x1ae3ce, 0xff0084, 0xb4ff00]
var size = 2;
function boid(x, y, z) {	this.position = new THREE.Vector3(x, y, z);	this.maxSpeed = Math.random() + .5;	this.velocity = (new THREE.Vector3(Math.random()*2 - 1, Math.random()*2 - 1, Math.random()*2 - 1)).normalize();	this.holdVel = this.velocity;	this.obj = new THREE.Group();	this.obj.position.x = x;	this.obj.position.y = y;	this.obj.position.z = z;	this.trail = [];	this.trailLength = Math.floor(Math.random() * 50) + 10;	var obj = new THREE.Mesh(new THREE.IcosahedronGeometry(1, 1), new THREE.MeshBasicMaterial({color: colors[Math.floor(Math.random() * colors.length)]}));	obj.receiveShadow = true;	obj.castShadow = true;	for(var i = 0; i < this.trailLength; i++){	var newTrail = obj.clone();	var scale = (this.trailLength - i)/(this.trailLength/2);	newTrail.scale.set(scale, scale, scale);	this.trail.push(newTrail);	scene.add(newTrail);	}	this.updateTrail = function(){	for(var i = this.trail.length - 1; i > 0; i--){	this.trail[i].position.set(this.trail[i - 1].position.x, this.trail[i - 1].position.y, this.trail[i - 1].position.z);	}	this.trail[0].position.set(this.obj.position.x, this.obj.position.y, this.obj.position.z);	}
}
Boids ii - Script Codes
Boids ii - Script Codes
Home Page Home
Developer Khalkeus
Username khalkeus
Uploaded November 11, 2022
Rating 3
Size 3,358 Kb
Views 10,120
Do you need developer help for Boids ii?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Khalkeus (khalkeus) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!