Jump to content
OGXbox.com

Meerjel01’s XTL Tutorials


Meerjel01
 Share

Recommended Posts

Know triangle collision? No? Here then!

static bool AABBTriangleSAT (D3DXVECTOR3 testAxis, Caabb box, D3DXVECTOR3 v0, D3DXVECTOR3 v1, D3DXVECTOR3 v2) {
	float p0, p1, p2;
	p0 = D3DXVec3Dot(&v0, &testAxis);
	p1 = D3DXVec3Dot(&v1, &testAxis);
	p2 = D3DXVec3Dot(&v2, &testAxis);


	float r = box.extents.x * abs(D3DXVec3Dot(&u0, &testAxis)) +
		box.extents.y * abs(D3DXVec3Dot(&u1, &testAxis)) +
		box.extents.z * abs(D3DXVec3Dot(&u2, &testAxis));

	return max(-largest(p0, p1, p2), smallest(p0, p1, p2)) > r;
}
static bool TriangleIntersectAABB(Caabb box, CTri triangle) {
	D3DXVECTOR3 v0 = triangle.p0;
	D3DXVECTOR3 v1 = triangle.p1;
	D3DXVECTOR3 v2 = triangle.p2;

	v0 -= box.center;
	v1 -= box.center;
	v2 -= box.center;

	D3DXVECTOR3 f0 = v1 - v0;
	D3DXVECTOR3 f1 = v2 - v1;
	D3DXVECTOR3 f2 = v0 - v2;

		

	D3DXVECTOR3 ax_u0_f0, ax_u0_f1, ax_u0_f2;
	D3DXVECTOR3 ax_u1_f0, ax_u1_f1, ax_u1_f2;
	D3DXVECTOR3 ax_u2_f0, ax_u2_f1, ax_u2_f2;

	D3DXVec3Cross(&ax_u0_f0, &u0, &f0);
	D3DXVec3Cross(&ax_u0_f1, &u0, &f1);
	D3DXVec3Cross(&ax_u0_f2, &u0, &f2);
	D3DXVec3Cross(&ax_u1_f0, &u1, &f0);
	D3DXVec3Cross(&ax_u1_f1, &u1, &f1);
	D3DXVec3Cross(&ax_u1_f2, &u1, &f2);
	D3DXVec3Cross(&ax_u2_f0, &u2, &f0);
	D3DXVec3Cross(&ax_u2_f1, &u2, &f1);
	D3DXVec3Cross(&ax_u2_f2, &u2, &f2);

	if(AABBTriangleSAT(ax_u0_f0, box, v0, v1, v2) || AABBTriangleSAT(ax_u0_f1, box, v0, v1, v2) || AABBTriangleSAT(ax_u0_f2, box, v0, v1, v2) ||
			AABBTriangleSAT(ax_u1_f0, box, v0, v1, v2) || AABBTriangleSAT(ax_u1_f1, box, v0, v1, v2) || AABBTriangleSAT(ax_u1_f2, box, v0, v1, v2) ||
			AABBTriangleSAT(ax_u2_f0, box, v0, v1, v2) || AABBTriangleSAT(ax_u2_f1, box, v0, v1, v2) || AABBTriangleSAT(ax_u2_f2, box, v0, v1, v2)) {
			return false;
		}

	D3DXVECTOR3 triNormal = D3DXVECTOR3(0,0,0);
	D3DXVec3Cross(&triNormal, &f0, &f1);

	if(AABBTriangleSAT(u0, box, v0, v1, v2) || AABBTriangleSAT(u1, box, v0, v1, v2) || AABBTriangleSAT(u2, box, v0, v1, v2)) {
		return false;
	}

	if(AABBTriangleSAT(triNormal, box, v0, v1, v2))
		return false;


	return true;
}

Collision response soon..

  • Like 1
Link to comment
Share on other sites

From my character collider code. Take a newDirection vector variable and use this for your triangles. (AABB character vs world triangle collision)

D3DXVECTOR3 TriangleCollision(CTri triangle, D3DXVECTOR3 dir, D3DXVECTOR3 lastPos, D3DXVECTOR3 currentPos) {
	D3DXVECTOR3 vNorm = GetNormal(triangle);
	colliding = false;

	triIsGround = false;
	triIsCeiling = false;
	triIsWall = false;

	if(CPyhsics::TriangleIntersectAABB(boundingBox, triangle)) {
	
		colliding = true;

		triIsGround = CPyhsics::GetGrounded(vNorm, D3DXVECTOR3(0, -1, 0), 0.5);
		triIsCeiling = CPyhsics::GetGrounded(vNorm, D3DXVECTOR3(0, 1, 0), 0.5);
		triIsWall = !triIsGround && !triIsCeiling;
		
		if(triIsGround)
			grounded = true;

		D3DXVECTOR3 tempV = NegateVector(vNorm);

		tempV *= D3DXVec3Length(&MultiplyVector(dir, vNorm));
		D3DXVECTOR3 wallDir = dir - tempV;

		D3DXVECTOR3 closestPtn = CPyhsics::GetClosestPointOnTriangle(currentPos, triangle);

		if(currentPos.y > closestPtn.y)
			return dir;

		D3DXVECTOR3 newPos = lastPos + wallDir;
		
		D3DXVECTOR3 newDir = newPos - lastPos;

		return newDir;
	}
	return dir;
}

Call it without using + or - btw.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

A tip for textures. Use dds files for everything cause they're the most built for DirectX applications and games. And even if they take up storage size, they're still optimized for the memory.

You might already know this but I felt like adding it here anyway.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

Board Life Status


Board startup date: April 23, 2017 12:45:48
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.