I still see the undefined variable 'axis':
offset.x*(dirVec.x*dirVec.y*(1-c)+axis.z*s)
offset.x*(dirVec.x*dirVec.z*(1-c)-axis.y*s)
I still see the undefined variable 'axis':
offset.x*(dirVec.x*dirVec.y*(1-c)+axis.z*s)
offset.x*(dirVec.x*dirVec.z*(1-c)-axis.y*s)
That means dirVec should be the velocity (0, 10, 0)?
If yes, I tried that but the particles are moving straight in down direction.
D3DXVECTOR3 dirVec = velocity; // velocity = 0, 10, 0
D3DXVECTOR3 upVec( 0.0f, 1.0f, 0.0f );
D3DXVECTOR3 randVec = GetRandomVector() * scalar;
How do I determine the direction? a scalar value is not enough to determine the direction, sometimes the sparks will move down, sometimes will move up, sometimes will move to the left, it all depends on the gravity (D3DXVECTOR3 gravity).
So here is what I want to do instead:
D3DXVECTOR3 randVec = GetRandomVector(gravity) * scalar;
There is something I'm unsure about.
All I have is "D3DXVECTOR3 gravity", how do I get dirVec and upVec from gravity?
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;
}