コード例 #1
0
bool CCollision::SPHEREtoCAPSULE (CSphere* const sphere, CCapsule* const capsule)
{
	XMFLOAT3 closest;

	if(sphere->GetPos().x < capsule->GetTop().x)
		closest.x = capsule->GetTop().x;
	else if(sphere->GetPos().x > capsule->GetBottom().x)
		closest.x = capsule->GetBottom().x;
	else
		closest.x = sphere->GetPos().x;

	if(sphere->GetPos().y > capsule->GetTop().y)
		closest.y = capsule->GetTop().y;
	else if(sphere->GetPos().y < capsule->GetBottom().y)
		closest.y = capsule->GetBottom().y;
	else
		closest.y = sphere->GetPos().y;

	
	if(sphere->GetPos().z < capsule->GetTop().z)
		closest.z = capsule->GetTop().z;
	else if(sphere->GetPos().z > capsule->GetBottom().z)
		closest.z = capsule->GetBottom().z;
	else
		closest.z = sphere->GetPos().z;

	CSphere temp;
	temp.SetPos(closest);
	temp.SetRad(capsule->GetRad());

	return SPHEREtoSPHERE(&temp, sphere);
}
コード例 #2
0
bool CCollision::AABBtoCapsule( CAABB* const aabb, CCapsule* const capsule )
{
	XMFLOAT3 abCenter;
	abCenter.x = (aabb->GetMin().x + aabb->GetMax().x) * 0.5f;
	abCenter.y = (aabb->GetMin().y + aabb->GetMax().y) * 0.5f;
	abCenter.z = (aabb->GetMin().z + aabb->GetMax().z) * 0.5f;

	XMFLOAT3 L;
	L.x = capsule->GetTop().x - capsule->GetBottom().x;
	L.y = capsule->GetTop().y - capsule->GetBottom().y;
	L.z = capsule->GetTop().z - capsule->GetBottom().z;
	XMFLOAT3 N;
	XMStoreFloat3(&N, XMVector3Normalize(XMLoadFloat3(&L)));

	XMFLOAT3 vec; 
	vec.x = abCenter.x - capsule->GetBottom().x;
	vec.y = abCenter.y - capsule->GetBottom().y;
	vec.z = abCenter.z - capsule->GetBottom().z;

	float d = XMVector3Dot(XMLoadFloat3(&N), XMLoadFloat3(&vec)).m128_f32[0];

	XMFLOAT3 closest;
	if(d < 0)
	{
		closest = capsule->GetBottom();
	}
	else if(d > XMVector3Length(XMLoadFloat3(&L)).m128_f32[0])
	{
		closest = capsule->GetTop();
	}
	else
	{
		XMFLOAT3 dis = N;
		dis.x *= d;
		dis.y *= d;
		dis.z *= d;

		closest.x = capsule->GetBottom().x + dis.x;
		closest.y = capsule->GetBottom().y + dis.y;
		closest.z = capsule->GetBottom().z + dis.z;
	}

	CSphere temp;
	temp.SetPos(closest);
	temp.SetRad(capsule->GetRad());

	return AABBtoSPHERE(aabb, &temp);
}