Author Topic: project3D2D: how to tell if center is behind the viewplane and clip  (Read 3457 times)

Offline stormp

  • byte
  • *
  • Posts: 38
    • View Profile

Still working on my grid lines.  ;D 

project3D2D wont render if "the center is behind the viewplane".   How can I adjust my vectors to clip at the approriate end?

Much thanks.


Code: [Select]
    private void drawLine(FrameBuffer fb, Graphics2D g, SimpleVector v1, SimpleVector v2)
        SimpleVector point1 = Interact2D.project3D2D(theWorld.getCamera(), fb, v1);
        SimpleVector point2 = Interact2D.project3D2D(theWorld.getCamera(), fb, v2);

        if(point1 != null && point2 != null)
            g.drawLine((int)point1.x, (int)point1.y, (int)point2.x, (int)point2.y);
        }  // if

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
Re: project3D2D: how to tell if center is behind the viewplane and clip
« Reply #1 on: August 13, 2007, 09:48:20 pm »
By clipping them in 3D if one lies behind the plane (if both are behind, the line is hidden anyway). That's quite simple, but i suggest to clip them against plane+small-value to avoid rounding errors. Here's some code to get you started:

Code: [Select]
SimpleVector start=new SimpleVector(10,10,10);
SimpleVector end=new SimpleVector(30,30,-10);
float planeAt=1.01f;

SimpleVector dir=end.calcSub(start);
float mul=(planeAt-start.z)/dir.z;
float x=start.x+mul*dir.x;
float y=start.y+mul*dir.y;

SimpleVector intersect=new SimpleVector(x,y,planeAt);