Skip to main content
GameDev.net gamedev.net
🔒 Locked

Points on a Quad

Started by Omnipotent Jul 13, 2004 at 1:59 PM 2 replies 800+ views
Original Post
Omnipotent
Omnipotent
Hi All, I am trying to source out how to calculate random points on a 3d quad in a fast an efficient way and was wondering if anyone had an input on the topic. Thanks, Omnipotent
Motorherp
Motorherp
Let a quad be defined so that in its local space it lies in the xz plane with lengthX and lengthZ and zero y thickness. For each point generate two values f1 and f2 so that:

-lengthX*0.5 < f1 < lengthX*0.5
-lengthZ*0.5 < f2 < lengthZ*0.5

Plug these two values into a vector for that point so you get V(f1, 0, f2), then multiply V by the quad's world space rotation matrix and finally translate it by the quad's world space position. The final result is a random point in the quad in world space.

The rotation matrix for the quad is made by inserting the quad's normal vector and two perpendicular quad edge vectors into the matrix's rows or columns depending on if you use row or column dominant matricies (D3D = column dominant). If you plan on creating several random points at a time you may be better forming a translation matrix from the quads world space position and concatenating this with the rotation matrix so you only need one matrix multiply per point. Hope this helps
[size="1"] [size="4"]:: SHMUP-DEV ::
JohnBolton
JohnBolton
If the quad has the vertices A, B, C, and D and AB is parallel to CD and AD is parallel to BC (a square, rectangle, or parallelogram), then a choosing a uniformly distributed random point is easy.
Vector3 RandomPointInParallelogram( Vector3 & a, Vector3 & b, Vector3 & c, Vector3 & d ){//   B----C//  /    /// A----D    assert( IsParallel( a - b, d - c ) );    assert( IsParallel( a - d, b - c ) );    // rnd returns a random number in the range [0, 1]    return a + ( b - a ) * rnd() + ( d - a ) * rnd();} 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Motorherp
Motorherp
John Bolton thats brilliant, why didn't I think of that. I been giving the two above methods some thought though and there are some things you may want to weigh up.
If your quads are static then I definately recomend storing your quads as 4 verts and using Johns approach. It'll beat mine hands down. However if your quads are not static then my method has the advantage that it works for quads whose information is stored as its lengths, position vector, and orientation matrix. This allows your quads to be updated between frames with just one vector addition and one matrix-matrix multiplication rather than 4 vector additions and 4 vector-matrix multiplactions needed to update a quad stored as 4 verts. Obviously which is quickest is all a matter of balance, eg how often are your quads moved (if ever) and how many random points do you generate and how often. If this is being used as a particle emmitor then you might be better with Johns approach. Of course if memory isn't an issue then simply store all the information you need and pick the fastest method depending on what you're currently doing :)

PS: Oh and it just occured to me that the method I use to generate the local space random points only works for rectangular quads where as Johns method also works for parallelograms.

[Edited by - Motorherp on July 13, 2004 5:36:58 PM]
[size="1"] [size="4"]:: SHMUP-DEV ::

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.