[英]Realistic Bounce of Ball (off Line) in Coordinate Grid
問題
所以我正在嘗試制作一個程序,通過計算球與線形成的角度——如果它是一條相交線——並圍繞交點旋轉那條線來找到一個球從一條線上反彈的程序新的斜坡。 除了我將坡度返回給球動量的部分之外,我擁有所有algorithms
和公式。 所有計算的最終產品(我已經確定有效)是線和交點或反彈點的斜率。 如果我只有一個斜坡,我怎么能知道彈跳后球會在斜坡上移動的方向?
旁注:我正在使用的語言是帶有一些任意外部graphics library
Java 1.8
,但我不是在尋找代碼來插入我預先存在的代碼,我正在尋找您認為我可能能夠做到的一般概念做。 此外,問題的關鍵在於,整個項目都是基於坐標的。
非常感謝您的任何輸入或可能的答案,並詢問我是否需要有關該問題的規范!
https://cwynn.com/ball-bounce/是我幾年前制作的一個 html5 畫布小東西,它說明了 op 如何處理“反彈”
你有你的球 B,它將在碰撞點 C 上擊中一條線 L。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);
}
決定加個圖。。。
唯一“真實”的東西是球和棕線
“球”是粉紅色的,朝向射線“路徑”中心的“接觸”點,
投影是球穿過接觸點的反射,反射是投影點穿過直線的反射。
一旦球接觸到棕色線,它的方向向量應該從“路徑”變為“新路徑”(位於接觸和反射上的線)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.