Author Topic: Drawing a point client sided based on a coordinate  (Read 1710 times)

how would I draw a point on the client's screen based on a coordinate sent from the server?
I have literally no idea where to start


Strangely enough, i've been working with this fairly recently. After searching around for ages, I came across a function. I cant remember where I found it, of who made it, but its fairly accurate. The Y coordinate is too high (appears below) around the sides of the screen, at least it is for me.

Btw, this is all client sided. You definitely should use the server to get positions
Code: [Select]
function worldToScreen(%posb)
{
%self = serverConnection.getControlObject(); //Replace with player
%posa  = getMyEyePoint(); //Replace with player eyepos

%fv    = %self.getMuzzleVector(0); //Replace with eye vector
%yp    = getYawPitch(%fv,%posa,%posb);
%yaw   = getWord(%yp,0);
%pitch = getWord(%yp,1);

%halfx = getWord($Pref::Video::Resolution,0) / 2;
%halfy = getWord($Pref::Video::Resolution,1) / 2;
%fovx  = mDegToRad($CameraFov / 2);
%fovy  = mATan(mTan(%fovx) * %halfy / %halfx,1);

if(%yaw > %fovx)
%yaw = %fovx;
if(%yaw < -%fovx)
%yaw = -%fovx;
if(%pitch > %fovy)
%pitch = %fovy;
if(%pitch < -%fovy)
%pitch = -%fovy;

%ratx  = mTan(%yaw)   * mCos(%fovx) / mSin(%fovx);
%raty  = mTan(%pitch) * mCos(%fovy) / mSin(%fovy);

%x = mFloatLength(%ratx * %halfx + %halfx - getWord(%gui.extent,0) / 2,0);
%y = mFloatLength(%raty * %halfy + %halfy - getWord(%gui.extent,1) / 2,0);

%y -= (%x-getWord(%gui.extent, 0))/100;

return %x SPC %y;
}

function mRound(%num)
{
return mFloatLength(%num, 0);
}

function getYawPitch(%fv,%posa,%posb)
{
%x  = getWord(%fv,0);
%y  = getWord(%fv,1);
%vv = vectorNormalize(vectorSub(%posb,getWords(%posa,0,2)));
%xx = getWord(%vv,0);
%yy = getWord(%vv,1);

%yaw   = mATan(%xx,%yy) - mATan(%x,%y);
%pitch = mATan(getWord(%fv,2),mSqrt(%x  * %x  + %y  * %y )) -
         mATan(getWord(%vv,2),mSqrt(%xx * %xx + %yy * %yy));

if(%yaw   >  3.14159) %yaw   = -6.28318 + %yaw;
if(%yaw   < -3.14159) %yaw   =  6.28318 + %yaw;
if(%pitch >  3.14159) %pitch = -6.28318 + %pitch;
if(%pitch < -3.14159) %pitch =  6.28318 + %pitch;

return %yaw SPC %pitch;
}

After searching around for ages, I came across a function. I cant remember where I found it, of who made it, but its fairly accurate.
That's Truce's code. At one point both of us tried to make a client-side 3D compass, and his was the first to come out successfully.

That's Truce's code. At one point both of us tried to make a client-side 3D compass, and his was the first to come out successfully.
Haha thats cool, however this code isn't accurate at different altitudes do you by any chance have more reliable code?

Haha thats cool, however this code isn't accurate at different altitudes do you by any chance have more reliable code?

Well, the code posted is basically what I had - it's kind of a blend of Trigun's original idea and what I made it into. But mine didn't work at the diagonal edges of the screen (top left, bottom left, etc). So if that's what you're referring to, you'd need a different algorithm, since this one doesn't work properly regardless of the code's version.

When I saw the code I searched my Torque folder for "worldToScreen." It looked like my code, but the function name was dumb, so that's why I searched. I found this code from someone else (Trigun?), and there must have been a reason I wasn't using it, but maybe give it a try:

Code: [Select]
function worldToScreen(%eyeTransform, %worldPosition, %screenExtent, %cameraFoV)
{
%offset = MatrixMulVector("0 0 0" SPC getWords(%eyeTransform, 3, 5) SPC -1 * getWord(%eyeTransform, 6), VectorSub(%worldPosition, %eyeTransform));
%x = getWord(%offset, 0);
%y = getWord(%offset, 1);
%z = getWord(%offset, 2);

%fovFactor = %y * (%cameraFoV != 90 ? mTan(%cameraFoV * $pi / 360) : 1);
%screenWidth = getWord(%screenExtent, 0);
%screenHeight = getWord(%screenExtent, 1);

%screenX = ((%x / %fovFactor) + 1) / 2 * %screenWidth;
%screenY = ((%z / %fovFactor * %screenWidth) - %screenHeight) / -2;

return %screenX SPC %screenY;
}
« Last Edit: August 09, 2013, 01:11:01 AM by Truce »

Oh thanks, ill try that when i can get on my computer

I have also worked on this as a matter of fact.  The problem is that it is pretty much impossible to client sidedly figure out the camera position of a client in third person, and I figure being restricted to first person on these kinds of things is pretty lame.

I have also worked on this as a matter of fact.  The problem is that it is pretty much impossible to client sidedly figure out the camera position of a client in third person, and I figure being restricted to first person on these kinds of things is pretty lame.
being forced in first person is perfectly fine

Code: [Select]
function worldToScreen(%eyeTransform, %worldPosition, %screenExtent, %cameraFoV)
{
%offset = MatrixMulVector("0 0 0" SPC getWords(%eyeTransform, 3, 5) SPC -1 * getWord(%eyeTransform, 6), VectorSub(%worldPosition, %eyeTransform));
%x = getWord(%offset, 0);
%y = getWord(%offset, 1);
%z = getWord(%offset, 2);

%fovFactor = %y * (%cameraFoV != 90 ? mTan(%cameraFoV * $pi / 360) : 1);
%screenWidth = getWord(%screenExtent, 0);
%screenHeight = getWord(%screenExtent, 1);

%screenX = ((%x / %fovFactor) + 1) / 2 * %screenWidth;
%screenY = ((%z / %fovFactor * %screenWidth) - %screenHeight) / -2;

return %screenX SPC %screenY;
}
this doesn't work, thanks for trying though
I'm wondering how the shape name works