Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Positioning 3d Objects using mouse

Author
Message
Mootsey
16
Years of Service
User Offline
Joined: 12th Jan 2008
Location:
Posted: 13th Jan 2008 02:29
I was curious what is the best method to create an object at a mouse position on a 3d game.

Currently, I thought it would work something like



Of course this could work in a 2d game but the Z does not seem to work like this.

What is the best way to create an object at a specific mouse location in a 3d game?
tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 13th Jan 2008 09:13
dbMouseX() and dbMouseY() calls return the coordinates of a mouse pointer on the screen. You have to take this coordinate and unproject it from screen space to world space.

You can do this by applying inverse projection matrix on a normalised screen coordinate, which has [-1,-1] in bottom left corner of the screen and [1,1] in right top (compute the actual coordinate by interpolating the dbMouseX/Y() values across your screen size). Then you have to choose some "Z" coordinate since your screen is only 2D (e.g. choose the Near clipping plane distance). On this vector you apply inverse view matrix and you acquire a 3D world coordinates of the point on a screen.

Since there are infinite number of locations where the object can be put for a specified mouse position (again, your screen is 2D and you are solving it in 3D), you must choose some proper value. Fortunately, all the positions lies on a single straight line defined by vector (P-V), where P is the point you have computed and V is camera location in the world space. Find some proper location on this line, for example you can cast a ray and find where it cross a terrain. There you place your object (e.g. if you have distance from viewer z, the final location is z(norm(P-V))+V).

It's a bit of math, hope this helps.

//rem
Mootsey
16
Years of Service
User Offline
Joined: 12th Jan 2008
Location:
Posted: 15th Jan 2008 17:03
I actually looked on the forum and used the RTS tutorial code and it worked perfectly. I couldn't figure out the formulas in raw form but thanks for the help. Reading the formulas you gave and seeing the actual code I was able to piece them both together.
skull driveshaft
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location:
Posted: 19th Jan 2008 10:16
For those people that want to pick an object on the screen and move it using the mouse, here's a little bit of code for you ;]



When you click on an object in 3d space, you get the "vector" to it from the camera - as in the position of the object as if the camera was placed at 0,0,0 - to get the actual position of the object you add the two locations together to give you the true position of the object in 3d space.

You hold down the left mouse button, it becomes tied to your mouse pointer, you get to move it around, you let go of the left mouse button.

What I did find interesting was that the object would have a slight curve to their movement. It's like moving something with a magnet through some very thick glass

You can experiment with the dbSetCameraFOV ( Angle ); command for some different results (the angles available end at 360).

why doesn't it work the way I want it to!
Alfie
16
Years of Service
User Offline
Joined: 25th Dec 2007
Location:
Posted: 3rd Feb 2008 08:13 Edited at: 11th Feb 2008 23:53
I just thought I would put this here for a direct answer to finding where your mouse position hits on a terrain object (giving you the x, y and z of the position).

simply -
int not_important_variable = dbPickObject ( dbMouseX (), dbMouseY (), terrainNo, terrainNo ) ;
// where terrainNo = the terrain object number you gave it when you created the terrain
float xHit = dbGetPickVectorX() + dbCameraPositionX();
float yHit = dbGetPickVectorY() + dbCameraPositionY();
float zHit = dbGetPickVectorZ() + dbCameraPositionZ();

so if you position an object at those xHit/yHit/zHit coords it will follow your mouse on screen (assuming your mouse is over the terrain).

In more detail - when you dbPickObject () it returns the object number you clicked on in the range of objects you specified (in this case, just the terrain object). However, more importantly it stores data about the position of the hit as a vector (relative to the camera). You simply add the vector of the camera to the dbGetPickVector stored internally and you get the position.

Login to post a reply

Server time is: 2024-10-08 15:58:58
Your offset time is: 2024-10-08 15:58:58