0 Replies - 354 Views - Last Post: 30 June 2013 - 07:17 PM Rate Topic: -----

#1 AvidDismantler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 30-June 13

3D perspective projection (math)

Posted 30 June 2013 - 07:17 PM

Background: I've defined an icosahedron, as a collection of nodes and lines centered on 0,0,0, and I've established a rotating camera making a circular (x=sin(a) y=cos(a)) orbit around the shape. Now I'm trying, using this code that I've patched together from various math sites around the internet, to project the points of the icosahedron through the camera point, onto a viewing plane, and then map that plane onto the plane of the screen. Problem: it's not working. You can see the lines connecting up in a way which is topologically correct, but it's severely distorted, with lines sometimes running off to infinity, and the whole shape sometimes appearing flat. Also, it wobbles around its center axis, and sometimes seems to reverse its rotation. I've gone through it line by line, step by step, but I can't find the error... I've also tried altering things at random, which produces different but equally bad results. At this point I'm pretty stuck for the answer. Can anyone help me?

Here's my code:

    Function ProjectPoint(ByVal Pt As Point3D) As Point2D

        'define a vector rising from the center of the planet to the camera
        Dim CenterPoint As New Point3D(0, 0, 0)

        Dim v = New Vector3D(Pt, Camera)


        'now define a plane according to the camera position
 
        'firstly, getting a vector from planet center to camera
        Dim pv = New Vector3D(CenterPoint, Camera)

        'and a point some 50 pixels distant
        Dim Viewpoint As New Point3D
        Viewpoint.X = Camera.X + pv.UnitVector().X * 50

        Viewpoint.Y = Camera.Y + pv.UnitVector().Y * 50

        Viewpoint.Z = Camera.Z + pv.UnitVector().Z * 50

        'that right there should define a plane...



        'this is going to find the point along that line where it intersects the camera plane

        Dim t As Double

        t = pv.DotProduct(New Vector3D(Pt, Viewpoint))

        t /= pv.DotProduct(v) 'which is vector (pt, camera)

        'project to that point

        Pt.X += v.X * t

        Pt.Y += v.Y * t

        Pt.Z += v.Z * t

        'move the point to center around 0,0,0 - trying to map it onto the screen

        Pt.TranslateBy(New Vector3D(Viewpoint, New Point3D(0, 0, 0)))



        'now, if we find the line of intersection between the planes defined by:
        '-vector pv and point (0,0,0), and
        '-vector 0,0,1 and point (0,0,0)
        'and preform a rotation along that axis, by the correct amount,
        'that should bring all points into the viewing plane

        Dim CrossVector As New Vector3D(pv.Y, -pv.X, 0)

        Dim m As New Matrix3D

        'a rotation matrix, rotating the point around axis CrossVector by angle PV < 0,0,1
        m.SetToRotation(Cos(pv.AngleTo(New Vector3D(0, 0, 1))), CrossVector)
        Pt.TransformBy(m)



        'assign the function's output
        Return New Point2D(Pt.X, Pt.Y)
End Function



Is This A Good Question/Topic? 0
  • +

Page 1 of 1