FORUM CLOSED, PLEASE REGISTER AT FORUM.SIO2INTERACTIVE.COM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Camera Collision Sensor

2 posters

Go down

Camera Collision Sensor Empty Camera Collision Sensor

Post  calebcohoon Sat Aug 01, 2009 4:21 pm

I'm trying to add a sensor for my camera and a certain object but when a collision happens there is no register. Here's the code I'm using:

SIO2object *block, *player;
block = ( SIO2object * )sio2ResourceGet( sio2->_SIO2resource, SIO2_OBJECT, "object/Block" );
player = ( SIO2object * )sio2ResourceGet( sio2->_SIO2resource, SIO2_CAMERA, "camera/Camera" );
sio2SensorInitCollision("hit block", player, block, checkBlockSensorCollision);

I know that I'm casting the camera to an object but I thought that would work because the camera class contains the btRigidBody class.

What's the correct way to do this?

calebcohoon

Posts : 15
Join date : 2009-07-25

Back to top Go down

Camera Collision Sensor Empty Re: Camera Collision Sensor

Post  sio2interactive Sat Aug 01, 2009 4:40 pm

No you can't do that... If you want to do something like that... modify the source to receive rigidbody instead of object....
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 45
Location : Shanghai

http://sio2interactive.com

Back to top Go down

Camera Collision Sensor Empty Re: Camera Collision Sensor

Post  calebcohoon Sat Aug 01, 2009 6:37 pm

Well I figured out a way to do it but it's a hack. Since I'm only going to have sensors for my camera this should work okay. Wink I commented out some code in the sio2_physic.cc file:

Code:

bool sio2PhysicCollisionCallback( btManifoldPoint &cp,
                          const btCollisionObject *colObj0,int partId0, int index0,
                          const btCollisionObject *colObj1,int partId1, int index1 )
{
   unsigned int i = 0;
   
   while( i != sio2->_SIO2resource->n_sensor )
   {
      SIO2sensor *_SIO2sensor = ( SIO2sensor * )sio2->_SIO2resource->_SIO2sensor[ i ];
   
      if( _SIO2sensor->_SIO2sensorcollision )
      {
         /*if( ( _SIO2sensor->_SIO2object0->_btRigidBody == btRigidBody::upcast( colObj0 ) ||
              _SIO2sensor->_SIO2object0->_btRigidBody == btRigidBody::upcast( colObj1 ) )
              &&
            ( _SIO2sensor->_SIO2object1->_btRigidBody == btRigidBody::upcast( colObj0 ) ||
              _SIO2sensor->_SIO2object1->_btRigidBody == btRigidBody::upcast( colObj1 ) ) )
         { _SIO2sensor->_SIO2sensorcollision( _SIO2sensor ); }*/
         // just execute the sensor
         _SIO2sensor->_SIO2sensorcollision( _SIO2sensor );
      }
      else if( _SIO2sensor->_SIO2sensorcontact )
      {
         _SIO2sensor->_SIO2object0 = ( SIO2object * )btRigidBody::upcast( colObj0 )->getUserPointer();
         _SIO2sensor->_SIO2object1 = ( SIO2object * )btRigidBody::upcast( colObj1 )->getUserPointer();
         
         _SIO2sensor->_SIO2sensorcontact( _SIO2sensor );
         
         _SIO2sensor->_SIO2object0 = NULL;
         _SIO2sensor->_SIO2object1 = NULL;
      }
   
      ++i;
   }

   return false;
}

This is probably not the best solution but it works. Thanks for your help.

calebcohoon

Posts : 15
Join date : 2009-07-25

Back to top Go down

Camera Collision Sensor Empty Re: Camera Collision Sensor

Post  sio2interactive Sat Aug 01, 2009 7:17 pm

Hehe, no prob... that's why the source code is available so you can tweak it and adjust it to meet your requirements... However... I do think looking back at that function (which as been written like ages ago) that it would be more convenient to pass btRigidBody / btSoftBody handle more than the SIO2object...

I mark it down and will change it for the next version Wink

Cheers,
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 45
Location : Shanghai

http://sio2interactive.com

Back to top Go down

Camera Collision Sensor Empty Re: Camera Collision Sensor

Post  calebcohoon Sat Aug 01, 2009 8:03 pm

Sounds good, thanks! Smile

Btw, I got rid of the hack I previously posted and wrote a couple methods for checking object collision and object distance with the camera. Here's what I wrote:

Code:

SIO2sensor *sio2CameraSensorInitCollision( char             *_name,
                           SIO2object          *_SIO2object,
                           SIO2sensorcollision *_SIO2sensorcollision )
{
   SIO2camera *currentCamera = sio2->_SIO2camera;
   SIO2object *newObject = ( SIO2object * ) calloc( 1, sizeof( SIO2object ) );
   
   // Set the new object's tranform, rigid, rad and name to the camera's
   sio2StringCpy(newObject->name, currentCamera->name);
   newObject->_SIO2transform = currentCamera->_SIO2transform;
   newObject->_btRigidBody = currentCamera->_btRigidBody;
   newObject->rad = currentCamera->rad;
   
   SIO2sensor *_SIO2sensor = ( SIO2sensor * ) calloc( 1, sizeof( SIO2sensor ) );
   
   sio2StringCpy( _SIO2sensor->name, _name );
   
   _SIO2sensor->_SIO2object0 = newObject;
   _SIO2sensor->_SIO2object1 = _SIO2object;
   
   sio2ObjectEnableObjectCollisionCallback( newObject );
   sio2ObjectEnableObjectCollisionCallback( _SIO2object );
   
   _SIO2sensor->_SIO2sensorcollision = _SIO2sensorcollision;
   
   sio2ResourceAdd( sio2->_SIO2resource,
               SIO2_SENSOR,
               ( void * )_SIO2sensor );
   
   return _SIO2sensor;
}

SIO2sensor *sio2CameraSensorInitDistance( char           *_name,
                          SIO2object        *_SIO2object,
                          float            _threshold,
                          SIO2sensordistance *_SIO2sensordistance )
{
   SIO2camera *currentCamera = sio2->_SIO2camera;
   SIO2object *newObject = ( SIO2object * ) calloc( 1, sizeof( SIO2object ) );
   
   // Set the new object's tranform and name to the camera's
   sio2StringCpy(newObject->name, currentCamera->name);
   newObject->_SIO2transform = currentCamera->_SIO2transform;
   
   SIO2sensor *_SIO2sensor = ( SIO2sensor * ) calloc( 1, sizeof( SIO2sensor ) );
   
   sio2StringCpy( _SIO2sensor->name, _name );
   
   _SIO2sensor->_SIO2object0 = newObject;
   _SIO2sensor->_SIO2object1 = _SIO2object;
   
   _SIO2sensor->threshold = _threshold;
   
   _SIO2sensor->_SIO2sensordistance = _SIO2sensordistance;
   
   sio2ResourceAdd( sio2->_SIO2resource,
               SIO2_SENSOR,
               ( void * )_SIO2sensor );
   
   return _SIO2sensor;
}

calebcohoon

Posts : 15
Join date : 2009-07-25

Back to top Go down

Camera Collision Sensor Empty Re: Camera Collision Sensor

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum