原文地址鏈接
這篇文章描述了使用JSR 184 API如何在3D空間里選中物體,例子中將用到的技術(shù)包括碰撞檢測和選擇一個物體。
下載源代碼
在演示如何在3D空間里選中一個物體之前,我們將五個立方體隨意的放在場景里,你可以用指針選取他們中的一個。

網(wǎng)格是由不同的x,y,z值鎖定,并可以被選擇和在之間移動。
public boolean pick(int scope,
float x,
float y,
Camera camera,
RayIntersection ri)
這個方法用于判斷是否選擇了一個Group對象。
射線從近切割面的x和y坐標射向相應(yīng)的遠切割面。x和y坐標的取值范圍應(yīng)該在(0,0)和(1,1)之間,(0,0)是指的左上頂點,相應(yīng)的(1,1)就是右下頂點。
被分割的網(wǎng)格的信息是由各射線相交點的參數(shù)所確定的。
為了使射線交叉點起作用,你應(yīng)該確保能所有結(jié)點被選中和激活:setPickingEnable(true);
下面是這個方法如何實現(xiàn)的:
PRivate void pick(){
RayIntersection ray = new RayIntersection();
float p1, p2;
// Normalize, the viewport should be within the range of (0,0) - (1,1)
p1 = (float)POINTER_X / (float)WIDTH;
p2 = (float)POINTER_Y / (float)HEIGHT;
boolean hit = group.pick(-1, p1, p2, camera, ray);
float x=0, y=0, z=0; // the intersection point
float [] fray = new float[6]; // The origin (ox oy oz) and direction (dx dy dz) of the pick ray.
RayIntersection ri = new RayIntersection();
// get the distance to the intersected part mesh.
if (hit)
{
ray.getRay(fray);
x = fray[0] + fray[3] * ray.getDistance();
y = fray[1] + fray[4] * ray.getDistance();
z = fray[2] + fray[5] * ray.getDistance();
}
}
注意:返回的距離不是到網(wǎng)格中點的距離,而是到分割網(wǎng)格部分的距離。
(出處:http://www.survivalescaperooms.com)
新聞熱點
疑難解答