Jump to content

(C++) Camera::Project - Weird Results When Looking Away From Project Point


T0X1N
 Share

Recommended Posts

I am trying to use Camera::Project and Context::DrawLine to draw a 2d grid in 3d space for an map editor I am working on, but if 1 of the points go behind the camera, the grid gets goofy and is not drawing the line to connect to where it should in 3D space. Is this a bug or am I doing something wrong?

 

I see that the "Z" axis goes to a negative number when the point is behind the camera, but I don't understand why the X & Y axis go way out of what they are supposed to be nor how to get them to what they are supposed to be.

 

 

I have written a simple code snippet that does the same issue:

 

Vec3 p1 = Vec3(context->GetWidth()/2, context->GetHeight()/2, 0);
Vec3 p2 = camera->Project(Vec3(0, 0, 10));

context->DrawLine(p1.x, p1.y, p2.x, p2.y);

 

Here is a screenshot of what I am experiencing with the code above:

 

post-17-0-18601600-1465445065_thumb.jpg

Play Our Latest Game: Relic Rogue

Link to comment
Share on other sites

Thanks for the reply, Maryj. Odd thing is it does not matter what p1 nor what p2 is on the 3D space. I tried p1 as a 3D point on the grid (-10 Z units away from p2) and used Camera::Project to grab the 2D coordinates so I can draw the line, but same issue.

 

I think I know what the issue is, but I don't know the solution. I have the scene setup that you can fly the camera around using WASD and mouse to look. If one of the 3D points, of which I am using Camera::Project to get 2D coordinates to draw the line, go behind the camera, the Camera::Project basically returns a mirrored value and a negative Z axis. I am no math guru and I don't know of a formula to mirror those values back to where they should be so the program can draw the line correctly.

 

The end goal here is to draw a 2D grid in the 3D space, just like how the Leadwerks Editor has. I could use a large 3d plane with a grid texture to make the grid, but I would rather do it with 2D lines so I don't use any polygons nor material loaded in memory to draw it.

Play Our Latest Game: Relic Rogue

Link to comment
Share on other sites

I remember this problem when using Project to draw health bars. I solved it by doing a dot product to see whether or not a point is in front of the camera or not.

 

I don't have the code right now but can give it to you in about 12 hours from now if you want to.

 

Edit: Oops, saw too late that you still want to draw the line. I don't have a solution for that though. Although you can use the dot product to determine whether or not it should be drawn

Using Leadwerks Professional Edition (Beta), mainly using C++.

Windows 10 / Linux Mint, Visual Studio 2017. GPU: NVidia GeForce GTX970, CPU: Intel i7 7700K @ 4.20 GHz

Previously known as Evayr.

Link to comment
Share on other sites

Thanks for replying Evayr! That issue would be easy to solve since if the camera is not looking at the point, it would return a -Z value, so I would just stop drawing it if it was just one point.

 

However, the problem here is I want to draw a line of two points. If one point goes off camera, I have want to keep drawing the line, same would be if both points are off screen, but I am looking at the path.

 

Like I said in my previous post, I think the problem is that when the Z axis goes negative, the X & Y coordinates are mirrored on the screen. I just need to mirror them back and BINGO! I should have it fixed. smile.png

 

I'll keep working at this and see if I can fix it. If anybody has a solution before me, please chime in! smile.png

Play Our Latest Game: Relic Rogue

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...