Author Topic: 3D MousePos Raycast  (Read 1005 times)

Hello Coding Help,

How would I cast a ray from my mouse position on the screen, so the game would know what object I'm pointing at?

Pretty much how the Mission Editor works.

could probably check FoV as well as how far to the edge of the screen the cursor is
then a bit of trig later you could find the angle difference from your eyevector


isn't that the XY coordinate?
not the angle that needs to be checked or whatever is under the cursor?

Well, that's just part if it, the rest can be a little of math to see if it is in the "zone"

So basically doing it accurately requires matrix math and stuff
Here is Port's implementation of the matrix functions required to do this
https://github.com/portify/projection
Then here's a function I made to get the location that you clicked at
Code: [Select]
function RRscreenToWorld(%x, %y)
{
%xFac = getWord(getRes(), 0)/2;
%yFac = getWord(getRes(), 1)/2;
%newX = (%x - %xFac) / %xFac;
%newY = -(%y - %yFac) / %yFac;

%start = projectScreenToWorld(%newX SPC %newY SPC "-1 1");
%end = projectScreenToWorld(%newX SPC %newY SPC "0.999999 1");

%mask = $TypeMasks::All;
%player = serverConnection.getControlObject();
%scanTarg = containerRaycast(%start, %end, %mask, %player);
if(!%scanTarg)
{
echo("No intersection!");
return;
}
%pos = posFromRaycast(%scanTarg);
return %pos;
}
Where %x and %y are the cursor's (x,y)

Also for background on how it actually works, look up the pipeline for how OpenGL renders. It basically reverses this process.
« Last Edit: October 06, 2015, 10:03:07 PM by ZSNO »

I thought containerRaycast was only server side? I must be thinking of something else because it does exist on the client side.

I thought containerRaycast was only server side? I must be thinking of something else because it does exist on the client side.
Maybe, my code is for a server/client mod.
« Last Edit: October 07, 2015, 12:52:20 AM by ZSNO »

Well, I started up a BL client and that raycast function exists.

But you can't do raycasts client sided.

But you can't do raycasts client sided.

Yes, that's why that code was written to just be able to compute the different parameters as needed and not actually do the raycast. It's entirely client-sided. Common usage would be to send the -1 depth and 1 depth unprojected points to the server and have it raycast between them.

Yes, that's why that code was written to just be able to compute the different parameters as needed and not actually do the raycast. It's entirely client-sided. Common usage would be to send the -1 depth and 1 depth unprojected points to the server and have it raycast between them.

Code: [Select]
%xFac = getWord(getRes(), 0)/2;
%yFac = getWord(getRes(), 1)/2;
%start = projectScreenToWorld(%newX SPC %newY SPC "-1 1");
%end = projectScreenToWorld(%newX SPC %newY SPC "0.999999 1");
%player = serverConnection.getControlObject();
%scanTarg = containerRaycast(%start, %end, %mask, %player);
}

Maybe, my code is for a server/client mod.

By "that code" I was referring to my projection library which is used by what ZSNO posted