Advertisement

Calculating vectors with cross product

Started by January 14, 2003 10:56 AM
1 comment, last by okonomiyaki 22 years, 1 month ago
Now tell me this should work. I'm using this for a spherical billboarding effect. I came up with this last night but when I implemented it, it didn't work. What's wrong with it? First of all, I have a point that I want all the rectangles to face. For simplicity's sake, lets make it (0,0,0), so all the rectangles are facing the origin. Each rectangle is an object that has the center vector and then floats for its width and height (the actual stored values are width/2 and height/2, keep this in mind for later). So we have a vector pointing to the center of the rectangle in 3d space, pointing from (0,0,0), the point the rectangle should facec. This is how I calculated the 4 corners. The vector from (0,0,0) to the center of the rect is called pToObject. 1) Project the vector onto the XZ plane. vector.y = 0; In my application the Y axis is the "height" axis, or the one pointing up. This makes the vector flat. Call this ObjProjected. 2) Normalize(ObjProjected); 2) Create a temporary vector (0,1,0). Call this UpConst. Take the cross product of ObjProjected and UpConst. ObjProjected X UpConst = vRight. vRight is the vector from the center to the right side of the rectangle, and it should be cylindrically facing the point now. Normalize(vRight); 3) Take the cross product of vRight and Normalized(pToObject) (the original vector). vRight X pToObject should produce the "Up" vector, the vector from the center of the rect to the top. Now we have the vectors that give us the positioning of the verts, all we have to do is arrange them: vert1 (top left corner) is center + -vRight + Up. vert2 (bottom left corner) is center + -vRight + -Up. vert3 (top right corner) is center + vRight + Up. vert4 (bottom right corner) is center + vRight + -Up. The billboards kind of face me, but only cylindrically. Why don't they tilt up and down? And there are some that don't face me at all. Anyone see any faults in my design? If anyone still doesn't understand what I mean, hopefully this clears things up. The first step is in blue, producing red, then second step is is red/brown, producing purple. I don't know, I'm never too good at explaining things. Any faults anywhere? [edited by - okonomiyaki on January 14, 2003 11:57:11 AM]
The projection onto the xz plane is unneeded. The plane defined by pToObject and UpConst is the same one as the one defined by ObjProjected and UpConst, i.e. ObjProjected is a linear combination of pToObject and UpConst. As for why it isn''t spherical my only guess is that you are using the projected vector instead of the position vector for calculating the "Up" vector, i.e. the "Up" vector is UpConst. The only reason I see for losing billboards is if pToObject and UpConst are parallel which will result in vRight being the zero vector which in turn leads to the "Up" vector being zero.
Keys to success: Ability, ambition and opportunity.
Advertisement
You are right, the projection vector is unneeded Your hypothesis for my error if it were the case would also be correct, but that wasn''t my error. I found it out- the actual calculation of the vectors was:
vert1 (top left corner) is center + -vRight*vert1.Rightfac + Up*vert1.Upfac.vert2 (bottom left corner) is center + -vRight*vert2.Rightfac + -Up*vert2.Upfac.vert3 (top right corner) is center + vRight*vert3.Rightfac + Up*vert3.Upfac.vert4 (bottom right corner) is center + vRight*vert4.Rightfac + -Up*vert4.Upfac 



That makes the rectangle the actual specified width and height. The problem was that I was already negating the factors stored in each appropriate vertex, so I was negating it twice. As for why they didn''t look like they rotated up or down at all, I don''t know, that''s just the effect it had *shrug*
Thanks for the tip on the projection though Saves a step!

This topic is closed to new replies.

Advertisement