|
||||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
Objectcom.nttdocomo.ui.graphics3d.collision.Collision
衝突判定を行うクラスです。
同様の衝突判定機能に、DrawableObject3DクラスのisCrossメソッドがありますが、これはポリゴン
を使って正確に衝突判定を行うメソッドであり、判定の精度を重視したメソッドです。
一方、このクラスで提供するisHitメソッドは、単純な形状同士の衝突判定を行うAPIで、判定の精度
よりもパフォーマンスを重視した判定機能であり、isCrossメソッドとは性質が異なります。
パフォーマンスを重視するコンテンツでは、このクラスの機能を使用されることを推奨します。
このクラスには次の機能があります。
Ray
の交点情報を取得する(isPickedメソッド)
Ray
とFigureのPick判定
Shape
−Point
、Line
(Ray
)、Plane
、Triangle
、Box
、Capsule
、Cylinder
、Sphere
、AABBox
、AABCapsule
、AABCylinder
isHitメソッド、isPickedメソッドにおいて、Hitした時、Pickした時に通知が必要な場合(引数notifyが
trueの場合)は、CollisionObserver
インターフェースを実現したクラスのオブジェクトを、
setObserver
メソッドで設定しておく必要があります。
CollisionObserver
の各メソッドの呼び出しは、isHitメソッド、isPickedメソッド内部で
同期的に呼び出されます。
一次変換等の計算の結果、形状のいずれかの長さが0になって形状の次元数がさがる (例えば、LineがPointになる、SphereがPointになる等)場合の各メソッド (getDistance、getIntersection、isHit)の結果は保証されません。
以下にサンプルコードを示します。
public class sampleClass {
public static void main(String[] args) {
Figure fig = Figure.createInstance(....); // 何らかの3Dモデルをロードする
Collision collision = new Collision();
CollisionObserver co = new UserCollisionObserver();
collision.setObserver(co);
// 任意のBox作成
Box box0 = new Box(new Vector3D(10,10,10));
Box box1 = new Box(new Vector3D(10,10,10));
// Boxをワールド座標系にセット
box0.setTransform(null); // 恒等変換(設定しなくてもデフォルトは単位行列)
Transform trans = new Transform();
trans.translate(5, 20, 20);
box1.setTransform(trans); // 平行移動行列をセット
// Figureから、ボーンにBoundingVolumeオブジェクトが付加されていないBVFigureを生成
BVFigure bvFig = BVBuilder.createBVFigure(fig);
// Figure全体を包む球を生成
Sphere sphere = (Sphere)BVBuilder.createBV(fig, 0, Shape.TYPE_SPHERE, 1.0f);
// BVFigureに、Figure全体のBoundingVolumeをセット
bvFig.setBV(sphere);
// BVFigureをワールド座標系にセット
trans.setIdentity();
trans.translate(5, 20, 20);
bvFig.setTransform(trans); // 平行移動行列をセット
// パターン1. Box同士の衝突判定
// 衝突判定のみ処理(isHitの第3引数をfalseにする)
// (衝突しても、UserCollisionObserver.onHit()は呼ばれない)
if (collision.isHit(box0, box1, false)) {
// 衝突した場合の処理
} else {
// 衝突してない場合の処理
}
// パターン2. BoxとBVFigure(Figure全体のBoundingVolumeについて)の衝突判定
// 衝突判定時の情報を取得し複雑な処理を行いたい場合(isHitの第4引数をtrueにする)
// (衝突した場合、UserCollisionObserver.onHit()が呼ばれる)
if (collision.isHit(box0, bvFig, false, true)) {
// 衝突した場合の処理
} else {
// 衝突してない場合の処理
}
}
// 衝突判定時の情報を取得し,任意の処理を行う
static class UserCollisionObserver implements CollisionObserver {
public void onHit(Shape shape0, Shape shape1, boolean isInvolved, Vector3D point) {
// 使用しないため空実装
}
public boolean onHit(Shape shape, int boneId0, BoundingVolume[] bv, int[] boneId1,
boolean[] isInvolved, Vector3D[] point) {
// パターン2での衝突時,この処理が呼ばれる。
// bvFigには、Figure全体のBoundingVolumeしかセットしていないため、この処理
// が呼ばれる場合は、boneId1[0]=BVFigure.ID_WHOLE_FIGUREとなる。
//
// ユーザ任意の処理
//
return false; // 1回しか呼ばれないので、trueを返しても同じ
}
public void onHit(Shape shape, Sphere sphere, float contactPos,
Vector3D normal, float distance) {
// 使用しないため空実装
}
public void onPick(Ray ray, Figure fig, IntersectionAttribute[] attr) {
// 使用しないため空実装
}
}
}
コンストラクタの概要 | |
Collision()
Collisionオブジェクトを生成します。 |
メソッドの概要 | |
static float |
getDistance(Line line0,
Line line1)
Line (Ray )とLine (Ray )の最短距離を求めます。
|
static float |
getDistance(Point point,
Shape shape)
Point とShape との最短距離を求めます。
|
static float |
getDistance(Sphere sphere0,
Sphere sphere1)
Sphere とSphere の最短距離を求めます。
|
static Vector3D |
getIntersection(Ray ray,
Shape shape)
Ray とShape との交点を求めます。
|
boolean |
isHit(BVFigure bvFig0,
BVFigure bvFig1,
boolean isAllHit0,
boolean isAllHit1,
boolean notify)
BVFigure とBVFigure の衝突判定をします。
|
boolean |
isHit(Shape shape,
BVFigure bvFig,
boolean isAllHit,
boolean notify)
Shape とBVFigure の衝突判定をします。
|
boolean |
isHit(Shape shape0,
Shape shape1,
boolean notify)
Shape とShape の衝突判定をします。
|
boolean |
isHit(Shape shape,
Sphere sphere,
Vector3D pos,
boolean notify)
Shape とSphere の軌跡との衝突判定をします。
|
boolean |
isPicked(Ray ray,
Figure fig,
Transform trans,
boolean isAllPicked,
boolean notify)
Ray とFigureの交点情報を取得します。
|
void |
setObserver(CollisionObserver co)
CollisionObserver を設定します。
|
クラス Object から継承したメソッド |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
コンストラクタの詳細 |
public Collision()
Collisionオブジェクトを生成します。
メソッドの詳細 |
public void setObserver(CollisionObserver co)
CollisionObserver
を設定します。
isHitメソッド、isPickedメソッドにおいて、Hitした時、Pickした時に通知が必要な場合、
CollisionObserver
インターフェースを実現したクラスのオブジェクトを、このメソッドで
設定しておく必要があります。
co
- CollisionObserver
インターフェースを実現したクラスのオブジェクトを指定します。
nullの場合、設定されているCollisionObserver
オブジェクトが解除されます。
未設定の状態でnullが指定された場合、何もしません。public static float getDistance(setObserver
public void setObserver(