[DirectX9] Collision problem

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: [DirectX9] Collision problem

Postby Ripiz » Wed Jun 16, 2010 9:36 am

Could you please show how to do it? I didn't really understand.
Ripiz
 
Posts: 47
Joined: Sat Oct 03, 2009 12:07 pm

Re: [DirectX9] Collision problem

Postby Stucuk » Wed Jun 16, 2010 11:11 am

Code: Select all
Function ApplyMatrixToVec3(M : TMatrix4; V : TVec3) : TVec3;
begin
 Result[0] := ( V[0] * M[0,0] + V[1] * M[1,0] + V[2] * M[2,0] + M[3,0]);
 Result[1] := ( V[0] * M[0,1] + V[1] * M[1,1] + V[2] * M[2,1] + M[3,1]);
 Result[2] := ( V[0] * M[0,2] + V[1] * M[1,2] + V[2] * M[2,2] + M[3,2]);
end;


The above code apply's a matrix to a vertex (You should be able to convert it to C++ and DirectX). But id imagine that Direct X has a command simmiler to OpenGL's glMultiMatrix which apply's a matrix to the current world.

P.S Your still treating them as triangles which may work for most stuff, but there is bound to be instances when the polygon's are not Triangles(Such as a cylinder's top).
P.S2 Im glad i don't use DirectX, way to complex to render something which only takes about 2-5 lines in OpenGL.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: [DirectX9] Collision problem

Postby Julio Jerez » Wed Jun 16, 2010 11:24 am

I edit htis form teh debug display demo

#define MAX_DEBUG_LINES 2000
int debugLineCount;
dVector debugDisplayList[MAX_DEBUG_LINES][2];

Code: Select all
static void DebugSubmitGeometryCollision (void* userData, int vertexCount, const dFloat* faceVertec, int id)
{
   dVector p0 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
   for (int i = 0; (i < vertexCount) && (debugPointsCount < MAX_DEBUG_CONTACTS); i ++) {
      dVector p1 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
      debugDisplayList[debugLineCount][0] = p0;
      debugDisplayList[debugLineCount][1] = p1;
      debugLineCount ++;
      p0 = p1;
   }
}


static void DebugShowBodyCollision (const NewtonBody* body, void* userData)
{
   dMatrix matrix;
   NewtonBodyGetMatrix(body, &matrix[0][0]);
   NewtonCollisionForEachPolygonDo (NewtonBodyGetCollision(body), &matrix[0][0], DebugShowGeometryCollision, NULL);
}


void DebugRenderWorldCollision (const NewtonWorld* world)
{
   dVector aabbP0 (-1000.0f, -500.0f, -1000.0f);
   dVector aabbP1 (1000.0f, 500.0f, 1000.0f);

   debugLineCount = 0;
   NewtonWorldForEachBodyInAABBDo (world, &aabbP0[0], &aabbP1[0], DebugShowBodyCollision, NULL);
}


then in your render loop afte a newton update
you call


Code: Select all
...
DebugRenderWorldCollision (world)

// Render the array of line list usng d3d
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [DirectX9] Collision problem

Postby Ripiz » Wed Jun 16, 2010 1:16 pm

Stucuk wrote:
Code: Select all
Function ApplyMatrixToVec3(M : TMatrix4; V : TVec3) : TVec3;
begin
 Result[0] := ( V[0] * M[0,0] + V[1] * M[1,0] + V[2] * M[2,0] + M[3,0]);
 Result[1] := ( V[0] * M[0,1] + V[1] * M[1,1] + V[2] * M[2,1] + M[3,1]);
 Result[2] := ( V[0] * M[0,2] + V[1] * M[1,2] + V[2] * M[2,2] + M[3,2]);
end;


I think it should be like this then

Code: Select all
void SetTransformCallback (const NewtonBody* body, const float* matrix, int threadIndex){
   Model* ent;
   Matrix mx = matrix;
   ent = (Model*)NewtonBodyGetUserData(body);
   ent->vecPos.x = matrix[12];
   ent->vecPos.y = matrix[13];
   ent->vecPos.z = matrix[14];

   Vector3 newRot(
      ent->vecRot.x*mx._11 + ent->vecRot.y*mx._21 + ent->vecRot.z*mx._31 + mx._41,
      ent->vecRot.x*mx._12 + ent->vecRot.y*mx._22 + ent->vecRot.z*mx._32 + mx._42,
      ent->vecRot.x*mx._13 + ent->vecRot.y*mx._23 + ent->vecRot.z*mx._33 + mx._43);
   ent->vecRot = newRot;
}

I did it wrong, either these maths are wrong, because object is rotating like crazy.
Ripiz
 
Posts: 47
Joined: Sat Oct 03, 2009 12:07 pm

Re: [DirectX9] Collision problem

Postby Stucuk » Wed Jun 16, 2010 2:18 pm

You can't use it like that. You need to apply the matrix to Each vertex. You can't use it as angles to rotate the object.

You may(I have never used Direct X so i can't be sure) be able to use D3DXMatrixMultiply with the matrix newton gives you instead of manualy applying the matrix to each vertex.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: [DirectX9] Collision problem

Postby Carli » Wed Jun 16, 2010 2:24 pm

Ripiz wrote:I did it wrong, either these maths are wrong, because object is rotating like crazy.


DX is a left-handed system, GL a right-handed.
Try to swap each m_ab to m_ba (transpose the matrix)
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron