get the point where a line crosses an imaginary plane?

Posted By: Rackscha

get the point where a line crosses an imaginary plane? - 08/23/11 13:48

Hi,
When i have my mousecursor at the screen. I can use the direction to send a trace. But how can i determine if my line cuts a plane?

For this specific situation:


Iam looking at my surface made out of different cubes(atm).
Now i need to calculate which cube is below my cursor to select it. Since i might consider using a single mesh later, i cannot use something like SHOOT. i need to calculate the point where my cursor trace hits the same plane as my surface.
Bets ist, if its entity independend.

Maybe someone has an idea?

EDIT: since all of my blocks are aligned on my grid, i just need the global position to calculate the required block.

Posted By: Quad

Re: get the point where a line crosses an imaginary plane? - 08/23/11 14:12

hit.x hit.y hit.z gives world coordinates of last c_trace
Posted By: Rackscha

Re: get the point where a line crosses an imaginary plane? - 08/23/11 14:37

mh, okay. i'll try that one.
But an entity independend solution is still better. Imagine i remove one of those blocks, but still need to be able to select the space of this block. With a trace it would go thorugh the empty block(s). So PlaneIntersection seems more sufficient.
Posted By: Pappenheimer

Re: get the point where a line crosses an imaginary plane? - 08/23/11 14:42

http://www.opserver.de/wiki/index.php/Additional_vector_functions_by_Kombucha
Posted By: Rackscha

Re: get the point where a line crosses an imaginary plane? - 08/23/11 15:37

still doing something wrong.

Is it correct that the plane normal is (0,0,1) for intersecting with a xy plane?
How do i correctly send the line from the mouse straight into the 3dspace. Have problems with mouse_dir3d etc (yes setting mousepos to mouse_cursor)

and is the origina of the plane correct if (0,0,0) ?

Maybe someone can help me a bit. My brain is cooking here at 30 dgree celcius >.<
Posted By: Slin

Re: get the point where a line crosses an imaginary plane? - 08/23/11 17:13

vec_for_screen() projects your vector into your world, if you just have the xy plane as plane, your normal is 0, 0, 1 and 0, 0, h is a good origin. With that knowledge you can build the HNF for the plane, which is something like:
Code:
0
0  *  v - h = a
1



vec_for_screen(vector(0, 0, 1), camera) - camera.xyz (you will have to use some temp vector and vec_substract or something like that) gives you the direction of your line and camera.xyz is a position on it.
So as a next step you have to enter your cameras position as v into the hnf:
Code:
camera.z - h = a



After this, a is the cameras distance to the plane. If you now normalize your direction, multiply it with a/z and add the cameras position, you´ve got your point:
Code:
mousedir*((camera.z-h)/mousedir.z)+camera.xyz = position



It is a bit more tricky with an arbitary normal, but the approach is basicly the same and this should already do it for your example.
Posted By: Rackscha

Re: get the point where a line crosses an imaginary plane? - 08/23/11 17:44

thanks, i'll try that

edit: what is h?

edit2: ah dump, h is my z distance to the plane >.<
Posted By: Slin

Re: get the point where a line crosses an imaginary plane? - 08/23/11 17:53

No, h is your planes z position.
Posted By: Rackscha

Re: get the point where a line crosses an imaginary plane? - 08/23/11 18:40

mh seems something is still wrong here.
my point always ends up with z > 500.

Sorry to ask, but can you deliver a full example? something is absolutely wrong here >.<
Posted By: Myrkling

Re: get the point where a line crosses an imaginary plane? - 08/23/11 22:29

I planned to replace the additional vector functions bit by bit with smaller and clearer code snippets. This seems to be the right occasion to continue this plan.

Here is the new line-plane intersection test:
http://www.opserver.de/wiki/index.php/Plane-Line_Intersection

You can use it like that:
Code:
#include <acknex.h>

<<<INCLUDE LINE-PLANE INTERSECTION TEST CODE HERE>>>

void main()
{
    level_load(NULL);
    mouse_mode = 4;
    
    vec_set(camera.x, vector(-200,0,300));
    camera.tilt = -45;
    
    // Create a simple floor.
    ENTITY *cube = ent_create(CUBE_MDL, NULLVECTOR, NULL);
    cube.z -= cube.max_z;
    vec_set(cube.scale_x, vector(8,8,1));
    
    while(1)
    {
        VECTOR to;
        vec_set(to,mouse_dir3d);
        vec_add(to,mouse_pos3d);
    
        // Get the intersection between the mouse ray and the floor.
        VECTOR *ip = get_line_plane_intersection(mouse_pos3d, to, NULLVECTOR,
            vector(0,0,1), NULL);

        if (ip) // Draw intersection point.
            draw_point3d(ip, COLOR_RED, 100, 6);
            
        wait(1);
    }
}


Just insert the intersection test code and compile it to see it in action.
Posted By: Slin

Re: get the point where a line crosses an imaginary plane? - 08/24/11 09:49

Code:
mouse_dir3d*((camera.z-plane.z)/mouse_dir3d.z)+camera.xyz = position

Lets assume that you levels origin is within the plane and that
the camera is placed at (0|0|100):
                                    0
mouse_dir3d*(100/mouse_dir3d.z) +   0 = position
                                  100

                        1
Now, if mouse_dir3d is  1 / sqrt(3), the whole thing with values 
                       -1
looks like this:


 1                             0   -100
 1 / sqrt(3) * -173.205081 +   0 = -100
-1                           100    200

As you see this is wrong, as the correct position would be
(100|100|0)... Inverting mouse_dir3d would give correct results.
I wonder where I missed the stupid sign -.-


This however, should then just work:

void traceMouse(VECTOR *pos)
{
  vec_set(pos, mouse_dir3d);
  vec_normalize(pos, -1.0);
  vec_scale(pos, camera.z/pos.z);
  vec_add(pos, camera.x);
}


Posted By: Rackscha

Re: get the point where a line crosses an imaginary plane? - 08/24/11 10:25

eh... the function you wrote os only working when iam below the plane and looking up.

it needs to be^^

Code:
vec_scale(pos, -camera.z/pos.z);



now its working grin

edit: @Myrkling: Sorry havent seen your answer. Thx for your effort too laugh
Posted By: Slin

Re: get the point where a line crosses an imaginary plane? - 08/24/11 11:18

Code:
void traceMouse(VECTOR *pos)
{
  vec_set(pos, mouse_dir3d);
  vec_normalize(pos, 1.0);
  vec_scale(pos, camera.z/pos.z);
  vec_add(pos, camera.x);
}


Should then be a nicer solution. Now I am really confused...^^

Edit:
Code:
void intersectMouseGroundplane(VECTOR *pos, var height)
{
  vec_set(pos, mouse_dir3d);
  vec_normalize(pos, 1.0);
  vec_scale(pos, (camera.z-height)/pos.z);
  vec_add(pos, camera.x);
}


© 2023 lite-C Forums