Ball Physics

Size
3,151 Kb
Views
56,672

How do I make an ball physics?

Bouncing, colliding rubber balls.. What is a ball physics? How do you make a ball physics? This script and codes were developed by Well Caffeinated on 08 August 2022, Monday.

Ball Physics Previews

Ball Physics - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Ball Physics</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="c"></canvas> <script src="js/index.js"></script>
</body>
</html>

Ball Physics - Script Codes CSS Codes

* { padding:0; margin:0;
}
canvas { width:100%; height:100%;
}

Ball Physics - Script Codes JS Codes

/*
By Adam a coepen (http://codepen.io/stuffit) 2012
Rubber bouncing balls.
Still a work in progress.
Reduce BALL_NUMBER if it runs slowly.
*/
var balls = function() { /* configuration */ var BALL_NUMBER = 30; var BALL_SIZE = 60; var BALL_COLOR = "#70CDFF"; var BACK_COLOR = "#f9f9f9"; var GRAVITY_X = 0; var GRAVITY_Y = 0.5; var MOUSE_REPEL = false; var MOUSE_STRENGTH = 1.4; var BOUNCINESS = 0.8; var FRICTION = 1.01; var WALL_STICK = false; var WALL_BOUNCE = true; var STICKINESS = 0; window.requestAnimFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); }; })(); var pixels, mouseX, mouseY, mouseDown = false, balls = [], canvas, context, width, height;	var loop = function() {	pixels = new Array(width * height); context.fillStyle = BACK_COLOR; context.fillRect(0,0,width,height);	for (var i = 0; i < BALL_NUMBER; i++) {	balls[i].tick();	}	requestAnimFrame(loop);	};	var Ball = function(radius) {	this.x	= Math.random() * width;	this.y	= Math.random() * height;	this.fx = 0;	this.fy = 0;	this.dx = 0;	this.dy = 0;	};	Ball.prototype.tick = function() {	if (mouseDown) {	var dx = this.x - mouseX;	var dy = this.y - mouseY;	var d = Math.sqrt(dx * dx + dy * dy)/(MOUSE_STRENGTH);	if (d < BALL_SIZE * 4) { if(MOUSE_REPEL) { this.dx += dx / d; this.dy += dy / d; } else { this.dx -= dx / d; this.dy -= dy / d; }	}	}	this.fy += GRAVITY_Y;	this.fx += GRAVITY_X;	this.x += this.fx;	this.y += this.fy;	if (this.x < BALL_SIZE * .5) { if(WALL_STICK) { this.x = BALL_SIZE * .5; this.fy /= FRICTION*STICKINESS; } else if(WALL_BOUNCE) { this.x = BALL_SIZE * .5; this.fx = this.fx * (-1) * BOUNCINESS; this.fy /= FRICTION; } else { this.dx += (BALL_SIZE * .5 - this.x); }	}	else if (this.x > width - BALL_SIZE * .5) { if(WALL_STICK) { this.x = width - BALL_SIZE * .5; this.fy /= FRICTION*STICKINESS; } else if(WALL_BOUNCE) { this.x = width - BALL_SIZE * .5; this.fx = this.fx * (-1) * BOUNCINESS; this.fy /= FRICTION; } else { this.dx -= (this.x - width + BALL_SIZE * .5); }	}	if (this.y < BALL_SIZE * .5) { if(WALL_STICK) { this.y = BALL_SIZE * .5; this.fx /= FRICTION*STICKINESS; } else if(WALL_BOUNCE) { this.y = BALL_SIZE * .5; this.fy = this.fy * (-1) * BOUNCINESS; this.fx /= FRICTION; } else { this.dy += (BALL_SIZE * .5 - this.y); }	}	else if (this.y > height - BALL_SIZE * .5) { if(WALL_STICK) { this.y = height - BALL_SIZE * .5; this.fx /= FRICTION*STICKINESS; } else if(WALL_BOUNCE) { this.y = height - BALL_SIZE * .5; this.fy = this.fy * (-1) * BOUNCINESS; this.fx /= FRICTION; } else { this.dy -= (this.y - height + BALL_SIZE * .5); }	}	var inwidth = Math.round(this.x / BALL_SIZE);	var inheight = Math.round(this.y / BALL_SIZE);	for (var a = inwidth - 1; a <= inwidth + 1; a++) {	for (var b = inheight - 1; b <= inheight + 1; b++) {	var g = pixels[b * width + a] || [];	for (j = 0, l = g.length; j < l; j++) {	var that = g[j];	var dx = that.x - this.x;	var dy = that.y - this.y;	var d = Math.sqrt(dx * dx + dy * dy);	if (d < BALL_SIZE && d > 0) {	dx = (dx / d) * (BALL_SIZE - d) * .25;	dy = (dy / d) * (BALL_SIZE - d) * .25;	this.dx -= dx;	this.dy -= dy;	that.dx += dx;	that.dy += dy;	}	}	}	}	if (!pixels[inheight * width + inwidth]) {	pixels[inheight * width + inwidth] = [this];	}	else {	pixels[inheight * width + inwidth].push(this);	}	this.x	+= this.dx;	this.y	+= this.dy;	this.fx += this.dx;	this.fy += this.dy;	this.dx = 0;	this.dy = 0;	context.beginPath();	context.fillStyle = BALL_COLOR;	context.arc(this.x, this.y, BALL_SIZE / 2, 0, Math.PI * 2, true);	context.closePath();	context.fill();	};	return {	init: function(canvas, w, h) { canvas = document.getElementById(canvas);	context = canvas.getContext('2d');	canvas.height = (h) ? h : window.innerHeight;	canvas.width = (w) ? w : window.innerWidth;	width = canvas.width;	height = canvas.height;	canvas.addEventListener('mousedown', function(e) {	if (e.preventDefault) e.preventDefault();	mouseDown = true;	return false;	}, false);	canvas.addEventListener('mouseup', function(e) {	mouseDown = false;	return false;	}, false);	canvas.addEventListener('mousemove', function(e) { if(e.offsetX) { mouseX = e.offsetX; mouseY = e.offsetY; } else if(e.layerX) { mouseX = e.layerX; mouseY = e.layerY; }	return false;	}, false);	window.onresize = function() {	init(c,w,h);	return false;	}	setTimeout(function() {	for (var i = 0; i < BALL_NUMBER; i++) {	balls[i] = new Ball(20);	}	loop();	}, 100);	}	};
}();
balls.init('c');
Ball Physics - Script Codes
Ball Physics - Script Codes
Home Page Home
Developer Well Caffeinated
Username wellcaffeinated
Uploaded August 08, 2022
Rating 3
Size 3,151 Kb
Views 56,672
Do you need developer help for Ball Physics?

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!

Well Caffeinated (wellcaffeinated) Script Codes
Create amazing marketing copy 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!