JS初练——弹弹球与墙壁碰撞处理实例

举报
nimo的小舔狗 发表于 2022/05/11 00:10:13 2022/05/11
【摘要】 最近在js学习中,发现关于碰撞反弹的问题,笔者觉得非常有趣,就浅浅了解了一下相关原理,如有不足,可以在评论区批评指正,碰撞处理中对于小球与其他形状的处理,主要取决于小球与其他正形状的相切问题(复杂图形笔者能力有限),本次案例就以小球与边界问题来做案例。 代码: <!DOCTYPE html><html...

最近在js学习中,发现关于碰撞反弹的问题,笔者觉得非常有趣,就浅浅了解了一下相关原理,如有不足,可以在评论区批评指正,碰撞处理中对于小球与其他形状的处理,主要取决于小球与其他正形状的相切问题(复杂图形笔者能力有限),本次案例就以小球与边界问题来做案例。

代码:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. #body {
  6. margin: 10px;
  7. border: 20px solid rgb(80, 38, 78);
  8. padding: 0px;
  9. width: 700px;
  10. height: 700px;
  11. background-color: rgb(60, 247, 255);
  12. }
  13. #box {
  14. margin: 0px;
  15. padding: 0px;
  16. width: 700px;
  17. height: 700px;
  18. overflow: hidden;
  19. background-color: rgb(254, 255, 67);
  20. float: left;
  21. }
  22. .ball {
  23. position: absolute;
  24. border: 1px solid black;
  25. border-radius: 50%;
  26. text-align: center;
  27. line-height: 20px;
  28. }
  29. </style>
  30. <title>弹弹球</title>
  31. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  32. <meta name="keywords" content="Machi">
  33. </head>
  34. <script>
  35. var timer;
  36. var colorList = ["#FFFF99", "#B5FF91", "#94DBFF", "#FFBAFF", "#FFBD9D", "#C7A3ED", "#CC9898", "#8AC007", "#CCC007"];
  37. var colorNum = colorList.length;
  38. var ballList = new Array();
  39. var timerList = new Array();
  40. class ball {
  41. constructor(_id, _r, _x, _y, _c, _vx, _vy) {
  42. this.id = _id;
  43. this.r = _r;
  44. this.x = _x;
  45. this.y = _y;
  46. this.c = _c;
  47. this.vx = _vx;
  48. this.vy = _vy;
  49. }
  50. addBall(obj) {
  51. var ball = document.createElement("div");
  52. ball.className = "ball";
  53. ball.id = this.id;
  54. ball.style.height = this.r + "px";
  55. ball.style.width = this.r + "px";
  56. ball.style.left = this.x + "px";
  57. ball.style.top = this.y + "px";
  58. ball.style.backgroundColor = this.c;
  59. //ball.textContent = ball.id;
  60. obj.appendChild(ball);
  61. }
  62. move() {
  63. var nextx = this.x + this.vx;
  64. var nexty = this.y + this.vy;
  65. if (nextx < 10 + 20 + 0 || nextx > 10 + 700 - this.r + 20)
  66. this.vx = -this.vx;
  67. if (nexty < 10 + 20 + 0 || nexty > 10 + 700 - this.r + 20)
  68. this.vy = -this.vy;
  69. this.x = this.x + this.vx;
  70. this.y = this.y + this.vy;
  71. this.vx = this.vx + iax + this.vx * ikx;
  72. this.vy = this.vy + iay + this.vy * iky;
  73. //console.log("new position "+this.x+"\t"+this.y+"\t\t"+"new velocity "+this.vx+"\t"+this.vy);
  74. var thisBall = document.getElementById(this.id);
  75. thisBall.style.left = this.x + "px";
  76. thisBall.style.top = this.y + "px";
  77. }
  78. }
  79. var ballCount = 0;
  80. //时间间隔
  81. var dt = 10;
  82. //重力场
  83. var iax = 0,
  84. iay = 0.05;
  85. //阻力系数
  86. var ikx = 0,
  87. iky = 0;
  88. function createBall(obj) {
  89. var e = event || window.event;
  90. div_x = e.clientX;
  91. div_y = e.clientY;
  92. ballCount++;
  93. _id = "ball" + ballCount;
  94. _r = parseInt(Math.random() * 50 + 25);
  95. _x = div_x - _r / 2;
  96. _y = div_y - _r / 2;
  97. _c = colorList[parseInt(Math.random() * colorNum)];
  98. _theta = parseInt(Math.random() * 180);
  99. _v = 5;
  100. _vx = _v * Math.cos(_theta);
  101. _vy = _v * Math.sin(_theta);
  102. ballList[ballCount] = new ball(_id, _r, _x, _y, _c, _vx, _vy);
  103. console.log("ball created " + ballList[ballCount]);
  104. ballList[ballCount].addBall(obj);
  105. console.log(ballList);
  106. }
  107. function moving() {
  108. for(i=1;i<ballList.length;i++){
  109. ballList[i].move();
  110. }
  111. }
  112. function keepMoving() {
  113. clearInterval(timer);
  114. timer = setInterval("moving()", dt);
  115. }
  116. </script>
  117. <body id="body" onclick="keepMoving()">
  118. <div id="box" onclick="createBall(this)">
  119. </div>
  120. </body>

文章来源: blog.csdn.net,作者:渣渣ye,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yyfloveqcw/article/details/123888911

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。