<span style="font-size:18px;">void CBounding::Read_coordinate(LPCSTR filename,D3DXVECTOR3 model_pos,D3DXVECTOR3 fScall)//从X文件读取底面的坐标
{
<span style="color:#009900;">/* c语言
FILE *pFile=fopen(filename,"rb");
//获取文件大小
fseek(pFile,0,SEEK_END);
long lSize=ftell(pFile);
rewind(pFile);
//分配内存存储整个文件
char *buffer=(char *)malloc(sizeof(char)*lSize);
//将文件拷贝到buffer中
size_t result=fread(buffer,1,lSize,pFile);
//查找特征字符串
fclose(pFile);
string strbuffer=buffer;
free(buffer);*/自己可以尝试用C语言</span>
<span style="color:#FF0000;">//c++实现</span>
ifstream filestr;
long size=0;
filestr.open(filename,ios::binary);
<span style="color:#006600;">//获取filestr对应buffer对象的指针</span>
filebuf *pbuf=filestr.rdbuf();
<span style="color:#33CC00;">//调用buffer对象方法获取文件大小</span>
size=pbuf->pubseekoff(0,ios::end,ios::in);
pbuf->pubseekpos(0,ios::in);
<span style="color:#33FF33;">//分配到内存空间</span>
char *buffer=new char[size];
<span style="color:#33CC00;">//获取文件内容</span>
pbuf->sgetn(buffer,size);
filestr.close();
string strbuffer=buffer;
delete []buffer;
string featurestr="Frame Rectangle221";
<span style="color:#33CC00;">//查找,返回字符串位置 </span>
int paraloc=strbuffer.find(featurestr);
char str[1000];
//找到了的话
if(paraloc!=-1)
{
<span style="color:#33CC00;"> //跳过特征字符串,提取1000位 </span>
strbuffer.copy(str,1000,paraloc+featurestr.length());
}
char L_UX[20],L_UY[20],L_UZ[20],L_DX[20],L_DY[20],L_DZ[20],R_UX[20],R_UY[20],R_UZ[20],R_DX[20],R_DY[20],R_DZ[20];
char L_UX2[20],L_UY2[20],L_UZ2[20],L_DX2[20],L_DY2[20],L_DZ2[20],R_UX2[20],R_UY2[20],R_UZ2[20],R_DX2[20],R_DY2[20],R_DZ2[20];
int count=0,t3=0,t4=0,t5=0,t6=0,t7=0,t8=0,t9=0,t10=0,t11=0,t12=0,t13=0,t14=0;
for(int i=100;i<1000;i++)
{
if(str[i]==';')
{
count++;
}//右上角的坐标
if(count==3)
{
R_UX[t3]=str[i];
t3++;
}
else if(count==4)
{
R_UY[t4]=str[i];
t4++;
}
else if(count==5)
{
R_UZ[t5]=str[i];
t5++;
}
//左上角的坐标
else if(count==6)
{
L_UX[t6]=str[i];
t6++;
}
else if(count==7)
{
L_UY[t7]=str[i];
t7++;
}
else if(count==8)
{
L_UZ[t8]=str[i];
t8++;
}
//左下角的坐标
else if(count==9)
{
L_DX[t9]=str[i];
t9++;
}
else if(count==10)
{
L_DY[t10]=str[i];
t10++;
}
else if(count==11)
{
L_DZ[t11]=str[i];
t11++;
}
//右下角的坐标
else if(count==12)
{
R_DX[t12]=str[i];
t12++;
}
else if(count==13)
{
R_DY[t13]=str[i];
t13++;}
else if(count==14)
{
R_DZ[t14]=str[i];
t14++;
}
else if(count>14)
{ break; }
}
for(int i=0;i<16;i++)
{
R_UX2[i]=R_UX[i+4];//去掉分号换行和空格
R_UY2[i]=R_UY[i+1];
R_UZ2[i]=R_UZ[i+1];
L_UX2[i]=L_UX[i+5];//去掉分号,逗号,换行符,两个空格
L_UY2[i]=L_UY[i+1];
L_UZ2[i]=L_UZ[i+1];
L_DX2[i]=L_DX[i+5];
L_DY2[i]=L_DY[i+1];
L_DZ2[i]=L_DZ[i+1];
R_DX2[i]=R_DX[i+5];
R_DY2[i]=R_DY[i+1];
R_DZ2[i]=R_DZ[i+1];
}
//将char *转化为float并赋值给向量
R_U.x=model_pos.x+fScall.x*atof(R_UX2); R_U.y=model_pos.y+fScall.y*atof(R_UY2); R_U.z=model_pos.z+fScall.z*atof(R_UZ2);
L_U.x=model_pos.x+fScall.x*atof(L_UX2); L_U.y=model_pos.y+fScall.y*atof(L_UY2); L_U.z=model_pos.z+fScall.z*atof(L_UZ2);
L_D.x=model_pos.x+fScall.x*atof(L_DX2); L_D.y=model_pos.y+fScall.y*atof(L_DY2); L_D.z=model_pos.z+fScall.z*atof(L_DZ2);
R_D.x=model_pos.x+fScall.x*atof(R_DX2); R_D.y=model_pos.y+fScall.y*atof(R_DY2); R_D.z=model_pos.z+fScall.z*atof(R_DZ2);
}
//<span style="color:#FF0000;">如果大神们还有其他方法,不妨拿出来分享一下!毕竟我是学渣</span>
bool CBounding::Check_Bounding(D3DXVECTOR3 point)//计算是否碰撞
{
if(point.z-(point.x*(R_U.z-L_U.z)-L_U.x*R_U.z+R_U.x*L_U.z)/(R_U.x-L_U.x)>0)
return false ;
if(point.z-(point.x*(R_D.z-R_U.z)-R_U.x*R_D.z+R_D.x*R_U.z)/(R_D.x-R_U.x)>0)
return false ;
if(point.z-(point.x*(L_D.z-R_D.z)-R_D.x*L_D.z+L_D.x*R_D.z)/(L_D.x-R_D.x)<0)
return false ;
if(point.z-(point.x*(L_D.z-L_U.z)-L_U.x*L_D.z+L_D.x*L_U.z)/(L_D.x-L_U.x)<0)
return false ;
return true;//在底面内部(即发生碰撞)
}</span>
关于解决从.x文件读取底面坐标来解决碰撞问题
最新推荐文章于 2019-11-26 14:44:37 发布