• Advertisement
Sign in to follow this  

Particles Rotation

This topic is 1658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have rain particles and I want to make them face the camera BUT ONLY horizontally so when the player look up, the rain should never rotate vertically or face the camera vertically.

 

My current code is making the particles face the camera both vertically and horizontally, how do I set static rotation vertically and make the particles only face the camera horizontally?

const D3DXMATRIX& viewMat = getCameraViewMatrix();

float f = 0.5f * particleSize;
D3DXVECTOR3 horizontal(viewMat(0,0) * f, viewMat(1,0) * f, viewMat(2,0) * f);
f = -0.5f * particleSize;
D3DXVECTOR3 vertical(viewMat(0,1) * f, viewMat(1,1) * f, viewMat(2,1) * f);
D3DXVECTOR3 view(-viewMat(0,2), -viewMat(1,2) , -viewMat(2,2));

D3DXVECTOR3 shorizontal = horizontal * particleSize;
D3DXVECTOR3 svertical = vertical * particleSize;

vertices[j*4+0] = CUSTOM_VERTEX(particlePosition + shorizontal + svertical);
vertices[j*4+1] = CUSTOM_VERTEX(particlePosition + shorizontal - svertical);
vertices[j*4+2] = CUSTOM_VERTEX(particlePosition - shorizontal - svertical);
vertices[j*4+3] = CUSTOM_VERTEX(particlePosition - shorizontal + svertical);

 

Share this post


Link to post
Share on other sites
Advertisement

This works for me:

// Get the position of the camera.
D3DXVECTOR3 *cameraPosition = Camera->GetPostion();

// Calculate the rotation that needs to be applied to the billboard model to face the current camera position using the arc tangent function.
double angle = atan2(position.x - cameraPosition->x, position.z - cameraPosition->z) * (180.0 / D3DX_PI);

// Convert rotation into radians.
float arotation = (float)angle * 0.0174532925f;

// Setup the rotation the billboard at the origin using the world matrix.
RotateY(arotation);

I think this is what you need, the RotateY function created a matrix which is applied in the shaders.

Share this post


Link to post
Share on other sites

@Migi0027: I think you misunderstood me, I have the particles facing the camera already.

 

I want to make it STOP facing the camera vertically.

Share this post


Link to post
Share on other sites

You could just set up to UNIT_Y (some pseudocode):

view = normalize(cameraOrigin - particleOrigin);
if (sphericalBillboard) // whenever the particle normal should truly face the camera
{
  vertical = cameraUpVector;
}
else
{
  vertical = Vector3::UNIT_Y;
}
horizontal = cross(view, vertical);

Keep in mind to do the calculation in the right space (i.e. worldspace) and to adjust to your coordinate system.

Share this post


Link to post
Share on other sites

@David_pb: Still couldn't get it to work as expected.

 

vertical and horizontal are D3DXVECTOR3, I already have horizontal set to face the camera in the code I posted.

 

Now, how do I set static rotation for D3DXVECTOR3 vertical?

Share this post


Link to post
Share on other sites

I think what you want is for the particles to face the camera, but still be constrained to the up/down of the world.

Vec3 up = world.up; // (usually (0,1,0)
Vec3 side = crossproduct(up, -camera.forward);
vec3 forward = crossproduct(side,up);
 
Matrix4 particleworld= (  Vec4(side, 0), Vec4(up,0), Vec4(forward,0), Vec4(particle.position, 1));

as long as your camera cannot look directly up or down this should work. EDIT: but be forewarned, i'm low on caffeine at the moment.

Edited by Burnt_Fyr

Share this post


Link to post
Share on other sites

Burnt_Fyr's response should do the trick. If you want further information, the term to Google is "axis-aligned billboard." Most of the results look like they're written for OpenGL, but if you grok the theory it shouldn't be too difficult to align billboards to whatever axis you need.

Share this post


Link to post
Share on other sites

another option:

 

apparently, rain falls faster than the eye can see individual drops when looking straight ahead. it just started raining here now, and its true. you can look up, and follow a drop down, but you cant follow a drop as it falls while looking straight ahead.

 

this is a rain effect i read about that takes advantage of this:

 

you draw a cone around the camera (pointing up), with an animated random raindrops texture on it.  i tried it, looks just like real rain, and no particle system. i used to use particles, but this is now my preferred method.

Share this post


Link to post
Share on other sites

@Burnt_Fyr: I just need to modify my own variable, I don't have a matrix for the particles, I'm setting the vertices position directly.

 

Here is how the particles face the camera in the code I posted:

 

const D3DXMATRIX& viewMat = getCameraViewMatrix();

float f = 0.5f * particleSize;
D3DXVECTOR3 horizontal(viewMat(0,0) * f, viewMat(1,0) * f, viewMat(2,0) * f);
f = -0.5f * particleSize;
D3DXVECTOR3 vertical(viewMat(0,1) * f, viewMat(1,1) * f, viewMat(2,1) * f); // This line is making the particles face the camera vertically, I need to modify this line to make the particles never face the camera vertically, instead I want to set a vertical rotation

D3DXVECTOR3 shorizontal = horizontal * particleSize;
D3DXVECTOR3 svertical = vertical * particleSize;

 

// NOTICE: I'm setting the particles vertices directly

vertices[j*4+0] = CUSTOM_VERTEX(particlePosition + shorizontal + svertical);

vertices[j*4+1] = CUSTOM_VERTEX(particlePosition + shorizontal - svertical);
vertices[j*4+2] = CUSTOM_VERTEX(particlePosition - shorizontal - svertical);
vertices[j*4+3] = CUSTOM_VERTEX(particlePosition - shorizontal + svertical);

Edited by Medo3337

Share this post


Link to post
Share on other sites

ok, so using your process:

 

f = -0.5f * particleSize;
D3DXVECTOR3 vertical(0,1*f,0)

 

this *might* work. The issue is that you you are using the camera's up vector as the up for your particle. As you pitch the camera(what you call a vertical rotation) the camera's up vector changes, which causes your vertical vec3 to point somewhere that is not aligned with the world up. By using the Y axis(which i assume to be world up) directly, as shown above, you ensure that the particle's quad is parallel to the Y axis.

Share this post


Link to post
Share on other sites

@Burnt_Fyr: That works, however...

 

I might need in future to set rotation for the particles, how do I set the particles rotation (pitch, yaw, roll)?

 

Notice: Sometimes, I could want it to face the camera vertically and set the rotation horizontally or I could want it to face the camera horizontally and set the rotation vertically.

Edited by Medo3337

Share this post


Link to post
Share on other sites

You should likely brush up on your math.

 

You have 2 vectors that you are using to describe your particle, it's vertical axis, and it's horizontal. I will consider pitching the movement i helped you with earlier, using your vertical vector, which i will now call the particles up vector.

 

When you don't want any pitching, you need to constrain the particles up vector to the world up axis(0,1,0). If you want to induce pitching, you can rotate the up vector by a certain degree, along the right vector.  Because your front vector is constrained to pointing at your camera, your right vector will be perpedicular to that. You can get it via the cross product of the up and front vectors.

 

Vec3 right = crossproduct(up, -camera.forward);

 

once you have this vector, you can rotate a vector(0,1,0) using AxisAngleRotation

// might be wrong on the exact functions but something like this
 
D3DXMATRIX rotationMatrix;
 
D3DXMatrixRotationAxisAngle(right, angle_in_rads, &rotationMatrix);
 
D3DXVECTOR3 up(0,1,0 );
 
D3DXVec3TransformNormal(&up, &rotationMatrix);

 

This up vector can now be used to create your particles. If you rotate by the opposite amount that you have pitched the camera, this up vector will be the same as your view transform's up vector.

 

A similar process can be done to get yawing or pitching using the correct transforms/axis. There is likely a better method to get the results you are trying to acheive, perhaps you could give us a better idea of that that is.

Share this post


Link to post
Share on other sites

I will check that out.

 

BTW, should the rain particles always face the camera in vertically and horizontally?

 

I have many rain drops in the same texture, but when the player look up, the particle rotate to face the camera which make the rain drops look like they are not pointed at the ground.

Edited by Medo3337

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement