## Compound objects

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

### Compound objects

Code: Select all
`NewtonCollision *newton_addcollision(ENTITY* entity, VECTOR* diff){   if(!entity) return;      NewtonCollision* collision;      int vertices = ent_vertices(entity);   float *vertexcloud = (float*)malloc(sizeof(float) * vertices * 3);      int i;   for(i = 0; i < vertices; i++)   {      VECTOR vertex; vec_for_mesh(&vertex, entity, i + 1);      vertexcloud[i * 3 + 0] = diff.x * QUANTTOMETER + (vertex.x) * entity->scale_x * QUANTTOMETER;      vertexcloud[i * 3 + 1] = diff.y * QUANTTOMETER + (vertex.y) * entity->scale_y * QUANTTOMETER;      vertexcloud[i * 3 + 2] = diff.z * QUANTTOMETER + (vertex.z) * entity->scale_z * QUANTTOMETER;   }      collision = NewtonCreateConvexHull(nworld, vertices, vertexcloud, 12, 0.01, NULL);   free(vertexcloud);      return collision;}`

next step is to collect all collision to one body
Code: Select all
`function AddToCompound(int comp, ENTITY* ent, float mass, int part, int total_parts){   if (part==0)   {       CompMainEnt=ent;      compoundParts[0] = newton_addcollision(me, nullvector);   }   else   {      compoundParts[part] = newton_addcollision(me, vector(my.x - CompMainEnt.x,my.y - CompMainEnt.y,my.z - CompMainEnt.z));   }      if (part==0) {      wait(1);            NewtonCollision* collision = NewtonCreateCompoundCollision (nworld, total_parts, compoundParts);      int i;      for (i = 0; i < total_parts; i++) { NewtonReleaseCollision (nworld, compoundParts[i]);   }            NewtonBody* body = NewtonCreateBody(nworld, collision);      float m[16]; ent_getmatrix_rb(my, m);      NewtonBodySetMatrix(body, m);            my.skill99 = body; // save body pointer with entity      NewtonBodySetUserData(body, my); // save entity pointer with body            NewtonBodySetTransformCallback(body, onsettransform);      NewtonBodySetDestructorCallback(body, onsetdestructor);            NewtonBodySetForceAndTorqueCallback(body, onforceandtorque);            float inertia[3];      float centerofmass[3];      NewtonConvexCollisionCalculateInertialMatrix(collision, inertia, centerofmass);       NewtonBodySetMassMatrix(body, mass, mass * inertia[0], mass * inertia[1], mass * inertia[2]);            NewtonReleaseCollision(nworld, collision);   }   }`

and after this just adds entity to compound shape
Code: Select all
`action Compound_pt3(){   wait(3);   AddToCompound(1,me,75,2,3);}action Compound_pt2(){   wait(3);   AddToCompound(1,me,75,1,3);}action Compound_pt1(){   CompMainEnt=me;   //   CompoundControl();   wait(3);   //   vec_diff(   AddToCompound(1,me,75,0,3);}`

everything looking work fine, but the main problem is that colition ingores entitie's direction

and

moving of the graphics entities isnt a problem, direction is a trouble... i also tried to rotate vertexcloud from newton_addcollision like
Code: Select all
`vec_rotate(vector(vertexcloud[i * 3 + 0],vertexcloud[i * 3 + 1],vertexcloud[i * 3 + 2]),entity.pan);vec_rotate(vector(vertexcloud[i * 3 + 0],vertexcloud[i * 3 + 1],vertexcloud[i * 3 + 2]),entity.tilt);vec_rotate(vector(vertexcloud[i * 3 + 0],vertexcloud[i * 3 + 1],vertexcloud[i * 3 + 2]),entity.roll);`

but i didnt get anything usefull

btw, maybe NewtonConvexHullModifierSetMatrix() can help me?
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

You can't apply transformation to compound object pieces themselves, the collision pieces should have transformation already set (offset matrix), you can also use a convex hull which is already created offsetted and transformed properly from object origin when making the hull, you cannot set the offset matrix for collision after you create it, so be sure it is correct before you create it.

NewtonConvexHullModifierSetMatrix is related to convex hull modifier (not compound object).
Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Compound objects

convex hull which is already created offsetted and transformed properly from object origin when making the hull

hm, i already use convex hull, but why i get something like from screenshots above, if they are transformed?

i tried
Code: Select all
`NewtonCollision *newton_addcollision(ENTITY* entity, VECTOR* diff){   if(!entity) return;      NewtonCollision* collision;      int vertices = ent_vertices(entity);   float *vertexcloud = (float*)malloc(sizeof(float) * vertices * 3);      int i;   for(i = 0; i < vertices; i++)   {      VECTOR vertex; vec_for_mesh(&vertex, entity, i + 1);      vertexcloud[i * 3 + 0] = diff.x * QUANTTOMETER + (vertex.x) * entity->scale_x * QUANTTOMETER;      vertexcloud[i * 3 + 1] = diff.y * QUANTTOMETER + (vertex.y) * entity->scale_y * QUANTTOMETER;      vertexcloud[i * 3 + 2] = diff.z * QUANTTOMETER + (vertex.z) * entity->scale_z * QUANTTOMETER;vec_rotate(vector(vertexcloud[i * 3 + 0],vertexcloud[i * 3 + 1],vertexcloud[i * 3 + 2]),entity.pan);vec_rotate(vector(vertexcloud[i * 3 + 0],vertexcloud[i * 3 + 1],vertexcloud[i * 3 + 2]),entity.tilt);vec_rotate(vector(vertexcloud[i * 3 + 0],vertexcloud[i * 3 + 1],vertexcloud[i * 3 + 2]),entity.roll);   }      collision = NewtonCreateConvexHull(nworld, vertices, vertexcloud, 12, 0.01, NULL);   free(vertexcloud);      return collision;}`

but i get something horrible like
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

the matrix transformations must be wrong then, i built huge advanced compound meshes with 1000s of pieces without any problems.
Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Compound objects

and were your pieces turned in different directions?
hm, also i notice that direction of all parts depens of direction of entity, from wich i create body...
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

No, mine weren't, but neither are yours since you create a convex hull out of your points, you transform them yourself, so your points must be sent wrong to newton (i see you pass a null pointer for offset matrix).

Here is example newton compound mesh from my project, it works perfectly:

Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Compound objects

hm, okay, i changed script, but result is the same(moffset used good for other primitives like balls or boxes, but not for compounds, i dont know why)
Code: Select all
`NewtonCollision *newton_addcollision(ENTITY* entity, VECTOR* diff){   if(!entity) return;      NewtonCollision* collision;      VECTOR vmin; vec_for_min(&vmin, entity);   VECTOR vmax; vec_for_max(&vmax, entity);   vmin.x *= entity.scale_x; vmin.y *= entity.scale_y; vmin.z *= entity.scale_z;   vmax.x *= entity.scale_x; vmax.y *= entity.scale_y; vmax.z *= entity.scale_z;      D3DXMATRIX moffset; // offset from center of bounding box to coordinate origin of entity   D3DXMatrixTranslation(&moffset,  (vmin.x + (vmax.x - vmin.x) / 2.0) * QUANTTOMETER,   (vmin.y + (vmax.y - vmin.y) / 2.0) * QUANTTOMETER,   (vmin.z + (vmax.z - vmin.z) / 2.0) * QUANTTOMETER);      int vertices = ent_vertices(entity);   float *vertexcloud = (float*)malloc(sizeof(float) * vertices * 3);      int i;   for(i = 0; i < vertices; i++)   {      VECTOR vertex; vec_for_mesh(&vertex, entity, i + 1);      vertexcloud[i * 3 + 0] = diff.x * QUANTTOMETER + (vertex.x) * entity->scale_x * QUANTTOMETER;      vertexcloud[i * 3 + 1] = diff.y * QUANTTOMETER + (vertex.y) * entity->scale_y * QUANTTOMETER;      vertexcloud[i * 3 + 2] = diff.z * QUANTTOMETER + (vertex.z) * entity->scale_z * QUANTTOMETER;   }      collision = NewtonCreateConvexHull(nworld, vertices, vertexcloud, 12, 0.01, &moffset);   free(vertexcloud);      return collision;}`

also, cant you, please rotate one part of your ship's compound just for test?
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

parts of the ship can not rotate, collisions objects in compound stay as they are once they are part of the compound.
Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Compound objects

Delfi, thank you very much for help, i just must rotate vertex vector before equalaiting to vertexcloud:)
here is working example
Code: Select all
`NewtonCollision *newton_addcollision(ENTITY* entity, VECTOR* diff){   if(!entity) return;      NewtonCollision* collision;      int vertices = ent_vertices(entity);   float *vertexcloud = (float*)malloc(sizeof(float) * vertices * 3);      int i;   for(i = 0; i < vertices; i++)   {      VECTOR vertex; vec_for_mesh(&vertex, entity, i + 1);            vec_rotate(vertex,entity.pan);            vertexcloud[i * 3 + 0] = diff.x * QUANTTOMETER + (vertex.x) * entity->scale_x * QUANTTOMETER;      vertexcloud[i * 3 + 1] = diff.y * QUANTTOMETER + (vertex.y) * entity->scale_y * QUANTTOMETER;      vertexcloud[i * 3 + 2] = diff.z * QUANTTOMETER + (vertex.z) * entity->scale_z * QUANTTOMETER;   }      collision = NewtonCreateConvexHull(nworld, vertices, vertexcloud, 12, 0.01, NULL);   free(vertexcloud);      return collision;}`
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

Delfi wrote:parts of the ship can not rotate, collisions objects in compound stay as they are once they are part of the compound.

yes, i meaned to rotate one of the part before you finish constructing compound(like i rotate second cone)
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

if you do not rotate yours, do they end up OK? in that case, your transformation routines are broken, try setting offsetmatrix instead.
Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Compound objects

yes, everything now works fine:
1) i rotate entity as i wish and then
2) i add it to compound shape and all that entities are falling and reacting like one body, as it may be

one more time thank you
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

i think that offset matrix don't need because vec_for_mesh returns local coordinate of every vertex in mesh... problem was i forgot to rotate mesh(or, if to say more exactly, every vertex of mesh) in direction that i need..
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

hmm... looking like its not the end... the center of mass isn't on its place...

looking like
Code: Select all
`float inertia[3];float centerofmass[3];NewtonConvexCollisionCalculateInertialMatrix(collision, inertia, centerofmass); NewtonBodySetMassMatrix(bodyCC, CompM, CompM * inertia[0], CompM * inertia[1], CompM * inertia[2]);`

dont work normally or whar?
Nearga

Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

### Re: Compound objects

Inertial and Mass matrix DO NOT set the centre of mass.

see: NewtonBodySetCentreOfMass
Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Next