Ball physics
How do I make an ball physics?
I made a bungee rope and a few hundred balls bounce around and a ball on the end of bungee rope. It actually simulates/displays the physics of panning for gold. When panning for gold the non gold materials are less dense so they take up more space for how much they weigh and the gold sinks to the bottom of the pan when allowed to slide past the other pieces with water.. What is a ball physics? How do you make a ball physics? This script and codes were developed by Darby Rathbone on 03 October 2022, Monday.
Ball physics - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>ball physics</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>
Ball physics - Script Codes JS Codes
var Vector = (function () { function Vector(x, y) { this.x = x; this.y = y; this.prevmag = 1 }; return Vector
})();
Vector.prototype = { toString: function () { return "[" + this.x + "," + this.y + "]" }, mag: function () { return Math.sqrt((this.x * this.x) + (this.y * this.y)) }, mult: function (k) { this.x = this.x * k; this.y = this.y * k; return this.toString() }, div: function (k) { this.x = (this.x) / k; this.y = (this.y) / k; return this.toString() }, norm: function () { this.prevmag = this.mag(); this.div((this.mag())); return this.toString() }, add: function (a) { this.x = this.x + a.x; this.y = this.y + a.y; return this.toString() }, sub: function (a) { this.x = this.x - a.x; this.y = this.y - a.y; return this.toString() }, direction: function () { return Math.atan2(this.y, this.x) }, dot: function (a) { return (a.x * this.x) + (a.y * this.y) }
};
var Position = (function () { function Position(x, y) { this.x = x; this.y = y; this.velocity = new Vector(0, 0); this.xMax = 100; this.yMax = 100; this.radius = 10; this.accel = new Vector(0, 0) }; return Position
})(), Spring = (function () { function Spring(k, d, a) { this.end1 = null; this.end2 = null; this.k = k; this.d = d; this.len = a }; return Spring })();
Spring.prototype = { force: function () { return new Vector(this.k * (this.end1.x - this.end2.x - this.len), this.k * (this.end1.y - this.end2.y - this.len)) }, damper: function () { return (new Vector(this.d * (this.end1.velocity.x - this.end2.velocity.x), this.d * (this.end1.velocity.y - this.end2.velocity.y))) }, calculate: function (a, b) { this.end1 = a; this.end2 = b; var c = new Vector(a.x - b.x, a.y - b.y), distan = c.mag(), x = a.x - b.x, y = a.y - b.y; a.velocity.x += -(((x - (this.len / distan)) / this.k) + ((x - (this.len / distan)) / this.d)) / (2.0 * this.d); a.velocity.y += -(((y - (this.len / distan)) / this.k) + ((y - (this.len / distan)) / this.d)) / (2.0 * this.d); b.velocity.x += (((x - (this.len / distan)) / this.k) + ((x - (this.len / distan)) / this.d)) / (2.0 * this.d); b.velocity.y += (((y - (this.len / distan)) / this.k) + ((y - (this.len / distan)) / this.d)) / (2.0 * this.d); a.velocity.mult(0.98); b.velocity.mult(0.98); return true }, length: function () { return Math.sqrt(this.end1.dist(this.end2)) }
};
Position.prototype = { impact: function (point2) { var point1 = this, normal = point1.toPoint(point2), normal2 = point2.toPoint(point1), tangent = new Vector(-normal.y, normal.x), tangent2 = new Vector(-normal2.y, normal2.x); normal.norm(); normal2.norm(); tangent.norm(); tangent2.norm(); var midpoint = new Vector(((point1.x + point2.x) / 2.0), (point1.y + point2.y) / 2.0); point1.x = midpoint.x - normal.x * (this.radius + 0.5); point1.y = midpoint.y - normal.y * (this.radius + 0.5); point2.x = midpoint.x - normal2.x * (point2.radius); point2.y = midpoint.y - normal2.y * (point2.radius); var v1n = normal.dot(point1.velocity), v1t = tangent.dot(point1.velocity), v2n = normal2.dot(point2.velocity), v2t = tangent2.dot(point2.velocity); normal.mult(v2n); //normal.mult(point1.velocity.mag); normal2.mult(v1n); //normal2.mult(point2.velocity.mag); tangent.mult(v1t); //tangent.mult(point1.velocity.mag); tangent2.mult(v2t); //tangent2.mult(point2.velocity.mag); var newv1 = new Vector(-(normal.x) + (tangent.x), -(normal.y) + (tangent.y)); var newv2 = new Vector(-(normal2.x) + (tangent2.x), -(normal2.y) + (tangent2.y)); point1.setvelocity(newv1); point2.setvelocity(newv2); }, checkBounds:function(){ var maxx = this.xMax; var maxy = this.yMax; if (this.x < 0) { this.x = (this.x ) * -1; this.velocity.x *= -1; this.velocity.mult(.9); } if (this.x > maxx) { this.x = maxx - ((this.x - maxx) ); this.velocity.x *= -1; this.velocity.mult(.9); } if (this.y > maxy) { this.y = maxy - ((this.y - maxy) ); this.velocity.y *= -1; this.velocity.mult(.9); } if (this.y < 0) { this.y = (this.y ) * -1; this.velocity.y *= -1; this.velocity.mult(.9); } }, getvelocity: function () { if (this.type === "ball" || this.movableanchor === "false" || typeof this.anchor === 'undefined') { return new Vector(this.velocity.x, this.velocity.y) } else { var t = this.anchor.getvelocity(); t.add(this.velocity); return new Vector(t.x, t.y) } }, display: function (c) { var a = c, p = this; a.beginPath(); var x = p.x|0, y = p.y|0, rx = this.radius, start = 0, end = 2 * Math.PI, anticlockwise = true; if (this.spring !== null) { a.moveTo(this.spring.end1.x|0, this.spring.end1.y|0); a.lineTo(this.spring.end2.x|0, this.spring.end2.y|0) } else a.arc(x|0, y|0, rx, start, end, anticlockwise); a.fillStyle = this.color; a.stroke(); a.fill(); }, dist: function (p) { return ((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y)) }, step: function () { var vix= this.velocity.x, viy = this.velocity.y; this.velocity.x += this.accel.x*.1; this.velocity.y += this.accel.y*.1; this.x +=((vix*.1)+(this.velocity.x*.1))/2.0; this.y +=((viy*.1)+(this.velocity.y*.1))/2.0; }, toString: function () { return "[" + this.x + "," + this.y + "@" + this.velocity.toString + "]" }, toPoint: function (v) { return new Vector(-(this.x - v.x), -(this.y - v.y)) }, setvelocity: function (v) { this.velocity.x = v.x; this.velocity.y = v.y }
}
function displayp(p, q) { var a = can.getContext('2d'); a.beginPath(); var x = p.x|0, y = p.y|0, rx = 10, start = 0, end = 2 * Math.PI, anticlockwise = true; a.arc(x|0, y|0, rx, start, end, anticlockwise); a.stroke(); a.moveTo(x|0, y|0); a.lineTo((p.x + (p.velocity.x * 10.0))|0, (p.y + (p.velocity.y * 10.0))|0); a.stroke()
};
var bungiecount =8, can = document.createElement("canvas");
can.setAttribute("width", "500");
can.setAttribute("height", "500");
can.setAttribute("style", "transform: scale(0.70, 0.70);border-style:groove;border-width:4px;");
document.body.appendChild(can);
var balls = [];
for (var i = 0; i < bungiecount; i++) { balls.push(new Position(50, 50)); balls[balls.length - 1].xMax = can.width; balls[balls.length - 1].yMax = can.height; balls[balls.length - 1].velocity = new Vector(Math.random() - 0.5, Math.random() - 0.5); balls[balls.length - 1].velocity.mult(5.0); balls[balls.length - 1].radius = 20; balls[balls.length - 1].accel = new Vector(0, 10); balls[balls.length - 1].type = "pend"; balls[balls.length - 1].spring = new Spring(1, 1.5, 1.0); balls[balls.length - 1].color = "RGB(" + Math.round(Math.random() * 100) + "," + Math.round(Math.random() * 100) + "," + Math.round(Math.random() * 100) + ")"; if (i === 0) { balls[i].type = "ball"; balls[i].spring = null }
};
var ballcount = 200;
for (var i = 0; i < ballcount; i++) { balls.push(new Position(can.width * Math.random(), can.height * Math.random())); balls[balls.length - 1].velocity = new Vector(2 * Math.random() - 1, 2 * Math.random() - 1); balls[balls.length - 1].velocity.mult(2.0); balls[balls.length - 1].radius = 10; balls[balls.length - 1].xMax = can.width-balls[balls.length - 1].radius; balls[balls.length - 1].yMax = can.height-balls[balls.length - 1].radius; balls[balls.length - 1].accel = new Vector(0, 10); balls[balls.length - 1].type = "ball"; balls[balls.length - 1].spring = null
}
function repeater() { var c = can.getContext('2d'); c.clearRect(0, 0, can.width, can.height); for (var k = 0; k < balls.length; k++) { //balls[k].display(c); balls[k].step(); //balls[k].checkBounds(); //balls[k].display(c); balls[bungiecount-1].x = mx; balls[bungiecount-1].y = my; if (balls[k].type =="ball"){ balls[k].checkBounds();} } for (var k = 0; k < balls.length; k++) { //balls[k].display(c); if (balls[k].spring !==null ) { balls[k].spring.calculate(balls[k],balls[k-1]); }
if(balls[k].type =="ball")
{
for (var j = 0; j < ballcount; j++) { if(balls[j].type =="ball")
{ balls[k].checkBounds();balls[j].checkBounds(); if (k != j) { if (balls[k].dist(balls[j]) < (balls[k].radius+balls[j].radius)*(balls[k].radius+balls[j].radius)) { balls[k].impact(balls[j]); //balls[k].checkBounds();balls[j].checkBounds();
// balls[k].velocity.mult(.99); //balls[j].velocity.mult(.99); //balls[k].step(); //balls[j].step(); } } }
}
} //balls[k].checkBounds(); //balls[k].display(c); } for (var i = 0; i < balls.length; i++) { // balls[i].step(); balls[i].display(c); //balls[i].checkBounds();
balls[bungiecount-1].x = mx; balls[bungiecount-1].y = my; } requestAnimationFrame(repeater); };
var mx = 300, my = 300; requestAnimationFrame(repeater);
document.body.onmousemove = function mousemover(a) { mx = a.pageX; my = a.pageY
}
Developer | Darby Rathbone |
Username | blackkbot |
Uploaded | October 03, 2022 |
Rating | 3.5 |
Size | 3,874 Kb |
Views | 30,360 |
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!
Name | Size |
3d pure javascript 2d canvas | 2,667 Kb |
Sphere detail | 2,558 Kb |
Jigsaw puzzle | 5,193 Kb |
Pendulum | 3,104 Kb |
Webworker with an editable textbox | 3,525 Kb |
Sudoku grid | 2,042 Kb |
Remote image | 1,989 Kb |
JQuery Number Counter Plugin | 3,195 Kb |
3d css transformations | 2,652 Kb |
3d pure javascript 2d canvas | 2,600 Kb |
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!
Name | Username | Size |
Formations | Cantelope | 5,731 Kb |
Fullscreen Parallax | Bassta | 3,313 Kb |
Front in Aracaju Logo Pure CSS | Shankarcabus | 2,581 Kb |
Glitchy Text Effect | Kescoe | 2,208 Kb |
Exploding Text | Jjmartucci | 4,749 Kb |
Lunar eclipse | Ademilter | 2,056 Kb |
Particle Motion trajectories | Sniejadlik | 5,899 Kb |
A Pen by Patrick Cox | Pcridesagain | 2,899 Kb |
Pure CSS albums gallery | Renaudtertrais | 2,978 Kb |
Vertically rotating text with CSS | Nopr | 2,141 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!