Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to project a 3D point on to a canvas  (Read 2374 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Devvie

« Posted 2013-02-19 17:45:41 »

Hi everyone, the current task I've set myself is to take an array of 4 three-dimensional points and project them on to a two-dimensional canvas. I'm having a bit of trouble understanding what I've read and tried to put into practice and was wondering whether anyone could tell me where I've gone wrong.
Say for example I have these 4 three-dimensional points representing the corners of a quadrilateral, with two points at Z coordinate 20 and the other two at Z coordinate 40, making up a square with its faces facing down the X axis. If I then translate this set of points 100 points down the negative X axis, I should now be able to see the back two points, which should also be rendered with perspective projection, meaning that the two furthest points appear closer together. This is the perspective projection I've used:
In the "Remarks" section.

Now my understanding breaks down when I come to transform the points to their perspective view. From what I've gathered, the point, which can be represented with a 3-dimensional vector is multiplied by the view matrix, which I've initially set to the identity matrix. From here presumably you're meant to have a transformed point (3-dimensional vector), which you then have to multiply by the projection matrix, which in my case is in a perspective view.

From that I now have a new transformed 3-dimensional vector that I can simply render to the screen as I would normal screen pixels. So in a nutshell:

TransformedPoint = Point x ViewMatrix x ProjectionMatrix

Is this right? This appears to just produce the same point as the original. I know I'll have to post some code up here, but could you tell if this logically isn't very sound anyway?
Also, I'm rendering all this into an AWT Canvas.

Thanks in advance for any help!

Offline relminator
« Reply #1 - Posted 2013-02-22 15:13:03 »

Yes, that should work. 

Or just use gluProject.
Offline Porlus

Junior Devvie

« Reply #2 - Posted 2013-02-23 16:06:24 »

Hey, thanks for the reply! I've almost got what I need now, but there's a glitch that I don't really understand the reason of. I've taken the 3D point and multiplied if by the view matrix, which I've just set as the identity. I've then multiplied this transformed point by the projection matrix shown above to get the clip space coordinates, which I then multiply by the viewport half width/height to transform it to screen coordinates and then divide both the X and Y components by the Z component for the foreshortening. The problem I've found is that when I move the point forward, it moves into the scene as you'd expect, but when I move it backwards to the point where it's behind the camera and not visible in the scene, it reappears in opposite corner.
So for example, a point that's in the bottom right hand corner, moves out of the window by the bottom right corner as its Z component deceases, but the point then reappears from the top left hand corner and moves towards the centre.
Has anyone ever heard of anything similar to this?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

« JGO Spiffy Duke »

Medals: 149
Projects: 1
Exp: 6 years

Java guru wannabe

« Reply #3 - Posted 2013-02-23 17:34:05 »


That's how gluProject works as well, there you can grab the 3rd parameter returned to see whether the point is in front ( >1 ) or behind the canvas plane ( <1 ). Maybe you have something similar and otherwise you can just check if the point is behind the canvas plane.


My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Porlus

Junior Devvie

« Reply #4 - Posted 2013-02-23 18:03:18 »

Thanks for the reply. Smiley Here's the code I have for rendering the points:

for(int i = 0; i < vertices.length; i++) {
   Point4f transPos = Point4f.multiply(Point4f.multiply(vertices[i], viewMat), projMat);
   if(transPos.getX() < -1 || transPos.getX() > 1 || transPos.getY() < -1 || transPos.getY() > 1) continue;
   int x = (int) (Main.WIDTH * 0.5 + transPos.getX() * Main.WIDTH * 0.5 / transPos.getZ());
   int y = (int) (Main.HEIGHT * 0.5 + transPos.getY() * Main.HEIGHT * 0.5 / transPos.getZ());
   screen.drawPoint(x, y, 0xffffffff);

(btw Main.WIDTH and Main.HEIGHT are the viewport width and height).

I thought that the if statement on the 3rd line checks whether the point is outside of the clip space, which would have eliminated points behind the camera. Could this be because the X and Y components are divided by the Z component after that check? Or is normal to just put in another check to determine whether the Z component of the point is less than the Z component of the camera?
Offline Porlus

Junior Devvie

« Reply #5 - Posted 2013-02-25 21:51:45 »

Hi all again Smiley I've still got this problem. I've attempted to change a few things, although they didn't really help. The problem remains that when the point moves behind the viewport, it reappears in the opposite corner and moves towards the point to infinity. I tried adding the Z component to this statement:

if(transPos.getX() < -1 || transPos.getX() > 1 || transPos.getY() < -1 || transPos.getY() > 1) continue;

Although it didn't seem to solve my problem, however it did clip points further out in the scene. Does anyone know of any source code where something similar has been achieved and is working (perspective projection of a 3D point on a 2D canvas)?
If anyone could post a source I'd really appreciate it. I'm reading from a few different sources, although from what I'm gathering from them all collectively doesn't mention a glitch of this nature.
Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!