# 3D perspective projection (math)

Page 1 of 1

## 0 Replies - 689 Views - Last Post: 30 June 2013 - 07:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323964&amp;s=cbc6406b6e6c485772dc0d5363949e0e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AvidDismantler

Reputation: 1
• 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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}