A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Sort of back face culling question

  1. #1
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149

    Sort of back face culling question

    I've got a question for the 3d guns. Im making a ramp in a mode7 3d space... Its using a distortImage routine to draw the image. Im looking for a way to determine from the view whether to use the top image or the bottom image.

    Best shown with a couple of pictures. The first shows the view that has the top view, the second picture though I want to show a different image. I need a formula based on the bike position and the coordinates of the ramp (all listed on the images) that I can use to determine whether its a top of bottom view. I had a look at some back face culling but my attempts at implementing were.... how does one say... fruitless ..... Just wondering if there is some wicked cool routine that I don't know about yet ...



    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    you take cross product of, for example, p1 - p0 and p1 - p2 (any pair of vectors, just make sure you always take same pair). the resulting vector then can be projected on triangle-to-camera vector (or in case of good coordinate choise you could just take its z coordinate), and the sign of the result will show you the way. the cross product, aka normal vector, can be computed once per game for static geometry; projection is a matter of dot product.
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Location
    Las Vegas
    Posts
    770
    Couldn't you just use winding, such as F10 will offer, you just have to incorporate it into your objects.

  4. #4
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    View the ramp surface as a set of points which satisfies Ax+By+Cz=D. The surface of the ramp splits R3 into two halfspaces, above and below. Compute a displacement vector from any point on the ramp surface towards the camera. Dot this vector the against the surface normal and compare against the surfaces "D" parameter.

    I think it's easier to explain in math than in words.

    Inputs:

    // the position vectors of your things in 3D (or 2D, since they're all at z=0)
    p0, p1, p2 : Vector2D or Vector3D,
    // the height of stick thingy),
    h : Number
    // 3D position of the camera
    C : Vector3D;

    Algorithm:

    // Augment position vectors to 3D, if they aren't already.
    p0prime : Vector3D = <p0.x, p0.y, 0.0>
    p1prime : Vector3D = <p1.x, p1.y, 0.0>
    p2prime : Vector3D = <p2.x, p2.y, 0.0>)
    // Compute a point at the top of the ramp.
    p2prime += h * <0 0 1>;
    // Compute normal of ramp surface
    normal : Vector3D = (p2prime-p1prime) CROSS (p0prime-p1prime)
    normal.normalize();
    D : Number = normal DOT p0prime
    // Compute displacement from surface to camera.
    displacement : Vector3D = C - p0prime;
    Dcamera : Numer = normal DOT displacement
    above : Boolean = Dcamera > D;

    Output:
    If (above) then use top surfaces texture.
    If (!above) then use the bottom texture.

    From a design standpoint, I think it would look better as a solid polyhedron. Maybe that can be a future refinement.

    EDIT: yeah what makc said - you're gettin' gangbanged with answers out here
    Last edited by rachil0; 05-22-2008 at 07:02 PM.

  5. #5
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    yes, you could have used winding: sign of (sx0*sy1 - sx1*sy0) + (sx1*sy2 - sx2*sy1) + (sx2*sy0 - sx0*sy2)

    yet the dot product is simpler, ax*bx + ay*by + az*bz. doesn't really matter for the game like this.

    EDIT: I have made slight error here.
    Last edited by realMakc; 05-22-2008 at 07:16 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  6. #6
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149
    thanks for the quick replies....
    looking at your one rachil0... are you saying that I can create an arbitrary triangle out of the ramps surface to do this check? SO I can use as you have mentioned the two bottom coordinates and one of the top positions?

    I'll give that one a go
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  7. #7
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149
    ok realMakc can you elaborate further.....

    I get the (p1 - p0) CROSS (p1 - p2)
    but Im not sure how you implement the projection onto the triangle-to-camera vector. (Or what point to choose on the triangle to make the vector)
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  8. #8
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    Quote Originally Posted by rachil0
    EDIT: yeah what makc said
    aren't you in effect suggesting winding, too? those 0-es you have added in z-s should make few products disappear I think. but I sort of thought he had raw 3d data.
    who is this? a word of friendly advice: FFS stop using AS2

  9. #9
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    @FK, (p1 - p0) CROSS (p1 - p2) is triangle normal, meaning it equally applies to any point on the triangle. I don't think you could construct a triangle (or even a plane) so that projections of its normal onto its point -to- some outer point vector would have different signs for different points. EDIT it could be easier to think about it other way around - projecting triangle-to-camera vector onto its normal... oh well. just go with Jerry's idea and use winding
    Last edited by realMakc; 05-22-2008 at 07:29 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  10. #10
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    [are you saying that I can create an arbitrary triangle out of the ramps surface to do this check?]

    It's no so much you're creating a triangle, you're creating a plane. Although you're using 3 points to determining the ABCD of the plane, the triangle is just a subset of the points that satisfy the ABCD equation. Use any 3 points on the plane that you wish.

    [Or what point to choose on the triangle to make the vector]

    Pick any point on the triangle. Or get fancy, and pick any point on the plane. You'll still get the same projection (with exact arithmetic). I picked p0prime, but you could really pick any convex summation of p0prime, p1prime and p2prime.

    [aren't you in effect suggesting winding, too?]

    Winding works too. In my codes, I use this solution because the backface culling step is done before perspective projection into screen space.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center