Re: [程式] 請問怎麼讓單位具有可視範圍?
看板GameDesign (遊戲設計)作者barroslee (很久沒寫Hspice 我早忘了)時間18年前 (2006/08/19 18:26)推噓0(0推 0噓 1→)留言1則, 1人參與討論串2/2 (看更多)
※ 引述《l314 (紅虫)》之銘言:
: 我希望我的單位(actor),都具有可視範圍,當其它單位進入視線時,會作出反應..
: 我的每個單位都是一個Object,可以擁有自己的屬性..
: 目前只知道需要視角和可視半徑,
: 像下面這張圖一樣,單位具有可視範圍.
: http://www.cs.nccu.edu.tw/~t9148/perception.JPG
: 請先進們提示小弟一些實作的技巧..
: 謝謝..
對每個 actor 算他們的 view frustum
算是基本功
Y
|
|
|-------X
/
/
/
Z
enum FrustumSide
{
RIGHT = 0,
LEFT,
BOTTOM,
TOP,
BACK,
FRONT
};
void CalculateFrustum()
{
vec4 frustum[6];
float proj[16]; // projection matrix of an actor
float modl[16]; // modelview matrix of an actor
float clip[16]; // clipping planes of an actor
// find projection matrix and modelview matrix
// .....
//
// opengl:
// glGetFloatv( GL_PROJECTION_MATRIX, proj );
// glGetFloatv( GL_MODELVIEW_MATRIX, modl );
// find clipping planes and view frustum
clip[ 0] = modl[ 0] * proj[ 0] +
modl[ 1] * proj[ 4] +
modl[ 2] * proj[ 8] +
modl[ 3] * proj[12];
clip[ 1] = modl[ 0] * proj[ 1] +
modl[ 1] * proj[ 5] +
modl[ 2] * proj[ 9] +
modl[ 3] * proj[13];
clip[ 2] = modl[ 0] * proj[ 2] +
modl[ 1] * proj[ 6] +
modl[ 2] * proj[10] +
modl[ 3] * proj[14];
clip[ 3] = modl[ 0] * proj[ 3] +
modl[ 1] * proj[ 7] +
modl[ 2] * proj[11] +
modl[ 3] * proj[15];
clip[ 4] = modl[ 4] * proj[ 0] +
modl[ 5] * proj[ 4] +
modl[ 6] * proj[ 8] +
modl[ 7] * proj[12];
clip[ 5] = modl[ 4] * proj[ 1] +
modl[ 5] * proj[ 5] +
modl[ 6] * proj[ 9] +
modl[ 7] * proj[13];
clip[ 6] = modl[ 4] * proj[ 2] +
modl[ 5] * proj[ 6] +
modl[ 6] * proj[10] +
modl[ 7] * proj[14];
clip[ 7] = modl[ 4] * proj[ 3] +
modl[ 5] * proj[ 7] +
modl[ 6] * proj[11] +
modl[ 7] * proj[15];
clip[ 8] = modl[ 8] * proj[ 0] +
modl[ 9] * proj[ 4] +
modl[10] * proj[ 8] +
modl[11] * proj[12];
clip[ 9] = modl[ 8] * proj[ 1] +
modl[ 9] * proj[ 5] +
modl[10] * proj[ 9] +
modl[11] * proj[13];
clip[10] = modl[ 8] * proj[ 2] +
modl[ 9] * proj[ 6] +
modl[10] * proj[10] +
modl[11] * proj[14];
clip[11] = modl[ 8] * proj[ 3] +
modl[ 9] * proj[ 7] +
modl[10] * proj[11] +
modl[11] * proj[15];
clip[12] = modl[12] * proj[ 0] +
modl[13] * proj[ 4] +
modl[14] * proj[ 8] +
modl[15] * proj[12];
clip[13] = modl[12] * proj[ 1] +
modl[13] * proj[ 5] +
modl[14] * proj[ 9] +
modl[15] * proj[13];
clip[14] = modl[12] * proj[ 2] +
modl[13] * proj[ 6] +
modl[14] * proj[10] +
modl[15] * proj[14];
clip[15] = modl[12] * proj[ 3] +
modl[13] * proj[ 7] +
modl[14] * proj[11] +
modl[15] * proj[15];
frustum[RIGHT].x = clip[ 3] - clip[ 0];
frustum[RIGHT].y = clip[ 7] - clip[ 4];
frustum[RIGHT].z = clip[11] - clip[ 8];
frustum[RIGHT].w = clip[15] - clip[12];
NormalizePlane(frustum, RIGHT);
frustum[LEFT].x = clip[ 3] + clip[ 0];
frustum[LEFT].y = clip[ 7] + clip[ 4];
frustum[LEFT].z = clip[11] + clip[ 8];
frustum[LEFT].w = clip[15] + clip[12];
NormalizePlane(m_Frustum, LEFT);
frustum[BOTTOM].x = clip[ 3] + clip[ 1];
frustum[BOTTOM].y = clip[ 7] + clip[ 5];
frustum[BOTTOM].z = clip[11] + clip[ 9];
frustum[BOTTOM].w = clip[15] + clip[13];
NormalizePlane(frustum, BOTTOM);
frustum[TOP].x = clip[ 3] - clip[ 1];
frustum[TOP].y = clip[ 7] - clip[ 5];
frustum[TOP].z = clip[11] - clip[ 9];
frustum[TOP].w = clip[15] - clip[13];
NormalizePlane(frustum, TOP);
frustum[BACK].x = clip[ 3] - clip[ 2];
frustum[BACK].y = clip[ 7] - clip[ 6];
frustum[BACK].z = clip[11] - clip[10];
frustum[BACK].w = clip[15] - clip[14];
NormalizePlane(frustum, BACK);
frustum[FRONT].x = clip[ 3] + clip[ 2];
frustum[FRONT].y = clip[ 7] + clip[ 6];
frustum[FRONT].z = clip[11] + clip[10];
frustum[FRONT].w = clip[15] + clip[14];
NormalizePlane(frustum, FRONT);
}
--
我的遊戲引擎圖
http://www.wretch.cc/album/album.php?id=barroslee&book=1
我的 blog
http://barroslee.blogspot.com/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.20.103
→
08/19 23:17, , 1F
08/19 23:17, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
GameDesign 近期熱門文章
PTT遊戲區 即時熱門文章