Author Topic: Get the distance from the object a player is looking at (Client-sided)  (Read 2424 times)

/title
'By the object the player is looking at', I am talking about the brick/player/item/whatever the client's crosshair is targeted on. You know, that thin white circle at the center of the screen.

I need to do this on the client's side.

You can't use raycasts client-sided (for some bullstuff reason they're disabled)
So you'll have a hard time getting the object you look at



You can't use raycasts client-sided (for some bullstuff reason they're disabled)
So you'll have a hard time getting the object you look at
math?

math?
You'd have a hard time testing every object to see if it's in the path of your raycast considering you'd need to first somehow get a group of all objects, cut out the ones that you wouldn't want to check (so you're not looping over every object), then test each using its position and bounding box. That plus there really is no super reliable way to get some of the player vectors clientsided.

You'd have a hard time testing every object to see if it's in the path of your raycast considering you'd need to first somehow get a group of all objects, cut out the ones that you wouldn't want to check (so you're not looping over every object), then test each using its position and bounding box. That plus there really is no super reliable way to get some of the player vectors clientsided.
Plus the bounding box does not always represent the object's shape. Example: an open default door.

Is there a way to determine if the object you're looking at is a player? I was thinking of looping through all of the server connection objects and seeing if they were players. And if that object's distance is very close or equal to getFocusDistance(), then it is the player you are aiming at.
But is there a more efficient way to do this?

Is there a way to determine if the object you're looking at is a player? I was thinking of looping through all of the server connection objects and seeing if they were players. And if that object's distance is very close or equal to getFocusDistance(), then it is the player you are aiming at.
But is there a more efficient way to do this?
Depends on if you want to release your add-on

Depends on if you want to release your add-on
I now think the main problem is determining what your distance is from the object.
Wait...I thought of a better method!
You can do a containerRadiusSearch for a within a certain distance and for a certain 'class'/object type, and whatever's in that search is a player.
Will this work?
EDIT: I looked at some Torque console commands documentation, and you unfortunately need to know what your own position is in order to do initContainerRadiusSearch. Is this disabled in Blockland?
« Last Edit: October 12, 2013, 09:16:16 AM by hammereditor² »

containerRadiusSearch is a type of raycast method, and it cant be done on the client, i think.

Edit: In regards to your edit, you can get your own players position easily. Something like serverConnection.getPosition() will work fine.
« Last Edit: October 12, 2013, 09:18:14 AM by boodals 2 »

containerRadiusSearch is a type of raycast method, and it cant be done on the client, i think.

Edit: In regards to your edit, you can get your own players position easily. Something like serverConnection.getPosition() will work fine.
Okay, so I have to loop through objects instead of doing an easy container search.
But how do I find the distance from each object?

You can get the objects position (assuming its not a player or a bot) just with getTransform()

However, getting the point you are aiming at is a lot harder. It can be done client sided with math and a lot of tweaking to make it accurate. I actually have some code which does this. It is not as accurate as it can be, however it is pretty good.

Code: [Select]
package packageName
{
function crouch(%a)
{
$isCrouching = %a;
return parent::crouch(%a);
}
};
activatePackage(packageName);

function getFocusPos()
{
%dist = getFocusDistance();
%eye = getMyEyePoint();
%vec = serverConnection.getControlObject().getMuzzleVector(0);
%vec = vectorScale(%vec, %dist);
return vectorAdd(%eye, %vec);
}

function getMyEyePoint()
{
%player = serverConnection.getControlObject();

if(!isObject(%player))
return "0 0 0";
%pos = %player.getPosition();
%vec = %player.getForwardVector();
%scale = %player.getScale();
%x = getWord(%pos, 0) + (getWord(%vec, 0)*0.14 + 0.002)*getWord(%scale, 0); //me no likey
%y = getWord(%pos, 1) + (getWord(%vec, 1)*0.14 + 0.002)*getWord(%scale, 1);
%z = getWord(%pos, 2) + (getWord(%player.getDatablock().boundingBox, 2)/4.92 + $isCrouching*1.53 + 0.002)*getWord(%scale, 2); //optimized for standard blockhead, good luck otherwise
return %x SPC %y SPC %z;
}

Note: This isnt my code, i think i found it on the forums somewhere after lots of searching.

EDIT: Actually, i think getYawPitch is part of another script.. I cant see it being used anywhere..

Nope, its not. Removed it.
« Last Edit: October 12, 2013, 10:04:44 AM by boodals 2 »

You can get the objects position (assuming its not a player or a bot) just with getTransform()

However, getting the point you are aiming at is a lot harder. It can be done client sided with math and a lot of tweaking to make it accurate. I actually have some code which does this. It is not as accurate as it can be, however it is pretty good.

Code: [Select]

Note: This isnt my code, i think i found it on the forums somewhere after lots of searching.

EDIT: Actually, i think getYawPitch is part of another script.. I cant see it being used anywhere..

Nope, its not. Removed it.
Are any of those a replacement for getEyeVector?

While you cant get eye vectors, you can get muzzle vectors, which is the same (or very slightly different).
Code: [Select]
%vec = serverConnection.getControlObject().getMuzzleVector(0);