Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Particles Rotation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Medo3337   Members   -  Reputation: 672

Like
0Likes
Like

Posted 09 August 2013 - 06:31 AM

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

 



Sponsor:

#2 Migi0027 =A=   Crossbones+   -  Reputation: 1943

Like
0Likes
Like

Posted 09 August 2013 - 06:35 AM

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.


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to kill everyone you meet, ohh, AND STEAL ALL ZE TRIANGLES FROM ZHEM!

#3 Medo3337   Members   -  Reputation: 672

Like
0Likes
Like

Posted 09 August 2013 - 06:38 AM

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

 

I want to make it STOP facing the camera vertically.



#4 David_pb   Members   -  Reputation: 668

Like
0Likes
Like

Posted 09 August 2013 - 07:16 AM

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.


@D13_Dreinig

#5 Medo3337   Members   -  Reputation: 672

Like
0Likes
Like

Posted 09 August 2013 - 08:17 AM

@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?



#6 Burnt_Fyr   Members   -  Reputation: 1245

Like
1Likes
Like

Posted 09 August 2013 - 12:46 PM

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, 09 August 2013 - 12:50 PM.


#7 yckx   Prime Members   -  Reputation: 1289

Like
1Likes
Like

Posted 09 August 2013 - 02:58 PM

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.



#8 Norman Barrows   Crossbones+   -  Reputation: 2204

Like
0Likes
Like

Posted 09 August 2013 - 04:53 PM

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.


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#9 Medo3337   Members   -  Reputation: 672

Like
0Likes
Like

Posted 09 August 2013 - 07:30 PM

@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, 09 August 2013 - 07:31 PM.


#10 Burnt_Fyr   Members   -  Reputation: 1245

Like
2Likes
Like

Posted 09 August 2013 - 08:32 PM

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.



#11 Medo3337   Members   -  Reputation: 672

Like
0Likes
Like

Posted 09 August 2013 - 09:14 PM

@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, 09 August 2013 - 09:19 PM.


#12 Burnt_Fyr   Members   -  Reputation: 1245

Like
0Likes
Like

Posted 10 August 2013 - 11:05 AM

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.



#13 Medo3337   Members   -  Reputation: 672

Like
0Likes
Like

Posted 10 August 2013 - 11:10 AM

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, 10 August 2013 - 11:10 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS