Need help with 3D Cursor

Posted By: Espér

Need help with 3D Cursor - 12/02/11 20:15

hi there

i´ve a little problem with my Editor..
I want to change from keyboard controls, to mouse control.

Problem.. i´ve no clue how to set the 3D Block on the position of the mouse ._.

c_trace isn´t working, cause there´s nothing the trace can hit...

That´s how the Editor looks like:


This yellow transparent block is the 3D Cursor, the small thingy right is the mouse cursor.
The Grid is drawn with draw_line3d.

The Editor View is a Second view (not the camera) called "edit_view".


The Block should move on his X and Z Axis.. BUT NOT on his Y. Cause the Y position will be changed with the Mouse Wheel.


Can someone help me here? ._.
Posted By: Myrkling

Re: Need help with 3D Cursor - 12/02/11 23:19

If I understood you correctly, this code might help you getting along: http://www.opserver.de/wiki/index.php/Plane-Line_Intersection

You can construct a ray/line through the mouse cursor with mouse_dir3d and mouse_pos3d. The grid acts as a plane. You can then place the cube at the intersection point.
Posted By: Espér

Re: Need help with 3D Cursor - 12/04/11 21:38

uhm.. what?
Didn´t get it ._.
Posted By: WretchedSid

Re: Need help with 3D Cursor - 12/04/11 21:46

What Myrkling suggests is that you check if the imaginary line from the mouse cursor to some hypothetical position far in front of the mouse cursor, and check if and where it intersects with your grid.

If you also want to check if an entity is hit, you should first c_trace() along the line and if it hits nothing fall back to the intersection check.
Posted By: Espér

Re: Need help with 3D Cursor - 12/04/11 22:08

and how can i check that intersection with the grid?
Posted By: WretchedSid

Re: Need help with 3D Cursor - 12/04/11 23:25

Dude, he posted a link with working code and everything oO
Posted By: Pappenheimer

Re: Need help with 3D Cursor - 12/04/11 23:54

Why don't you simply add a plain terrain at the height of the grid, and then use a trace with mouse_pos3d and mouse_dir3d like the example from the manual?

http://www.conitec.net/beta/mouse_pos3d.htm

As soon as the player places a Terrain, he wants to place the things at the height of the hills or valley anyway.
Posted By: Espér

Re: Need help with 3D Cursor - 12/05/11 09:11

yes, but what do i need to fillin plane_point or plane_normal or ip?

i tried a terrain, but when i moved it with the camera to the sides and up and down, the framerate was killed..
Posted By: WretchedSid

Re: Need help with 3D Cursor - 12/05/11 10:38

Originally Posted By: Espér
yes, but what do i need to fillin plane_point or plane_normal or ip?

I... are you kidding us?

Quote:

// line_from First arbitrary point on the line.
// line_to Second arbitrary point on the line (must not be equal to line_from).
// place_point Arbitrary point on the plane.
// plane_normal Surface normal of the plane.
// ip [out] Vector to be set to the intersection point. May be NULL.

Posted By: Espér

Re: Need help with 3D Cursor - 12/05/11 10:38

i´ve no plane ._.
the room is complete empty..
Posted By: WretchedSid

Re: Need help with 3D Cursor - 12/05/11 10:46

You definitely are kidding us oO
The plane is imaginary, it assumingly starts at 0|0 and expands along the X and Y axis but stays 0 along the Z axis. Then an arbitrary point on this plane would be 0|0|0.
The normal of your imaginary plane is probably 0|0|1 since your plane faces upwards.
Posted By: Espér

Re: Need help with 3D Cursor - 12/05/11 10:57

k.. i don´t understand it..

i think i´ll shut down my PC now and get well.. perhaps i can think clearer when i´m not ill ._.

Thanks anyway...
Posted By: Carlos3DGS

Re: Need help with 3D Cursor - 12/06/11 21:25

I programmed the same thing as the link they gave you, so I might be able to help explain it.

as you know a line can be represented mathematically in two ways.
1- two points
2- a point and a direction

planes work the same way and they can also be calculated in two ways.
1- three points
2- a point and a normal

This information is only nneded to calculate it mathematically, you don't need a real phisical line, and you dont need a real phisical terrain either.


In his funtion he is using as input for the line the first method (two points), and for the imput to calculate the plane he is using the second method (a point and a normal).
-to give the inputs for the line you just need to give the function any two points on the line. for example (in your case) transforming the cursor's screen coords to the 3d view with a depth of 1, and transforming the cursor coords again to 3d coords with a depth of 2.
-to give the inputs for the plane you need to give it ANY point that is ON the imaginary plane, and the imaginary plane's normal. for example (in your case) if you wanted the plane to be at a height of z=0 you can give it any point on that plane. that means that you could pass any value for x and any value for y as long as you passed 0 for the z value (since all points with z=0 would be on that imaginary plane's surface).
For the normal you would give 0,0,1 if you want your plane's surface to be horizontal since that would mean it's surface normal would be pointing upwards.

You only need the values of this imaginary line and the imaginary plane so he can calucate it mathematically, you do not need to have them phisically loaded.

the output will be either a coordinate where they intersect, or NULL if the line is parallel to the plane (because then it never intersects with our imaginary plane).

How the function does this is irrelevant but if you wish to know... with the two vectors of the line it calculates the formula that defines that line, and with the point and normal for the plane it calculates the formula that will define that plane. then it simply plugs the values for one formula into the other formula and the result is the intersection of both. If you want to achieve this yourself you can read about it in any basic calculus book to learn how to create a formula that defines a line, how to create the formula that defines a plane, and how to calculate the intersection of both.

If you wish to learn in a more visual way, follow the links below for an online video demonstration:
LINES: http://www.youtube.com/watch?v=GFM8NOe_XM4
PLANES: http://www.youtube.com/watch?v=3QLaud6SnHM
INTERSECTIONS: http://www.youtube.com/watch?v=-_DStDQffQo

and here is the best (and most detailed) video I found on this subject. This video covers everything from 2d line equations and the basic concepts of planes, all the way to the more complex stuff you can do with this in 3d (like line and plane intersections).
http://www.youtube.com/watch?v=Q0gnrV699GQ
in my opinion this last video is a must-watch for anyone programming in 2d/3d
Hope I helped ;D
© 2024 lite-C Forums