T0X1N Posted June 9, 2016 Share Posted June 9, 2016 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: Quote Website | Twitter | Facebook | Steam Play Our Latest Game: Relic Rogue Link to comment Share on other sites More sharing options...
martyj Posted June 9, 2016 Share Posted June 9, 2016 It could be due to something like this. http://i.stack.imgur.com/RXJ3d.jpg Try specifying a different z-axis for your p1. Notice how the camera is at p(0, 0, 0), but the image placement is forward in the z direction. Quote Link to comment Share on other sites More sharing options...
T0X1N Posted June 9, 2016 Author Share Posted June 9, 2016 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. Quote Website | Twitter | Facebook | Steam Play Our Latest Game: Relic Rogue Link to comment Share on other sites More sharing options...
f13rce Posted June 9, 2016 Share Posted June 9, 2016 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 Quote 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 More sharing options...
T0X1N Posted June 10, 2016 Author Share Posted June 10, 2016 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. I'll keep working at this and see if I can fix it. If anybody has a solution before me, please chime in! Quote Website | Twitter | Facebook | Steam Play Our Latest Game: Relic Rogue Link to comment Share on other sites More sharing options...
Recommended Posts
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.