我正在编写一个 2d 物理引擎来取乐。
我为不同的形状编写了不同的类,BoxObject
是矩形,BallObject
是圆形和LineObject
直线。所有这些都实现了PhysicsObject
接口。
正在模拟的所有对象都位于一个列表中。
public static List<PhysicsObject> objects = new ArrayList<PhysicsObject>();
所有这些对象都需要检查是否相互碰撞。
Collision
类包含命名penetrationData
为获取所有可能类型对的穿透数据的方法,例如penetrationData(BoxObject a, BallObject b)
等penetrationData(BallObject a, LineObject b)
。这就是它的样子。
package com.optimus.game.physics;
public class Collision {
// Default - no collision
public static float[] penetrationData(PhysicsObject a, PhysicsObject b) {
return null;
}
// Line vs Circle
public static float[] penetrationData(LineObject line, BallObject ball) {
float distance = line.distance(ball.x, ball.y);
if (Math.abs(distance) > ball.radius) {
return null;
}
float penetration = ball.radius - Math.abs(distance);
float normalX = line.perpendicularX();
float normalY = line.perpendicularY();
return new float[] {normalX, normalY, penetration};
}
// Circle vs Line
public static float[] penetrationData(BallObject ball, LineObject line) {
float[] data = penetrationData(line, ball);
// reverse the normal
if (data != null) {
data[0] = -data[0];
data[1] = -data[1];
}
return data;
}
// Circle vs Circle
public static float[] penetrationData(BallObject a, BallObject b) {
// Vector from A to B
float normalX = b.x - a.x;
float normalY = b.y - a.y;
float penetration = 0;
// calculate the penetration and normal direction
// ...
// ... (code skipped)
return new float[] {normalX, normalY, penetration};
}
// Rect vs Rect
public static float[] penetrationData(BoxObject a, BoxObject b) {
// Vector from A to B
float normalX = b.x - a.x;
float normalY = b.y - a.y;
float penetration = 0;
// calculate the penetration and normal direction
// ...
// ... (code skipped)
return new float[] {normalX, normalY, penetration};
}
// Rect vs Circle
public static float[] penetrationData(BoxObject box, BallObject ball) {
// Vector from A to B
float normalX = ball.x - (box.x + box.width / 2f);
float normalY = ball.y - (box.y + box.height / 2f);
float penetration = 0;
// calculate the penetration and normal direction
// ...
// ... (code skipped)
return new float[] {normalX, normalY, penetration};
}
// Circle vs Rect
public static float[] penetrationData(BallObject ball, BoxObject box) {
float[] data = penetrationData(box, ball);
// reverse the normal
if (data != null) {
data[0] = -data[0];
data[1] = -data[1];
}
return data;
}
}
我检查碰撞像......
public static void checkAndResolveCollisions(List<PhysicsObject> objects) {
// check collision between all objects in a list
// list of objects can contain BallObject, LineObject and
// BoxObject all of which implement PhysicsObject
// loop over all possible pairs
for (int i = 0; i < objects.size(); i++) {
for (int j = i + 1; j < objects.size(); j++) {
// here I want that apt function is called based on type
// but objects.get(int i) always returns a PhysicsObject
float[] data = Collision.penetrationData(objects.get(i), objects.get(j));
// data = null implies no overlap
if (data == null) {
continue;
}
// Calculate relative velocity in terms of the normal direction
float normalX = data[0];
float normalY = data[1];
float penetration = data[2];
// Resolve the collision along the normal
// ...
// ... (code skipped)
}
}
}
但它不起作用并且每次都penetrationData
返回......null
如果我得到这样的渗透数据,但是这个代码很臭,我可以通过查看它来判断......
// here is what works, but I know is bad java code
float[] data = getAptPenetrationData(objects.get(i), objects.get(j));
getAptPenetrationData
类型转换然后调用 apt 函数的通用函数在哪里
// generic function that calls others
public static float[] getAptPenetrationData(PhysicsObject a, PhysicsObject b) {
// bad java code...
boolean aIsBall = a instanceof BallObject;
boolean aIsBox = a instanceof BoxObject;
boolean aIsLine = a instanceof LineObject;
boolean bIsBall = b instanceof BallObject;
boolean bIsBox = b instanceof BoxObject;
boolean bIsLine = b instanceof LineObject;
float[] data = null;
// circle vs circle
if (aIsBall && bIsBall) {
data = Collision.penetrationData((BallObject)a, (BallObject)b);
// box vs box
} else if (aIsBox && bIsBox) {
data = Collision.penetrationData((BoxObject)a, (BoxObject)b);
// box vs circle
} else if (aIsBox && bIsBall) {
data = Collision.penetrationData((BoxObject)a, (BallObject)b);
// circle vs box
} else if (aIsBall && bIsBox) {
data = Collision.penetrationData((BallObject)a, (BoxObject)b);
// circle vs line
} else if (aIsBall && bIsLine) {
data = Collision.penetrationData((BallObject)a, (LineObject)b);
// line vs circle
} else if (aIsLine && bIsBall) {
data = Collision.penetrationData((LineObject)a, (BallObject)b);
}
return data;
}
我一定错过了一些东西,并且必须有更好的方法来做我想做的事情。任何帮助表示赞赏。谢谢。
编辑:
根据 Zim-Zam 的建议,碰撞现在是这样解决的……
public static void checkAndResolveCollisions(List<PhysicsObject> objects) {
// check collision between all objects in a list
// list of objects can contain BallObject, LineObject and
// BoxObject all of which implement PhysicsObject
// loop over all possible pairs
for (int i = 0; i < objects.size(); i++) {
for (int j = i + 1; j < objects.size(); j++) {
// does not work, goes into infinite recursion
// still calls PhysicsObject.penetrationData(PhysicsObject)
float[] data = objects.get(i).penetrationData(objects.get(j));
// data = null implies no overlap
if (data == null) {
continue;
}
// Calculate relative velocity in terms of the normal direction
float normalX = data[0];
float normalY = data[1];
float penetration = data[2];
// Resolve the collision along the normal
// ...
// ... (code skipped)
}
}
}
但它只有在我将以下方法签名添加到PhysicsObject
接口时才有效
public PenetrationData penetrationData(PhysicsObject other);
public PenetrationData penetrationData(BallObject other);
public PenetrationData penetrationData(BoxObject other);
public PenetrationData penetrationData(LineObject other);
这仍然不是理想的,因为每次我实现一个新的PhysicsObject
我都必须更改接口。
PhysicsObject 实现(仅限penetrationData
函数) -
BallObject.java
public class BallObject implements PhysicsObject {
@Override
public float[] penetrationData(PhysicsObject other) {
return other.penetrationData(this);
}
@Override
public float[] penetrationData(LineObject line) {
return Collision.penetrationData(this, line);
}
@Override
public float[] penetrationData(BoxObject box) {
return Collision.penetrationData(this, box);
}
@Override
public float[] penetrationData(BallObject ball) {
return Collision.penetrationData(this, ball);
}
// ... rest of the code
}
BoxObject.java
public class BoxObject implements PhysicsObject {
@Override
public float[] penetrationData(PhysicsObject other) {
return other.penetrationData(this);
}
@Override
public float[] penetrationData(LineObject line) {
return null; // not implemented
}
@Override
public float[] penetrationData(BoxObject box) {
return Collision.penetrationData(this, box);
}
@Override
public float[] penetrationData(BallObject ball) {
return Collision.penetrationData(this, ball);
}
// ... rest of the code
}
LineObject.java
public class LineObject implements PhysicsObject {
@Override
public float[] penetrationData(PhysicsObject other) {
return other.penetrationData(this);
}
@Override
public float[] penetrationData(LineObject line) {
return null; // not implemented
}
@Override
public float[] penetrationData(BoxObject box) {
return null; // not implemented
}
@Override
public float[] penetrationData(BallObject ball) {
return Collision.penetrationData(this, ball);
}
// ... rest of the code
}