线段 矩形 碰撞检测
template <class Real>
bool IntrLine2Box2<Real>::Clip (Real fDenom, Real fNumer, Real& rfT0,
Real& rfT1)
{
// Return value is 'true' if line segment intersects the current test
// plane. Otherwise 'false' is returned in which case the line segment
// is entirely clipped.
if (fDenom > (Real)0.0)
{
if (fNumer > fDenom*rfT1)
{
return false;
}
if (fNumer > fDenom*rfT0)
{
rfT0 = fNumer/fDenom;
}
return true;
}
else if (fDenom < (Real)0.0)
{
if (fNumer > fDenom*rfT0)
{
return false;
}
if (fNumer > fDenom*rfT1)
{
rfT1 = fNumer/fDenom;
}
return true;
}
else
{
return fNumer <= (Real)0.0;
}
}
//////////////////////////////////////////////////////////////////
template <class Real>
bool IntrLine2Box2<Real>::DoClipping (Real fT0, Real fT1,
const Vector2<Real>& rkOrigin, const Vector2<Real>& rkDirection,
const Box2<Real>& rkBox, bool bSolid, int& riQuantity,
Vector2<Real> akPoint[2], int& riIntrType)
{
assert(fT0 < fT1);
// convert linear component to box coordinates
Vector2<Real> kDiff = rkOrigin - rkBox.Center;
Vector2<Real> kBOrigin(
kDiff.Dot(rkBox.Axis[0]),
kDiff.Dot(rkBox.Axis[1])
);
Vector2<Real> kBDirection(
rkDirection.Dot(rkBox.Axis[0]),
rkDirection.Dot(rkBox.Axis[1])
);
Real fSaveT0 = fT0, fSaveT1 = fT1;
bool bNotAllClipped =
Clip(+kBDirection.X(),-kBOrigin.X()-rkBox.Extent[0],fT0,fT1) &&
Clip(-kBDirection.X(),+kBOrigin.X()-rkBox.Extent[0],fT0,fT1) &&
Clip(+kBDirection.Y(),-kBOrigin.Y()-rkBox.Extent[1],fT0,fT1) &&
Clip(-kBDirection.Y(),+kBOrigin.Y()-rkBox.Extent[1],fT0,fT1);
if (bNotAllClipped && (bSolid || fT0 != fSaveT0 || fT1 != fSaveT1))
{
if (fT1 > fT0)
{
riIntrType = IT_SEGMENT;
riQuantity = 2;
akPoint[0] = rkOrigin + fT0*rkDirection;
akPoint[1] = rkOrigin + fT1*rkDirection;
}
else
{
riIntrType = IT_POINT;
riQuantity = 1;
akPoint[0] = rkOrigin + fT0*rkDirection;
}
}
else
{
riIntrType = IT_EMPTY;
riQuantity = 0;
}
return riIntrType != IT_EMPTY;
}
调用DoClipping()即可获得交点坐标
代码来自开源引擎WildMagic4p9\GeometricTools\WildMagic4\LibFoundation\Intersection\Wm4IntrLine2Box2.cpp
天气
分类
标签
存档
- 2024年3月(1)
- 2024年2月(1)
- 2023年8月(1)
- 2023年7月(1)
- 2023年5月(1)
- 2022年9月(1)
- 2022年8月(1)
- 2022年1月(2)
- 2021年10月(1)
- 2021年7月(1)
- 2020年9月(1)
- 2020年8月(1)
- 2020年7月(1)
- 2020年6月(2)
- 2020年5月(1)
- 2019年10月(1)
- 2019年9月(2)
- 2019年7月(1)
- 2019年1月(4)
- 2018年12月(1)
- 2018年11月(1)
- 2018年10月(5)
- 2018年8月(2)
- 2018年7月(5)
- 2018年6月(2)
- 2018年4月(1)
- 2018年2月(1)
- 2017年12月(2)
- 2017年11月(1)
- 2017年10月(4)
- 2017年9月(3)
- 2017年8月(2)
- 2017年5月(2)
- 2017年4月(7)
- 2017年2月(1)
- 2016年12月(1)
- 2016年11月(2)
- 2016年10月(3)
- 2016年6月(2)
- 2016年3月(1)
- 2016年1月(2)
- 2015年12月(3)
- 2015年11月(3)
- 2015年10月(1)
- 2015年9月(1)
- 2015年8月(2)
- 2015年7月(2)
- 2015年5月(1)
- 2015年4月(1)
- 2015年2月(1)
- 2015年1月(1)
- 2014年12月(4)
- 2014年11月(1)
- 2014年10月(1)
- 2014年8月(4)
- 2014年7月(2)
- 2014年6月(1)
- 2014年2月(2)
- 2014年1月(2)
- 2013年12月(26)
- 2013年10月(2)

浙公网安备 33010602008237号