collision.cpp

来自「游戏开发人工智能技术 很好的一本书 讲解游戏中的人工智能技术 希望大家喜欢 」· C++ 代码 · 共 50 行

CPP
50
字号
#include "collision.h"



//--------------------2LinesIntersection2D-------------------------
//
//	Given 2 lines in 2D space AB, CD this returns true if an 
//	intersection occurs and sets dist to the distance the intersection
//  occurs along AB
//
//----------------------------------------------------------------- 
bool LineIntersection2D(const SPoint A,
                        const SPoint B,
                        const SPoint C, 
                        const SPoint D,
                        double &dist)
{

  double rTop = (A.y-C.y)*(D.x-C.x)-(A.x-C.x)*(D.y-C.y);
	double rBot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);

	double sTop = (A.y-C.y)*(B.x-A.x)-(A.x-C.x)*(B.y-A.y);
	double sBot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);

	if ( (rBot == 0) || (sBot == 0))
	{
		//lines are parallel
		return false;
	}

	double r = rTop/rBot;
	double s = sTop/sBot;

	if( (r > 0) && (r < 1.0f) && (s > 0) && (s < 1.0f) )
  {
  	dist = r;

    return true;
  }

	else
  {
		dist = 0;

    return false;
  }
}

	
		

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?