嗨,我正在使用this example为了让多个球(具有相同大小)在框架内弹跳。
问题是我需要改变它,使两个球碰撞后它们会弹回到随机方向,而不一定是“物理”方式。当然,球在任何时候都不能相互重叠。
很想听听一些关于如何在碰撞后创建随机运动的建议。
编辑
忘记提及,球应该始终保持相同的速度,因为我的情况下所有球都具有相同的半径,它们也具有相同的速度。所以简单地增加或减少 vx 和 vy 是不行的。
编辑2
在我看来,根据我的一些逻辑,球场应该是从球的接触点开始 180 度范围内的随机 Angular 。 问题是我不知道如何进行这些计算,获取每个球接触点并从那里获取范围
最佳答案
一种解决方案是改变碰撞后的作用 Angular
if ( CheckCollision(ball1, ball2) ) {
//change the angle of both the balls
console.log(ball1.aoa, ball2.aoa, "old");
var a1 = Math.floor((Math.random() * 10) + 1);//change the angle using random
ball1.aoa = Math.PI / a1;
var a2 = Math.floor((Math.random() * 10) + 1);//change the angle using random
ball2.aoa = Math.PI / a2;
console.log(ball1.aoa, ball2.aoa, "new");
工作代码here
关于javascript - d3 中的弹跳球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385522/