78
76
#include "KX_SumoPhysicsController.h"
78
struct KX_PhysicsInstance
80
DT_VertexBaseHandle m_vertexbase;
82
RAS_IPolyMaterial* m_material;
84
KX_PhysicsInstance(DT_VertexBaseHandle vertex_base, int vtxarray, RAS_IPolyMaterial* mat)
85
: m_vertexbase(vertex_base),
93
DT_DeleteVertexBase(m_vertexbase);
80
97
static GEN_Map<GEN_HashedPtr,DT_ShapeHandle> map_gamemesh_to_sumoshape;
98
static GEN_Map<GEN_HashedPtr, KX_PhysicsInstance*> map_gamemesh_to_instance;
82
100
// forward declarations
83
void BL_RegisterSumoObject(KX_GameObject* gameobj,class SM_Scene* sumoScene,class SM_Object* sumoObj,const STR_String& matname,bool isDynamic,bool isActor);
84
DT_ShapeHandle CreateShapeFromMesh(RAS_MeshObject* meshobj);
101
static void BL_RegisterSumoObject(KX_GameObject* gameobj,class SM_Scene* sumoScene,class SM_Object* sumoObj,const STR_String& matname,bool isDynamic,bool isActor);
102
static DT_ShapeHandle CreateShapeFromMesh(RAS_MeshObject* meshobj, bool polytope);
87
104
void KX_ConvertSumoObject( KX_GameObject* gameobj,
88
105
RAS_MeshObject* meshobj,
296
322
physicscontroller->SetObject(gameobj->GetSGNode());
299
DT_ShapeHandle CreateShapeFromMesh(RAS_MeshObject* meshobj)
325
static DT_ShapeHandle InstancePhysicsComplex(RAS_MeshObject* meshobj, int vtxarray, RAS_IPolyMaterial *mat)
327
// instance a mesh from a single vertex array & material
328
const RAS_TexVert *vertex_array = &((*meshobj->GetVertexCache(mat)[vtxarray])[0]);
329
//const KX_IndexArray &index_array = *meshobj->GetIndexCache(mat)[vtxarray];
330
DT_VertexBaseHandle vertex_base = DT_NewVertexBase(vertex_array[0].getLocalXYZ(), sizeof(RAS_TexVert));
332
DT_ShapeHandle shape = DT_NewComplexShape(vertex_base);
334
std::vector<DT_Index> indices;
335
for (int p = 0; p < meshobj->NumPolygons(); p++)
337
RAS_Polygon* poly = meshobj->GetPolygon(p);
339
// only add polygons that have the collisionflag set
340
if (poly->IsCollider())
342
DT_VertexIndices(3, poly->GetVertexIndexBase().m_indexarray);
345
if (poly->VertexCount() == 4)
348
DT_VertexIndex(poly->GetVertexIndexBase().m_indexarray[0]);
349
DT_VertexIndex(poly->GetVertexIndexBase().m_indexarray[2]);
350
DT_VertexIndex(poly->GetVertexIndexBase().m_indexarray[3]);
356
//DT_VertexIndices(indices.size(), &indices[0]);
357
DT_EndComplexShape();
359
map_gamemesh_to_instance.insert(GEN_HashedPtr(meshobj), new KX_PhysicsInstance(vertex_base, vtxarray, mat));
363
static DT_ShapeHandle InstancePhysicsPolytope(RAS_MeshObject* meshobj, int vtxarray, RAS_IPolyMaterial *mat)
365
// instance a mesh from a single vertex array & material
366
const RAS_TexVert *vertex_array = &((*meshobj->GetVertexCache(mat)[vtxarray])[0]);
367
//const KX_IndexArray &index_array = *meshobj->GetIndexCache(mat)[vtxarray];
368
DT_VertexBaseHandle vertex_base = DT_NewVertexBase(vertex_array[0].getLocalXYZ(), sizeof(RAS_TexVert));
370
std::vector<DT_Index> indices;
371
for (int p = 0; p < meshobj->NumPolygons(); p++)
373
RAS_Polygon* poly = meshobj->GetPolygon(p);
375
// only add polygons that have the collisionflag set
376
if (poly->IsCollider())
378
indices.push_back(poly->GetVertexIndexBase().m_indexarray[0]);
379
indices.push_back(poly->GetVertexIndexBase().m_indexarray[1]);
380
indices.push_back(poly->GetVertexIndexBase().m_indexarray[2]);
382
if (poly->VertexCount() == 4)
383
indices.push_back(poly->GetVertexIndexBase().m_indexarray[3]);
387
DT_ShapeHandle shape = DT_NewPolytope(vertex_base);
388
DT_VertexIndices(indices.size(), &indices[0]);
391
map_gamemesh_to_instance.insert(GEN_HashedPtr(meshobj), new KX_PhysicsInstance(vertex_base, vtxarray, mat));
395
// This will have to be a method in a class somewhere...
396
// Update SOLID with a changed physics mesh.
398
bool KX_ReInstanceShapeFromMesh(RAS_MeshObject* meshobj)
400
KX_PhysicsInstance *instance = *map_gamemesh_to_instance[GEN_HashedPtr(meshobj)];
403
const RAS_TexVert *vertex_array = &((*meshobj->GetVertexCache(instance->m_material)[instance->m_vtxarray])[0]);
404
DT_ChangeVertexBase(instance->m_vertexbase, vertex_array[0].getLocalXYZ());
410
static DT_ShapeHandle CreateShapeFromMesh(RAS_MeshObject* meshobj, bool polytope)
302
413
DT_ShapeHandle *shapeptr = map_gamemesh_to_sumoshape[GEN_HashedPtr(meshobj)];
414
// Mesh has already been converted: reuse
305
417
return *shapeptr;
420
// Mesh has no polygons!
308
421
int numpolys = meshobj->NumPolygons();
427
// Count the number of collision polygons and check they all come from the same
313
429
int numvalidpolys = 0;
431
RAS_IPolyMaterial *poly_material = NULL;
432
bool reinstance = true;
315
434
for (int p=0; p<numpolys; p++)
319
438
// only add polygons that have the collisionflag set
320
439
if (poly->IsCollider())
441
// check polygon is from the same vertex array
442
if (poly->GetVertexIndexBase().m_vtxarray != vtxarray)
445
vtxarray = poly->GetVertexIndexBase().m_vtxarray;
453
// check poly is from the same material
454
if (poly->GetMaterial()->GetPolyMaterial() != poly_material)
459
poly_material = NULL;
462
poly_material = poly->GetMaterial()->GetPolyMaterial();
465
// count the number of collision polys
468
// We have one collision poly, and we can't reinstance, so we
469
// might as well break here.
475
// No collision polygons
327
476
if (numvalidpolys < 1)
330
DT_ShapeHandle shape = DT_NewComplexShape(NULL);
335
for (int p2=0; p2<numpolys; p2++)
337
RAS_Polygon* poly = meshobj->GetPolygon(p2);
339
// only add polygons that have the collisionflag set
340
if (poly->IsCollider())
341
{ /* We have to tesselate here because SOLID can only raycast triangles */
345
meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
346
poly->GetVertexIndexBase().m_indexarray[2],
347
poly->GetMaterial()->GetPolyMaterial())->xyz().getValue(pt);
350
meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
351
poly->GetVertexIndexBase().m_indexarray[1],
352
poly->GetMaterial()->GetPolyMaterial())->xyz().getValue(pt);
355
meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
356
poly->GetVertexIndexBase().m_indexarray[0],
357
poly->GetMaterial()->GetPolyMaterial())->xyz().getValue(pt);
363
if (poly->VertexCount() == 4)
479
DT_ShapeHandle shape;
483
shape = InstancePhysicsPolytope(meshobj, vtxarray, poly_material);
485
shape = InstancePhysicsComplex(meshobj, vtxarray, poly_material);
491
std::cout << "CreateShapeFromMesh: " << meshobj->GetName() << " is not suitable for polytope." << std::endl;
493
std::cout << " Check mesh materials." << std::endl;
495
std::cout << " Check number of vertices." << std::endl;
498
shape = DT_NewComplexShape(NULL);
502
for (int p2=0; p2<numpolys; p2++)
504
RAS_Polygon* poly = meshobj->GetPolygon(p2);
506
// only add polygons that have the collisionflag set
507
if (poly->IsCollider())
508
{ /* We have to tesselate here because SOLID can only raycast triangles */
367
meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
368
poly->GetVertexIndexBase().m_indexarray[3],
369
poly->GetMaterial()->GetPolyMaterial())->xyz().getValue(pt);
511
DT_Vertex(meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
512
poly->GetVertexIndexBase().m_indexarray[2],
513
poly->GetMaterial()->GetPolyMaterial())->getLocalXYZ());
515
DT_Vertex(meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
516
poly->GetVertexIndexBase().m_indexarray[1],
517
poly->GetMaterial()->GetPolyMaterial())->getLocalXYZ());
372
meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
373
poly->GetVertexIndexBase().m_indexarray[2],
374
poly->GetMaterial()->GetPolyMaterial())->xyz().getValue(pt);
377
meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
519
DT_Vertex(meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
378
520
poly->GetVertexIndexBase().m_indexarray[0],
379
poly->GetMaterial()->GetPolyMaterial())->xyz().getValue(pt);
521
poly->GetMaterial()->GetPolyMaterial())->getLocalXYZ());
526
if (poly->VertexCount() == 4)
530
DT_Vertex(meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
531
poly->GetVertexIndexBase().m_indexarray[3],
532
poly->GetMaterial()->GetPolyMaterial())->getLocalXYZ());
534
DT_Vertex(meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
535
poly->GetVertexIndexBase().m_indexarray[2],
536
poly->GetMaterial()->GetPolyMaterial())->getLocalXYZ());
538
DT_Vertex(meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray,
539
poly->GetVertexIndexBase().m_indexarray[0],
540
poly->GetMaterial()->GetPolyMaterial())->getLocalXYZ());
549
DT_EndComplexShape();
389
DT_EndComplexShape();
391
552
if (numvalidpolys > 0)
439
602
dxSpace* space = odeEnv->GetOdeSpace();
440
603
dxWorld* world = odeEnv->GetOdeWorld();
442
if (!objprop->m_implicitsphere &&
443
MT_fuzzyZero(objprop->m_boundingbox.m_extends[0]) ||
444
MT_fuzzyZero(objprop->m_boundingbox.m_extends[1]) ||
445
MT_fuzzyZero(objprop->m_boundingbox.m_extends[2])
452
KX_OdePhysicsController* physicscontroller =
453
new KX_OdePhysicsController(
461
smmaterial->m_friction,
462
smmaterial->m_restitution,
463
objprop->m_implicitsphere,
464
objprop->m_boundingbox.m_center,
465
objprop->m_boundingbox.m_extends,
469
gameobj->SetPhysicsController(physicscontroller);
470
physicscontroller->setClientInfo(gameobj);
471
gameobj->GetSGNode()->AddSGController(physicscontroller);
473
bool isActor = objprop->m_isactor;
474
STR_String materialname;
476
materialname = meshobj->GetMaterialName(0);
478
const char* matname = materialname.ReadPtr();
481
physicscontroller->SetObject(gameobj->GetSGNode());
605
bool isSphere = false;
607
switch (objprop->m_boundclass)
612
KX_OdePhysicsController* physicscontroller =
613
new KX_OdePhysicsController(
621
smmaterial->m_friction,
622
smmaterial->m_restitution,
624
objprop->m_boundobject.box.m_center,
625
objprop->m_boundobject.box.m_extends,
626
objprop->m_boundobject.c.m_radius
629
gameobj->SetPhysicsController(physicscontroller);
630
physicscontroller->setNewClientInfo(gameobj->getClientInfo());
631
gameobj->GetSGNode()->AddSGController(physicscontroller);
633
bool isActor = objprop->m_isactor;
634
STR_String materialname;
636
materialname = meshobj->GetMaterialName(0);
638
const char* matname = materialname.ReadPtr();
641
physicscontroller->SetObject(gameobj->GetSGNode());