3rd dimension in pygame | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

3rd dimension in pygame

I have managed to create a 3d box with pygame but now I wanna rotate it. The problem is have only x and y coordinates of points of the box on screen.Can someone help on how I get the third i.e z coordinate since I'm working on 3d transformation?

15th Jan 2019, 4:54 AM
Dan Rhamba
Dan Rhamba - avatar
3 Answers
+ 5
I was actually thinking of implementing my 3D Engine in Pygame yesterday 😂 https://code.sololearn.com/WczjjrjYhno9/?ref=app
15th Jan 2019, 6:12 AM
Martin Möhle
Martin Möhle - avatar
+ 5
To make 3D graphics in a 2D canvas (which is the case in pygame) you need to project vectors of 3d to a plane in 2d. Look up on projection matrices. Here I have a simple camera projection in JS, this might help. The most basic concept is to have the camera/screen at the position 0,0,0 and the projection plane being the xy plane at z = 1. So if you have the vertices of a cube: 0,0,0 0,1,0 1,1,0 1,0,0 0,0,1 0,1,1 1,1,1 1,0,1 You can see they are ontop of each other, just one z unit difference. In a 2D canvas you dont have a z component so you want to project all points to the same z so they are all on a 2d plane. https://code.sololearn.com/Wi7f7eit2bFe/?ref=app Here the projection points of x and y are calculated in line 94/5. As you want z to be 1, (plane is at z = 1) you just divide all components of the 3d vector by their z component. If you want to project any point to any plane and any camera position you also might want to have a look at this: https://www.sololearn.com/post/58076/?ref=app
15th Jan 2019, 5:27 AM
Martin Möhle
Martin Möhle - avatar
+ 3
Thanks Martin... Actually that's my goal too 😀. I was doing everything right except that final bit of projection... I get it now
15th Jan 2019, 9:35 AM
Dan Rhamba
Dan Rhamba - avatar