Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMedo3337

Posted 20 May 2013 - 04:57 AM

Well, I'm not native English speaker, here is what I have done so far, still didn't get it to work, is there is anything wrong in the following code?

 

Generate a new particle:

D3DXVECTOR3 direction = gravity - position; // The lines should look at the gravity direction
D3DXVec3Normalize(&direction, &direction);
D3DXVECTOR3 Up;
// Check for a valid reference axis, can't be the same as +-direction vector.
if( abs( D3DXVec3Dot( &direction, &D3DXVECTOR3( 0, 1, 0 ) ) ) < 0.001f )
{
     D3DXVECTOR3 cr;
     D3DXVec3Cross(&cr, &direction, &D3DXVECTOR3( 1, 0, 0 ) );
     D3DXVec3Cross(&Up, &direction, &cr);
     D3DXVec3Normalize( &Up, &Up );
} else {
     D3DXVECTOR3 cr;
     D3DXVec3Cross(&cr, &direction, &D3DXVECTOR3( 0, 1, 0 ) );
     D3DXVec3Cross(&Up, &direction, &cr);
     D3DXVec3Normalize( &Up, &Up );
}

D3DXVECTOR3 randVec = GetRandVector(direction, Up);
particle.velocity += randVec * velocity_var;

 

D3DXVECTOR3 GetRandVector(D3DXVECTOR3 dirVec, D3DXVECTOR3 upVec)
{
  D3DXVec3Normalize(&dirVec, &dirVec);

  D3DXVECTOR3 vVector;

  const float  angleRange = D3DX_PI/2.0f; // 45 degree's.

  float angle  = GetRandMinMax( 0.0f, 2.0f * D3DX_PI );  // Anywhere in the full circle.
  float range = GetRandMinMax( 0.0f, tan( angleRange ) );

  D3DXVECTOR3 offset = upVec * range;

  // Hmm.  No D3DX for vector around vector rotation.  Bah..
  float s = sin( angle );
  float c = cos( angle );
  
  D3DXVECTOR3 temp(
    offset.x*(dirVec.x*dirVec.x*(1-c)+c) + offset.y * (dirVec.x*dirVec.y*(1-c)-dirVec.z*s) + offset.z*(dirVec.x*dirVec.z*(1-c)+dirVec.y*s),
   offset.x*(dirVec.x*dirVec.y*(1-c)+dirVec.z*s) + offset.y*(dirVec.y*dirVec.y*(1-c)+c) + offset.z*(dirVec.y*dirVec.z*(1-c-dirVec.x*s)),
   offset.x*(dirVec.x*dirVec.z*(1-c)-dirVec.y*s) + offset.y*(dirVec.y*dirVec.z*(1-c)+dirVec.x*s)+offset.z*(dirVec.z*dirVec.z*(1-c)+c)
  );


  temp += dirVec;
  D3DXVec3Normalize( &vVector, &temp );

  return vVector;
}

 

 


#1Medo3337

Posted 20 May 2013 - 04:56 AM

Well, I'm not native English speaker, here is what I have done so far, still didn't get it to work, is there is anything wrong in the following code?

D3DXVECTOR3 direction = gravity - position; // The lines should look at the gravity direction
D3DXVec3Normalize(&direction, &direction);
D3DXVECTOR3 Up;
// Check for a valid reference axis, can't be the same as +-direction vector.
if( abs( D3DXVec3Dot( &direction, &D3DXVECTOR3( 0, 1, 0 ) ) ) < 0.001f )
{
     D3DXVECTOR3 cr;
     D3DXVec3Cross(&cr, &direction, &D3DXVECTOR3( 1, 0, 0 ) );
     D3DXVec3Cross(&Up, &direction, &cr);
     D3DXVec3Normalize( &Up, &Up );
} else {
     D3DXVECTOR3 cr;
     D3DXVec3Cross(&cr, &direction, &D3DXVECTOR3( 0, 1, 0 ) );
     D3DXVec3Cross(&Up, &direction, &cr);
     D3DXVec3Normalize( &Up, &Up );
}

D3DXVECTOR3 randVec = GetRandVector(direction, Up);
particle.velocity += randVec * velocity_var;

 

 

D3DXVECTOR3 GetRandVector(D3DXVECTOR3 dirVec, D3DXVECTOR3 upVec)
{
  D3DXVec3Normalize(&dirVec, &dirVec);

  D3DXVECTOR3 vVector;

  const float  angleRange = D3DX_PI/2.0f; // 45 degree's.

  float angle  = GetRandMinMax( 0.0f, 2.0f * D3DX_PI );  // Anywhere in the full circle.
  float range = GetRandMinMax( 0.0f, tan( angleRange ) );

  D3DXVECTOR3 offset = upVec * range;

  // Hmm.  No D3DX for vector around vector rotation.  Bah..
  float s = sin( angle );
  float c = cos( angle );
  
  D3DXVECTOR3 temp(
    offset.x*(dirVec.x*dirVec.x*(1-c)+c) + offset.y * (dirVec.x*dirVec.y*(1-c)-dirVec.z*s) + offset.z*(dirVec.x*dirVec.z*(1-c)+dirVec.y*s),
   offset.x*(dirVec.x*dirVec.y*(1-c)+dirVec.z*s) + offset.y*(dirVec.y*dirVec.y*(1-c)+c) + offset.z*(dirVec.y*dirVec.z*(1-c-dirVec.x*s)),
   offset.x*(dirVec.x*dirVec.z*(1-c)-dirVec.y*s) + offset.y*(dirVec.y*dirVec.z*(1-c)+dirVec.x*s)+offset.z*(dirVec.z*dirVec.z*(1-c)+c)
  );


  temp += dirVec;
  D3DXVec3Normalize( &vVector, &temp );

  return vVector;
}

 


PARTNERS