www.cwynn.com/bounce有點HTML5畫布件事我幾年前提出,說明運算是如何想處理「反彈」
你有你的球B,這將創下線L上的碰撞點C. B-> C表示一條線。在C之外的那條線上取一點,並在L上反射它以得到反射點R.當球碰到C時,可以刪除它的方向矢量,並給它一個C-> R的矢量。你需要重置他們的速度。因此,抓住方向矢量的大小,並縮放新的方向矢量以匹配。
編輯:決定添加的代碼(這讓我意識到,我忘了自己的縮放速度)
//closestCollPt[0] is the line the 'player' will hit next
var dist = player.position.dist(closestCollPt[0]);
//the 'player' in my case is a circle, so this indicates collision
if(dist < player.radius*2)
{
//the collision point to the reflection like I mentioned
//in the text above
var newDir = closestCollPt[0].vecTo(reflection);
//I break out their parts, b/c I want to scale the vector
//doable in one line, but hard to debug
var newDirX = newDir.x;
var newDirY = newDir.y;
var newDirDist = newDir.dist(new Vector(0,0));
//for whatever reason I was calling the size of a vector
//'dist' when I wrote this
var currDirDist = player.direction.dist(new Vector(0,0));
//give the player the direction we got from Coll->Ref
//scale it so their speed doesn't change
player.direction = newDir.scale(currDirDist).scale(1/newDirDist);
}
決定添加圖像...
唯一的「真正的」東西都是球將棕色線
在「球」是粉紅色的, 朝向上射線「路徑」中心「接觸」點爲首
投影是reflectio n是接觸點上的球,反射是線上投影點的反射。
一旦球接觸棕色線是方向矢量應該從「路徑」到「新路徑」(行上的接觸和反射坐)
在關閉的機會改變沒有人回答了這個您可以通過http://cwynn.com/bounce/查看我們的源代碼,該代碼使用您描述的方法。如果我通過電腦登錄時沒有回答,我會帶你回答您的任何問題。 –
我投票結束這個問題,因爲這是一個數學/物理問題。 –
這是一個話題。 @jake問如何以編程方式實現這一點。 – MLavrentyev