Placing particles emitter in front of camera

View previous topic View next topic Go down

Placing particles emitter in front of camera

Post  Francescu on Sat Apr 11, 2009 4:28 am

I have been trying to place the snow emitter that is showcased in Tutorial12, in front of the camera at all times.

I want to do this in order to have snow falling everywhere the camera is and to limit GPU/CPU cycles on the device by not having too many particles and some emitter with a big radius.

With the help of ROm (SIO2) I have been trying out some code but it does NOT really seem to work at the moment...

If anyone has an idea of how to solve this I think it would be useful to the community and tutorial12 could even be updated.
The emitter would sit 5 units above the camera Z and should be in front of it (e.g. dir * distance).

So I have added the code below in template.mm of tutorial12 (line #73) just before sio2CameraRender:
(I have commented out normalization of the camera direction as it does not change anything)

Code:

      if( ROT_Z )
      {
         sio2TransformRotateZ( sio2->_SIO2camera->_SIO2transform,
                          ROT_Z );
      }
...
...
      // In case of snow, position the emitter in front of the camera
      if (EMITTER_TYPE == 3)
      {
         //sio2Normalize( sio2->_SIO2camera->_SIO2transform->dir,
         //sio2->_SIO2camera->_SIO2transform->dir );

         // Distance in front of the camera based on its direction
         vec3 d = { sio2->_SIO2camera->_SIO2transform->dir->x * 4.0f,
                  sio2->_SIO2camera->_SIO2transform->dir->y * 4.0f,
                  5.0f };
         
         // Placeholder for Emitter position
         vec3 pos = { 0.0f, 0.0f, 0.0f };
         
         sio2Vec3Add( sio2->_SIO2camera->_SIO2transform->loc,
                 &d,
                &pos );
         
         _SIO2emitter->_SIO2transform->loc->x = pos.x;
         _SIO2emitter->_SIO2transform->loc->y = pos.y;
         _SIO2emitter->_SIO2transform->loc->z = pos.z;
      }
...
...
      sio2CameraRender( sio2->_SIO2camera );
      
      sio2CameraUpdateFrustum( sio2->_SIO2camera );

If anyone has any idea on how to solve this, it would be much appreciated.

Cheers

--francois

Francescu

Posts : 136
Join date : 2009-03-18

View user profile

Back to top Go down

2nd Method

Post  Francescu on Sat Apr 11, 2009 8:46 pm

Ok I have tried a different method which consists of finding some arbitrary perpendicular vector by doing a cross-product of the camera dir and some cardinal basis vector with the least magnitude.

This does not work as well and am completely puzzled at this point.

Code:

      // In case of snow, position the emitter in front of the camera
      if (EMITTER_TYPE == 2)
      {
         vec3 pos = { 0.0f, 0.0f, 0.0f };
         vec3 loc = { 0.0f, 0.0f, 1.0f };
         
         // Find the cardinal basis vector with the least magnitude
         float min_component = sio2->_SIO2camera->_SIO2transform->dir->z;
         
         if (sio2->_SIO2camera->_SIO2transform->dir->x < min_component)
         {
            min_component = sio2->_SIO2camera->_SIO2transform->dir->x;
            loc.x = 1.0f;
            loc.z = 0.0f;
         }
         
         if(sio2->_SIO2camera->_SIO2transform->dir->y < min_component)
         {
            loc.x = 0.0f;
            loc.y = 1.0f;
         }

         // Calculate the (arbitrary) perpendicular vector
         sio2CrossProduct( sio2->_SIO2camera->_SIO2transform->dir, &loc, &pos );
         sio2Normalize(&pos, &pos);
                     
         _SIO2emitter->_SIO2transform->loc->x = pos.x * 4.0f;
         _SIO2emitter->_SIO2transform->loc->y = pos.y * 4.0f;
         _SIO2emitter->_SIO2transform->loc->z = 5.0f;
      }
...
...
      sio2CameraRender( sio2->_SIO2camera );
     

Something must me off somewhere obviously...oh well, back to the drawing board.... ;-(

--francois

Francescu

Posts : 136
Join date : 2009-03-18

View user profile

Back to top Go down

Re: Placing particles emitter in front of camera

Post  sio2interactive on Sun Apr 12, 2009 2:24 am

Changing the emitter position it's not the only thing you probably also have to change the particle creation callback the code that I send you sure work at 100% you probably haven't change the particle position when. Reared to be relative to the new emitter position.

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

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

View user profile http://sio2interactive.com

Back to top Go down

Re: Placing particles emitter in front of camera

Post  Francescu on Sun Apr 12, 2009 3:38 am

sio2interactive wrote:Changing the emitter position it's not the only thing you probably also have to change the particle creation callback the code that I send you sure work at 100% you probably haven't change the particle position when. Reared to be relative to the new emitter position.
I did not because I did not see any logic in particle_creation() that was setting the snow particle location based on the emitter one and the comments only made mention of changing the emitter position to give the impression it is snowing everywhere...For some stupid reason, I thought the particle location was relative to the emitter one and by changing this last one the particle location would also shift - I'll experiment more with it - Thanks for the help...

Francescu

Posts : 136
Join date : 2009-03-18

View user profile

Back to top Go down

Re: Placing particles emitter in front of camera

Post  sio2interactive on Sun Apr 12, 2009 7:57 am

Lemme know if you still have difficulty to pull this one off...

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

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

View user profile http://sio2interactive.com

Back to top Go down

Re: Placing particles emitter in front of camera

Post  Francescu on Sun Apr 12, 2009 2:52 pm

I don't understand the relationship between the emitter location and the particle location....
Why having some emitter location when the particle one can be (re)set alone?

In the current tutorial12 particle_creation(), for the snow emitter, you force the particle Z to be 0 (line #521) - I don't understand how the example is even working if the Z of a particle is set to 0.

Obviously I'm missing the big picture and I am not understanding how this emitter / particle system works...in terms of coordinates and how they relate at runtime... pale

I have tried to reset the particle location in particle_creation() based on the emitter one and factor some randomness as you do in the current example...it did not improve what I'm trying to achieve so yeah I must be missing the big picture...

Francescu

Posts : 136
Join date : 2009-03-18

View user profile

Back to top Go down

Re: Placing particles emitter in front of camera

Post  Francescu on Mon Apr 13, 2009 1:16 pm

ROm, would it be possible to have more information about the above ^^ - thanks in advance.

When a particle is created and rendered and the camera moves around, this particular particle is not going to move obviously so I'm wondering how you can even have the effect of constant snow as the camera rotates since you would have to create new particles for the new FOV and consequently it would not show a constant snow effect, no?
Seems like one would have to create them with a random Z as well so that they ca spawn at different Z locations in the FOV and not from the top as they do in the example, is that correct?

Cheers

Francescu

Posts : 136
Join date : 2009-03-18

View user profile

Back to top Go down

Re: Placing particles emitter in front of camera

Post  sio2interactive on Mon Apr 13, 2009 4:39 pm

Quick and dirty but working:

Line #352:

Code:


         // Put the snow on top of the camera
         else if( EMITTER_TYPE == 3 )
         {
               _SIO2emitter->_SIO2transform->loc->x = sio2->_SIO2camera->_SIO2transform->loc->x + sio2->_SIO2camera->_SIO2transform->dir->x * 5.0f;
               _SIO2emitter->_SIO2transform->loc->y = sio2->_SIO2camera->_SIO2transform->loc->y + sio2->_SIO2camera->_SIO2transform->dir->y * 5.0f;
               _SIO2emitter->_SIO2transform->loc->z = sio2->_SIO2camera->_SIO2transform->loc->z + 1.0f;
         }



Line #499


Code:


      // Snow
      case 3:
      {
         _SIO2particle->col->x = 1.0f;
         _SIO2particle->col->y = 1.0f;
         _SIO2particle->col->z = 1.0f;
         _SIO2particle->col->w = 1.0f;
         
         _SIO2particle->lifetime = MAX_LIFETIME;
         _SIO2particle->speed = MAX_SPEED;
         
         _SIO2particle->size = (float)sio2Randomui( 4 );
         
         _SIO2particle->loc->x = -5.0f + ( float )sio2Randomui( 1000 ) / 100.0f;
         _SIO2particle->loc->y = -5.0f + ( float )sio2Randomui( 1000 ) / 100.0f;
         _SIO2particle->loc->z = 0.0f;

         break;
      }



_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

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

View user profile http://sio2interactive.com

Back to top Go down

Re: Placing particles emitter in front of camera

Post  Francescu on Mon Apr 13, 2009 9:46 pm

Works great. Many thanks - I have set an emitter Z of 5.0f for an even better effect.
Would have been great to understand the relationship between the emitter and the particles, especially when it comes to location / positioning. There is no way I would have thought of the -5.0f on the x,y for the particle during creation, in respect with the emitter position (+5.0f in front of the camera)...
Cheers

Francescu

Posts : 136
Join date : 2009-03-18

View user profile

Back to top Go down

Re: Placing particles emitter in front of camera

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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