jorrit | 7 Aug 2012 10:01
Picon

SF.net SVN: cel:[4951] cel/trunk

Revision: 4951
          http://cel.svn.sourceforge.net/cel/?rev=4951&view=rev
Author:   jorrit
Date:     2012-08-07 08:01:44 +0000 (Tue, 07 Aug 2012)
Log Message:
-----------
- Added support for disabling physics in the dynworld plugin. In this mode
  no bodies will be created. It is assumed that in this mode the CS collision
  detection system will be used instead (OPCODE).
- Added a 'physics' property to enable/disable physics.
- Added support for attaching an OPCODE collider (csColliderWrapper) to
  a dynamic object (in addition or instead of) the physics bodies.

Modified Paths:
--------------
    cel/trunk/include/propclass/dynworld.h
    cel/trunk/plugins/addons/dynworldload/dynworldload.cpp
    cel/trunk/plugins/propclass/dynworld/dynworld.cpp
    cel/trunk/plugins/propclass/dynworld/dynworld.h

Modified: cel/trunk/include/propclass/dynworld.h
===================================================================
--- cel/trunk/include/propclass/dynworld.h	2012-08-06 16:55:07 UTC (rev 4950)
+++ cel/trunk/include/propclass/dynworld.h	2012-08-07 08:01:44 UTC (rev 4951)
 <at>  <at>  -358,6 +358,14  <at>  <at> 
    */
   virtual void DeleteBodies () = 0;

+  /**
+   * Create a collider for the CS collision detection system (not physics).
+   * This is useful if you want to make a game that doesn't use physics
+   * (see iPcDynamicWorld->EnablePhysics()).
+   */
+  virtual void SetColliderEnabled (bool e) = 0;
+  virtual bool IsColliderEnabled () const = 0;
+
   // ------------------------------------------------------------------------

   /**
 <at>  <at>  -701,6 +709,9  <at>  <at> 

 /**
  * Interface to the dynamic world plugin.
+ *
+ * This property class supports the following properties:
+ * - physics (bool, read/write): disable/enable physics.
  */
 struct iPcDynamicWorld : public virtual iBase
 {
 <at>  <at>  -731,6 +742,16  <at>  <at> 
   virtual void EnableGameMode (bool e) = 0;
   virtual bool IsGameMode () const = 0;

+  /**
+   * Enable/disable physics mode. Physics mode is enabled by default.
+   * If enabled physics bodies will be made for the objects if they have them
+   * defined. Otherwise no such bodies are created.
+   * It is safe to call this function after objects have been created already.
+   * Existing bodies will be created/removed on demand.
+   */
+  virtual void EnablePhysics (bool e) = 0;
+  virtual bool IsPhysicsEnabled () const = 0;
+
   //------------------------------------------------------------------------------

   /**

Modified: cel/trunk/plugins/addons/dynworldload/dynworldload.cpp
===================================================================
--- cel/trunk/plugins/addons/dynworldload/dynworldload.cpp	2012-08-06 16:55:07 UTC (rev 4950)
+++ cel/trunk/plugins/addons/dynworldload/dynworldload.cpp	2012-08-07 08:01:44 UTC (rev 4951)
 <at>  <at>  -118,6 +118,9  <at>  <at> 
   bool light = false;
   if (node->GetAttribute ("light"))
     light = node->GetAttributeValueAsBool ("light");
+  bool hasCollider = false;
+  if (node->GetAttribute ("collider"))
+    hasCollider = node->GetAttributeValueAsBool ("collider");

   csVector3 min, max;
   if (logic)
 <at>  <at>  -165,6 +168,8  <at>  <at> 
     fact->SetDefaultEntityTemplate (tmpName);
   }

+  fact->SetColliderEnabled (hasCollider);
+
   csRef<iDocumentNodeIterator> it = node->GetNodes ();
   while (it->HasNext ())
   {
 <at>  <at>  -684,6 +689,8  <at>  <at> 
       factNode->SetAttribute ("light", "true");
     if (fact->GetDefaultEntityTemplate ())
       factNode->SetAttribute ("template", fact->GetDefaultEntityTemplate ());
+    if (fact->IsColliderEnabled ())
+      factNode->SetAttribute ("collider", "true");
     if (fact->IsLogicFactory ())
     {
       const csBox3& bbox = fact->GetBBox ();

Modified: cel/trunk/plugins/propclass/dynworld/dynworld.cpp
===================================================================
--- cel/trunk/plugins/propclass/dynworld/dynworld.cpp	2012-08-06 16:55:07 UTC (rev 4950)
+++ cel/trunk/plugins/propclass/dynworld/dynworld.cpp	2012-08-07 08:01:44 UTC (rev 4951)
 <at>  <at>  -33,6 +33,7  <at>  <at> 
 #include "cstool/csview.h"
 #include "cstool/simplestaticlighter.h"
 #include "cstool/genmeshbuilder.h"
+#include "cstool/collider.h"
 #include "csgfx/imagememory.h"
 #include "iutil/objreg.h"
 #include "iutil/object.h"
 <at>  <at>  -919,6 +920,8  <at>  <at> 
   factory = 0;
   lightFactory = 0;

+  hasCollider = false;
+
   factory = world->engine->FindMeshFactory (name);
   if (factory == 0)
   {
 <at>  <at>  -1035,6 +1038,11  <at>  <at> 
   return new AttributeIterator (attributes.GetIterator ());
 }

+void DynamicFactory::SetColliderEnabled (bool e)
+{
+  hasCollider = e;
+}
+
 void DynamicFactory::AddRigidBox (const csVector3& offset, const csVector3& size,
     float mass, size_t idx)
 {
 <at>  <at>  -1530,6 +1538,7  <at>  <at> 
       dynobjFinder::AttachDynObj (light, this);
   }

+  CreateCollider ();
   CreateBody ();
 }

 <at>  <at>  -1618,6 +1627,20  <at>  <at> 
   return connectedObjects[jointIdx];
 }

+void DynamicObject::CreateCollider ()
+{
+  if (!mesh) return;
+  if (factory->IsColliderEnabled ())
+  {
+    csColliderWrapper* wrap = csColliderWrapper::GetColliderWrapper (mesh->QueryObject ());
+    if (wrap) return;
+    celPcDynamicWorld* world = factory->GetWorld ();
+    iCollideSystem* cdsys = world->GetCollideSystem ();
+    if (!cdsys) return;
+    csColliderHelper::InitializeCollisionWrapper (cdsys, mesh);
+  }
+}
+
 void DynamicObject::CreateBody ()
 {
   bsphereValid = false;
 <at>  <at>  -1633,7 +1656,7  <at>  <at> 
   {
     body = colliders[i]->Create (cell->dynSys, mesh, light, trans, body);
   }
-  if (is_static)
+  if (is_static && body)
     body->MakeStatic ();

   SetupPivotJoints ();
 <at>  <at>  -1642,8 +1665,25  <at>  <at> 
   MeshBodyToEntity (mesh, body);
 }

+void DynamicObject::RemoveCollider ()
+{
+  if (!mesh) return;
+  csColliderWrapper* wrap = csColliderWrapper::GetColliderWrapper (mesh->QueryObject ());
+  if (!wrap) return;
+  mesh->QueryObject ()->ObjRemove (static_cast<iObject*> (wrap));
+}
+
+void DynamicObject::RemoveBody ()
+{
+  if (!body) return;
+  body->DestroyColliders ();
+  body = 0;
+  MeshBodyToEntity (mesh, 0);
+}
+
 void DynamicObject::RefreshColliders ()
 {
+  CreateCollider ();
   if (!body) return;
   CreateBody ();
 }
 <at>  <at>  -1966,6 +2006,8  <at>  <at> 

 //---------------------------------------------------------------------------------------

+PropertyHolder celPcDynamicWorld::propinfo;
+
 celPcDynamicWorld::celPcDynamicWorld (iObjectRegistry* object_reg)
   : scfImplementationType (this, object_reg)
 {  
 <at>  <at>  -1986,6 +2028,12  <at>  <at> 
   currentCell = 0;
   inhibitEntities = false;
   gameMode = true;
+  doPhysics = true;
+
+  propholder = &propinfo;
+  propinfo.SetCount (1);
+  AddProperty (propid_physics, "physics",
+	CEL_DATA_BOOL, false, "Enable physics.", 0);
 }

 celPcDynamicWorld::~celPcDynamicWorld ()
 <at>  <at>  -1995,6 +2043,38  <at>  <at> 
     pl->ResetScope (scopeIdx);
 }

+iCollideSystem* celPcDynamicWorld::GetCollideSystem ()
+{
+  if (!cdsys)
+  {
+    cdsys = csQueryRegistry<iCollideSystem> (object_reg);
+    if (!cdsys)
+      Error ("Can't find collide system!");
+  }
+  return cdsys;
+}
+
+bool celPcDynamicWorld::SetPropertyIndexed (int idx, bool b)
+{
+  if (idx == propid_physics)
+  {
+    EnablePhysics (b);
+    return true;
+  }
+  return false;
+}
+
+bool celPcDynamicWorld::GetPropertyIndexed (int idx, bool& l)
+{
+  if (idx == propid_physics)
+  {
+    l = doPhysics;
+    return true;
+  }
+  return false;
+}
+
+
 DynamicCell* celPcDynamicWorld::FindCellForID (uint id)
 {
   csHash<csRef<DynamicCell>,csString>::GlobalIterator it = cells.GetIterator ();
 <at>  <at>  -2746,6 +2826,29  <at>  <at> 
   }
 }

+void celPcDynamicWorld::EnablePhysics (bool e)
+{
+  if (doPhysics == e) return;
+  doPhysics = e;
+
+  csRef<iDynamicCellIterator> it = GetCells ();
+  while (it->HasNext ())
+  {
+    iDynamicCell* cell = it->NextCell ();
+    if (doPhysics)
+      UpdateObjects (cell);
+    else
+    {
+      for (size_t i = 0 ; i < cell->GetObjectCount () ; i++)
+      {
+        iDynamicObject* idynobj = cell->GetObject (i);
+        DynamicObject* dynobj = static_cast<DynamicObject*> (idynobj);
+	dynobj->RemoveBody ();
+      }
+    }
+  }
+}
+
 iCelEntity* celPcDynamicWorld::CreateSpawnedEntity (iCelEntityTemplate* tpl,
       const char* entityName, iCelParameterBlock* params,
       const csVector3& p, float yrot, const char* sector,

Modified: cel/trunk/plugins/propclass/dynworld/dynworld.h
===================================================================
--- cel/trunk/plugins/propclass/dynworld/dynworld.h	2012-08-06 16:55:07 UTC (rev 4950)
+++ cel/trunk/plugins/propclass/dynworld/dynworld.h	2012-08-07 08:01:44 UTC (rev 4951)
 <at>  <at>  -317,6 +317,7  <at>  <at> 
   csBox3 physBbox;
   float maxradiusRelative;
   bool isLogic;
+  bool hasCollider;

   csArray<csVector3> pivotJoints;
   csArray<DynFactJointDefinition> joints;
 <at>  <at>  -383,6 +384,9  <at>  <at> 
   virtual void DeleteBody (size_t idx);
   virtual void DeleteBodies ();

+  virtual void SetColliderEnabled (bool e);
+  virtual bool IsColliderEnabled () const { return hasCollider; }
+
   virtual DynFactJointDefinition& CreateJoint ();
   virtual size_t GetJointCount () const { return joints.GetSize (); }
   virtual DynFactJointDefinition& GetJoint (size_t idx) { return joints[idx]; }
 <at>  <at>  -462,6 +466,7  <at>  <at> 
   // Update the mesh and body in the property classes of the entity.
   void MeshBodyToEntity (iMeshWrapper* mesh, iRigidBody* body);

+  void CreateCollider ();
   void CreateBody ();

 public:
 <at>  <at>  -528,6 +533,8  <at>  <at> 
   void RemoveMesh (celPcDynamicWorld* world);
   void RemoveLight (celPcDynamicWorld* world);
   void RemoveCsObject (celPcDynamicWorld* world);
+  void RemoveCollider ();
+  void RemoveBody ();
   void Save (iDocumentNode* node, iSyntaxService* syn);
   bool Load (iDocumentNode* node, iSyntaxService* syn, celPcDynamicWorld* world);

 <at>  <at>  -641,6 +648,7  <at>  <at> 
   csRef<iGraphics3D> g3d;
   csWeakRef<iCelPlLayer> pl;
   csRef<iVirtualClock> vc;
+  csRef<iCollideSystem> cdsys;
   csRefArray<DynamicFactory> factories;
   csHash<DynamicFactory*,csString> factory_hash;
   MeshCache meshCache;
 <at>  <at>  -648,10 +656,19  <at>  <at> 
   csRef<iELCM> elcm;
   size_t scopeIdx;

+  // For properties.
+  enum propids
+  {
+    propid_physics = 0
+  };
+  static PropertyHolder propinfo;
+
   // Don't create entities if this is true.
   bool inhibitEntities;
   // Game mode.
   bool gameMode;
+  // Physics.
+  bool doPhysics;

   uint lastIDBlock;
   // The following flag is set to true while we are restoring ID blocks.
 <at>  <at>  -709,6 +726,8  <at>  <at> 
   celPcDynamicWorld (iObjectRegistry* object_reg);
   virtual ~celPcDynamicWorld ();

+  iCollideSystem* GetCollideSystem ();
+
   uint AllocIDBlock ()
   {
     CS_ASSERT (!restoringIDBlocks);
 <at>  <at>  -740,6 +759,8  <at>  <at> 
   virtual bool IsInhibitEntities () const { return inhibitEntities; }
   virtual void EnableGameMode (bool e);
   virtual bool IsGameMode () const { return gameMode; }
+  virtual void EnablePhysics (bool e);
+  virtual bool IsPhysicsEnabled () const { return doPhysics; }

   void SafeToRemove (iCelEntity* entity);
   virtual void Dump ();
 <at>  <at>  -788,6 +809,11  <at>  <at> 
   virtual void MarkBaseline ();
   virtual csPtr<iDataBuffer> SaveModifications ();
   virtual void RestoreModifications (iDataBuffer* buf);
+
+  // Override SetProperty from celPcCommon in order to provide support
+  // for the 'physics' property.
+  virtual bool SetPropertyIndexed (int idx, bool b);
+  virtual bool GetPropertyIndexed (int, bool&);
 };

This was sent by the SourceForge.net collaborative development platform, the world's largest Open
Source development site.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

Gmane