Getting circle quarter based on Vector3 direction

Started by
22 comments, last by Medo Mex 10 years, 11 months ago

Can you provide a little bit of code so I can understand you better?

Let's see if I can modify your code (trying to remember D3DX, forgive any goofs please. smile.png):


D3DXVECTOR3 GetRandomVector()

{
  // Set these to normalized vectors as appropriate.
 // We'll use +z as direction and +y as up vector.
  D3DXVECTOR3 dirVec( 0.0f, 0.0f, 1.0f );
  D3DXVECTOR3 upVec( 0.0f, 1.0f, 0.0f );


  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 = upVector * 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;

}

I probably goofed something up in there but it should be pretty close.

In general though, you really should learn the math bits a better, it takes time but everything you do is going to be math related eventually.

Advertisement

The variable "axis" is not defined.

Do you mean the velocity?

I tried giving axis a value of (0, 10, 0) to make the particles move up, but they still start looking down then moving up.

Oops, typo converting from my library. Fixed it.

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)

Just replace them with dirVec, I'll go correct it. I translated the code from my library though obviously missed a couple things. :)

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 );

The presented code supplies a random unit vector (assuming no more bugs) within the directional cone. In order to apply your velocity, take the result vector and multiply by the desired velocity "scalar". In other words:

D3DXVECTOR3 randVec = GetRandomVector() * 10.0f;

// randVec is a random unit vector. Scale it by a given velocity scalar. I.e. you say "0, 10, 0", just multiply by 10.

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;

Replace (or pass in):

D3DXVECTOR3 dirVec( 0.0f, 0.0f, 1.0f );
D3DXVECTOR3 upVec( 0.0f, 1.0f, 0.0f );

With an appropriate set of vectors. dirVec is the emission direction and you need the right angle 'upVec' as a reference. As I mentioned in the other thread, you need a point and at least 2 vectors to fully define the emission of a particle system. Without the pair of vectors, the math won't work, I just chose two easy ones.

There is something I'm unsure about.

All I have is "D3DXVECTOR3 gravity", how do I get dirVec and upVec from gravity?

This topic is closed to new replies.

Advertisement