射線與平面相交檢測程式

來源:互聯網
上載者:User

http://www.azure.com.cn/default.asp?cat=14&page=4參照此blog,寫了個射線與平面的相交檢測程式:

Ray類和Plane類:

#pragma once
#include <d3dx9.h>
#include "Plane.h"

class Ray
{
public:
 Ray(void);
 Ray(D3DXVECTOR3 RayOrig,D3DXVECTOR3 RayDir);
 ~Ray(void);

 bool GetHitStatus(Plane& plane);
  float GetHitDistance(Plane& plane);
private:
    D3DXVECTOR3 RayOrigin;
    D3DXVECTOR3 RayDirec;
 float dist;                           // ray的起點與交點的距離
 bool IsHit;
};

#include "Ray.h"
#include <iostream>
using namespace std;

Ray::Ray(void)
{
 RayOrigin.x = 0.0f;
 RayOrigin.y = 0.0f;
 RayOrigin.z = 0.0f;

 RayDirec.x = 1.0f;
 RayDirec.y = 1.0f;
 RayDirec.z = 1.0f;

    dist = 0.0f;
}

Ray::Ray(D3DXVECTOR3 RayOrig,D3DXVECTOR3 RayDir)
{
 RayOrigin = RayOrig;
 RayDirec = RayDir;
 dist = 0.0f;
}

Ray::~Ray(void)
{
}

bool Ray::GetHitStatus( Plane& plane)
{
 // if true , the ray is hitted or false the ray is not hitted
    D3DXVECTOR3 vNorPlane;
 vNorPlane.x = plane.a;
 vNorPlane.y = plane.b;
 vNorPlane.z = plane.c;
 float res = 0.0f;
 res =D3DXVec3Dot(&vNorPlane,&RayDirec);
 if(res <= 0.0f)
 {
           cout<<"It is hitted"<<endl;

     return true;
 }
 else
 {
         cout<<"It can not be hitted"<<endl;
   return false;
 }
}

float Ray::GetHitDistance(Plane& plane)
{
 float nom = 0.0f;
 float dist = 0.0f;
 float denom = 0.0f;
 // float nom = dotproduct(p.normal(), r.start()) + p.d();
 D3DXVECTOR3 vNormPlane;
 vNormPlane=plane.GetNormPlane();
 nom = D3DXVec3Dot(&vNormPlane,&RayOrigin)+plane.d;
 // float denom = dotproduct(p.normal(), r.direction());
 denom = D3DXVec3Dot(&vNormPlane,&RayDirec);
 dist = -(nom/denom);
 return dist;
}

 

//////////////////////////////

#pragma once
#include <d3dx9.h>
class Plane:public D3DXPLANE
{
public:
 Plane(void);
 Plane(float a, float b, float c,float d);
 ~Plane(void);
 D3DXVECTOR3 GetNormPlane();
private:
 D3DXVECTOR3 vNormPlane;    // 必須是單位
};

 

#include "Plane.h"
#include <math.h>
#include <iostream>
using namespace std;
Plane::Plane(void)
{

}

Plane::Plane(float a, float b, float c,float d):D3DXPLANE(a,b,c,d)
{

}

Plane::~Plane(void)
{
}
// Get the normal of the plane
D3DXVECTOR3 Plane::GetNormPlane()
{
 float length = sqrt(a*a+b*b+c*c);
 D3DXVECTOR3 v(a/length,b/length,c/length);
 cout<<v.x<<" "<<v.y<<" "<<v.z<<endl;
 return v;
}

 

#include "Ray.h"
#include "Plane.h"
#include <d3dx9.h>
#include <iostream>
using namespace std;

int main()
{
 Plane  plane(-1.0f,-1.0f,-1.0f,1.0f);
 D3DXVECTOR3 vOri(0.0f,0.0f,0.0f);
 D3DXVECTOR3 vDir(0.0f,1.0f,0.0f);
    Ray *ray = new Ray(vOri,vDir);
 ray->GetHitStatus(plane);
 float dist = ray->GetHitDistance(plane);
 cout<<"ray起點與其交點的距離為:"<<dist<<endl;
    cin.get();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.