Page 1 of 1

### Physicalize procedurally created Mesh.

Posted: Mon Apr 10, 2017 2:55 pm
Hi, seeing old topics in the archives like this:
https://www.cryengine.com/community_arc ... ateStatObj

I tried to create a dynamic cube and apply Physics on it: the code look like this:

Code: Select all

```IStatObj *pObject = gEnv->p3DEngine->CreateStatObj(); pObject->AddRef(); IIndexedMesh *pMesh = pObject->GetIndexedMesh(); if (pMesh == NULL) return; const size_t numVerts = 8; const size_t numTriangles = 12; pMesh->SetVertexCount(numVerts); pMesh->SetFaceCount(numTriangles); pMesh->SetTexCoordCount(numVerts); Vec3* const vertices = pMesh->GetMesh()->GetStreamPtr<Vec3>(CMesh::POSITIONS); Vec3* const normals = pMesh->GetMesh()->GetStreamPtr<Vec3>(CMesh::NORMALS); SMeshTexCoord* const texcoords = pMesh->GetMesh()->GetStreamPtr<SMeshTexCoord>(CMesh::TEXCOORDS); SMeshFace* const faces = pMesh->GetMesh()->GetStreamPtr<SMeshFace>(CMesh::FACES); vertices[0] = Vec3(0.0f, 0.0f, 0.0f); vertices[1] = Vec3(0.0f, 10.0f, 0.0f); vertices[2] = Vec3(0.0f, 0.0f, 10.0f); vertices[3] = Vec3(0.0f, 10.0f, 10.0f); vertices[4] = Vec3(10.0f, 10.0f, 0.0f); vertices[5] = Vec3(10.0f, 10.0f, 10.0f); vertices[6] = Vec3(10.0f, 0.0f, 0.0f); vertices[7] = Vec3(10.0f, 0.0f, 10.0f); SMeshFace* meshface = &faces[0]; meshface->v[0] = 0; meshface->v[1] = 2; meshface->v[2] = 1; meshface = &faces[1]; meshface->v[0] = 1; meshface->v[1] = 2; meshface->v[2] = 3; meshface = &faces[2]; meshface->v[0] = 1; meshface->v[1] = 3; meshface->v[2] = 4; meshface = &faces[3]; meshface->v[0] = 4; meshface->v[1] = 3; meshface->v[2] = 5; meshface = &faces[4]; meshface->v[0] = 2; meshface->v[1] = 0; meshface->v[2] = 6; meshface = &faces[5]; meshface->v[0] = 2; meshface->v[1] = 6; meshface->v[2] = 7; meshface = &faces[6]; meshface->v[0] = 4; meshface->v[1] = 5; meshface->v[2] = 6; meshface = &faces[7]; meshface->v[0] = 6; meshface->v[1] = 5; meshface->v[2] = 7; meshface = &faces[8]; meshface->v[0] = 1; meshface->v[1] = 4; meshface->v[2] = 6; meshface = &faces[9]; meshface->v[0] = 6; meshface->v[1] = 0; meshface->v[2] = 1; meshface = &faces[10]; meshface->v[0] = 3; meshface->v[1] = 2; meshface->v[2] = 5; meshface = &faces[11]; meshface->v[0] = 5; meshface->v[1] = 2; meshface->v[2] = 7; //Yep. meshface->nSubset = 0; //Probably also does something. So yep. pMesh->SetSubSetCount(1); pMesh->SetSubsetMaterialId(0, 0); //Calc bounding box. then optimize stuff! Whatever that does... pMesh->CalcBBox(); pMesh->Optimize(); pMesh->RestoreFacesFromIndices(); pObject->SetBBoxMin(pMesh->GetBBox().min); pObject->SetBBoxMax(pMesh->GetBBox().max); pObject->Invalidate(true); pObject-> int slotId = GetEntity()->SetStatObj(pObject, 0, true); SEntityPhysicalizeParams physicalizeParams; physicalizeParams.type = PE_STATIC; GetEntity()->Physicalize(physicalizeParams); IPhysicalEntity* pe = GetEntity()->GetPhysics(); if (pe) { pe_action_awake aa; aa.bAwake = 0; pe->Action(&aa); pe_params_foreign_data pfd; pfd.iForeignFlagsOR = PFF_UNIMPORTANT; pe->SetParams(&pfd); gEnv->pLog->Log("Physical Entity found"); } GetEntity()->Activate(true); if (pObject->SaveToCGF("Assets/objects/generated/test.cgf")) { gEnv->pLog->Log("CGF Saved"); } else { gEnv->pLog->Log("Error: CGF Not Saved"); } pObject->Release();```
Using the above code results in having the cube drawn in my scene but the entity doesn't trigger any collision. It seems that it can "receive" collisions as it can fall and be stopped by terrain or static objects. But the rigid objects or the raycasts are just passing through my cube as if it is not here.
I Saved the CGF to see if it was well saved and if it would be physicalised with a Geom Entity. the fact is even the CGF doesn't seem to have good physics configured. Can't figure out why... Any Idea?

### Re: Physicalize procedurally created Mesh.

Posted: Tue Apr 11, 2017 1:07 pm
Hey, I have no time now to test your code myself but since there where similar issues, have you tried the solution proposed here:
viewtopic.php?p=1879#p1879

### Re: Physicalize procedurally created Mesh.

Posted: Tue Apr 11, 2017 3:30 pm
Thank you, I will give it a shot but at the moment I have really no Idea how to put a surface type on a static object

### Re: Physicalize procedurally created Mesh.

Posted: Tue Apr 11, 2017 5:14 pm
The surface type is set on the material. So first of all make sure there is a SurfaceTypes.xml file located in <YourAssetFolder>\Libs\MaterialEffects\. You can find an example of the default SurfaceTypes.xml in the topic walkingtall_85 linked to earlier. Next in the Material Editor select the material you assign to the generated mesh, and you will see a field called Surface Type. Set this value to for example "default". If the missing surface type was the problem than this should fix it.

If it's still not working you can try running some of the debug tools to see if something looks wrong. After running for example "p_draw_helpers 1" check if the physics proxies are either
1. Opaque: this means the physics proxy set up correctly and should be working.
2. Half-transparent: this means the physics proxy is there, but it's surface type is not set properly.
3. Invisible: this means there is something wrong with the physics proxy or it is missing.
With this information it might be easier to figure out what's going wrong.

### Re: Physicalize procedurally created Mesh.

Posted: Tue Apr 11, 2017 6:22 pm
Thanks a lot that solved my problem! You guys rock!

### Re: Physicalize procedurally created Mesh.

Posted: Sat Jan 05, 2019 5:06 am
You need to split vertices so that hard edges have more than one vertex, since normals are per-vertex. You're getting the same result that you would if you imported something like a cube, and turned on Generate Normals and gave it a smoothing angle of 180. If you made a cube from 8 vertices, then you can only have smooth lighting since all vertices share the same normals. But that naturally looks totally wrong for a cube, so you'd need to make it from 24 vertices (4 per side). This way each vertex can have proper normals.