ฉันกำลังพยายามเขียนการจำลองทางฟิสิกส์ง่ายๆ โดยที่ลูกบอลที่มีรัศมีและมวลต่างกันจะกระเด้งไปรอบๆ ในสภาพแวดล้อมที่ยืดหยุ่นและไม่มีแรงเสียดทานอย่างสมบูรณ์แบบ ฉันเขียนโค้ดของตัวเองตามแหล่งข้อมูลนี้: http://www.vobarian.com/collisions/2dcollisions2.pdf และฉันได้ทดสอบโค้ดจากที่นี่ด้วย: Ball to Ball การชน - การตรวจจับและการจัดการ
แก้ไขคำถามแล้ว
ด้วยความช่วยเหลือจาก Rick Goldstein และ Ralph ฉันได้โค้ดที่ใช้งานได้ (มีการพิมพ์ผิด...) ขอบคุณมากสำหรับความช่วยเหลือของคุณ อย่างไรก็ตาม ฉันยังคงสับสนว่าเหตุใดอัลกอริทึมอื่นจึงไม่ทำงานสำหรับฉัน ลูกบอลกระดอนไปในทิศทางที่ถูกต้อง แต่พลังงานทั้งหมดของระบบไม่เคยได้รับการอนุรักษ์ไว้ ความเร็วจะเร็วขึ้นเรื่อยๆ จนกระทั่งลูกบอลเริ่มกะพริบในตำแหน่งคงที่บนหน้าจอ จริงๆ แล้วฉันต้องการใช้โค้ดนี้ในโปรแกรมของฉัน เพราะมันกระชับมากกว่าโค้ดที่ฉันเขียนมาก
นี่คืออัลกอริธึมการทำงานที่ฉันเขียน (แม้ว่าฉันจะใช้บิตแรกจากแหล่งอื่นนั้นก็ตาม) มันอยู่ในคลาส Bubble:
public void resolveCollision(Bubble b)
{
// get the minimum translation distance
Vector2 delta = (position.subtract(b.position));
float d = delta.getMagnitude();
// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta.multiply(((getRadius() + b.getRadius())-d)/d);
// resolve intersection --
// inverse mass quantities
float im1 = 1 / getMass();
float im2 = 1 / b.getMass();
// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1 / (im1 + im2)));
b.position = b.position.subtract(mtd.multiply(im2 / (im1 + im2)));
//get the unit normal and unit tanget vectors
Vector2 uN = b.position.subtract(this.position).normalize();
Vector2 uT = new Vector2(-uN.Y, uN.X);
//project ball 1 & 2 's velocities onto the collision axis
float v1n = uN.dot(this.velocity);
float v1t = uT.dot(this.velocity);
float v2n = uN.dot(b.velocity);
float v2t = uT.dot(b.velocity);
//calculate the post collision normal velocities (tangent velocities don't change)
float v1nPost = (v1n*(this.mass-b.mass) + 2*b.mass*v2n)/(this.mass+b.mass);
float v2nPost = (v2n*(b.mass-this.mass) + 2*this.mass*v1n)/(this.mass+b.mass);
//convert scalar velocities to vectors
Vector2 postV1N = uN.multiply(v1nPost);
Vector2 postV1T = uT.multiply(v1t);
Vector2 postV2N = uN.multiply(v2nPost);
Vector2 postV2T = uT.multiply(v2t);
//change the balls velocities
this.velocity = postV1N.add(postV1T);
b.velocity = postV2N.add(postV2T);
}
และนี่คืออันที่ไม่ได้ผล
public void resolveCollision(Bubble b)
{
// get the minimum translation distance
Vector2 delta = (position.subtract(b.position));
float d = delta.getMagnitude();
// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta.multiply(((getRadius() + b.getRadius())-d)/d);
// resolve intersection --
// inverse mass quantities
float im1 = 1 / getMass();
float im2 = 1 / b.getMass();
// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1 / (im1 + im2)));
b.position = b.position.subtract(mtd.multiply(im2 / (im1 + im2)));
// impact speed
Vector2 v = (this.velocity.subtract(b.velocity));
float vn = v.dot(mtd.normalize());
// sphere intersecting but moving away from each other already
if (vn > 0.0f) return;
// collision impulse (1f is the coefficient of restitution)
float i = (-(1.0f + 1f) * vn) / (im1 + im2);
Vector2 impulse = mtd.multiply(i);
// change in momentum
this.velocity = this.velocity.add(impulse.multiply(im1));
b.velocity = b.velocity.subtract(impulse.multiply(im2));
}
แจ้งให้เราทราบหากคุณพบสิ่งใด ขอบคุณ