Created
September 3, 2013 10:18
-
-
Save ma6174/6422094 to your computer and use it in GitHub Desktop.
小球在引力的作用下自由运动
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var ballList = []; | |
| function createBall(){ | |
| var ball = document.createElement("div"); | |
| var r = 10+Math.random()*100; | |
| var density = 0.01; // 密度 | |
| ball.redis = r/2; // 半径 | |
| ball.name = "ball"+ballList.length; | |
| ball.velocity = {"x":0,"y":0}; // 速度 | |
| ball.acceleration = {"x":0,"y":0}; // 加速度 | |
| ball.mess = 4.0/3*Math.PI*ball.redis*ball.redis*ball.redis*density; // 质量 | |
| with(ball.style){ | |
| width = r+"px"; | |
| height = r+"px"; | |
| background="rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")"; | |
| opacity = 0.8; | |
| position = "absolute"; | |
| webkitBorderRadius = r/2.0+"px"; | |
| mozBorderRadius = r/2.0+"px"; | |
| top = window.screen.availHeight/2+window.screen.availHeight/2*(Math.random()-0.5)+"px"; | |
| left = window.screen.availWidth/2 + window.screen.availWidth/2*(Math.random()-0.5)+"px"; | |
| } | |
| ball.moveTo = function(x,y){ | |
| this.style.top = y-ball.redis+"px"; | |
| this.style.left = x-ball.redis+"px"; | |
| }; | |
| ball.getPosition = function(){ | |
| return { | |
| "x":Number(ball.style.left.slice(0,-2))+ball.redis, | |
| "y":Number(ball.style.top.slice(0,-2))+ball.redis | |
| }; | |
| }; | |
| ball.getForce = function(){ | |
| return { | |
| "x":this.acceleration.x * this.mess, | |
| "y":this.acceleration.y * this.mess | |
| }; | |
| }; | |
| ball.updateVelocity = function(force){ | |
| this.acceleration.x = force.x/this.mess; | |
| this.acceleration.y = force.y/this.mess; | |
| this.velocity.x = this.velocity.x + this.acceleration.x * 100/1000; | |
| this.velocity.y = this.velocity.y + this.acceleration.y * 100/1000; | |
| } | |
| return ball; | |
| } | |
| // 判断重合 | |
| function judgeCoincide(ball1,ball2){ | |
| if(ball1.redis + ball2.redis - calcDistance(ball1,ball2) > 0){ | |
| return true; | |
| } | |
| return false; | |
| } | |
| // 计算两球距离 | |
| function calcDistance(ball1,ball2){ | |
| var p1 = ball1.getPosition(); | |
| var p2 = ball2.getPosition(); | |
| return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); | |
| } | |
| // 计算两球引力 | |
| function calcGravitation(ball1,ball2){ | |
| if(judgeCoincide(ball1,ball2)===true){ | |
| return 0; | |
| } | |
| var G = 10; // 万有引力常量 | |
| var distance = calcDistance(ball1,ball2); | |
| return G*ball1.mess*ball2.mess/distance/distance; | |
| } | |
| // 求单位向量 | |
| function calcUnitVector(ball1,ball2){ | |
| var distance = calcDistance(ball1,ball2); | |
| p1 = ball1.getPosition(); | |
| p2 = ball2.getPosition(); | |
| return { | |
| "x":(p2.x-p1.x)/distance, | |
| "y":(p2.y-p1.y)/distance | |
| } | |
| } | |
| //计算小球引力合力 | |
| function calcAllForce(ball){ | |
| var currentForce = {"x":0,"y":0}; | |
| for(var i in ballList){ | |
| if(ball.name == ballList[i].name){ | |
| continue | |
| } | |
| var force = calcGravitation(ball,ballList[i]); | |
| var vector = calcUnitVector(ball,ballList[i]); | |
| currentForce.x += force * vector.x; | |
| currentForce.y += force * vector.y; | |
| } | |
| return currentForce; | |
| } | |
| function nextPosition(ball){ | |
| var force = calcAllForce(ball); | |
| var currentPosition = ball.getPosition() | |
| ball.moveTo( | |
| currentPosition.x + ball.velocity.x * 100/1000, | |
| currentPosition.y + ball.velocity.y * 100/1000 | |
| ); | |
| ball.updateVelocity(force); | |
| } | |
| function moveAll(){ | |
| for(var i in ballList){ | |
| nextPosition(ballList[i]); | |
| } | |
| } | |
| window.onload = function(){ | |
| //创建小球 | |
| for(var i=0; i<10; i++){ | |
| var ball = createBall(); | |
| ballList.push(ball); | |
| document.body.appendChild(ball); | |
| } | |
| // setInterval(moveAll,10); | |
| setTimeout(function(){ | |
| moveAll(); | |
| setTimeout(arguments.callee,10); | |
| },10) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment