Jump to content

Position HUD attachment over something in-world?


Wulfie Reanimator
 Share

You are about to reply to a thread that has been inactive for 2485 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I have a HUD that's only two default prims linked together (and attached to Center), the root is placed at <0,0,0>.

In-world, I have a target that I'm tracking. I want the child prim to be placed exactly over the target as I see it on my screen, but I'm struggling with it. Here's what I have so far:

key target = "ec85be1f-21b6-d989-6412-db165e53d5e7";
vector target_pos;
default
{
    state_entry()
    {
        llRequestPermissions(llGetOwner(), PERMISSION_TRACK_CAMERA);
    }
    
    run_time_permissions(integer perm)
    {
        if(perm)
        {
            target_pos = llList2Vector(
                llGetObjectDetails(target, [OBJECT_POS]), 0);
            llSetTimerEvent(0.1);
        }
    }
    
    timer()
    {
        rotation camera_rot = llRotBetween(
            llRot2Fwd(llGetCameraRot()), target_pos - llGetCameraPos());
        vector hud_pos = <1,0,0> * camera_rot;
        llSetLinkPrimitiveParamsFast(2, [PRIM_POSITION, hud_pos]);
    }
}

Sadly, this doesn't work. The placement isn't very accurate at any point, and if you turn your camera to face towards anything but the positive global X axis, the HUD's child prim either barely moves (while perpendicular), or moves in the reverse direction on the attachment's Z axis (while facing towards the negative global X axis).

Here is an image while perpendicular: http://puu.sh/wIwLU/94651c9ffd.png
And opposite: http://puu.sh/wIwKj/1d393a073b.png
And facing towards positive X: http://puu.sh/wIwN5/1b7e639472.png

The purple square is the HUD's root, at <0,0,0>
The yellow sphere is the HUD's child.

Edited by Wulfie Reanimator
typos
Link to comment
Share on other sites

4 hours ago, Xiija said:

mebbe you can mod this, or get some ideas from it?

http://wiki.secondlife.com/wiki/Target_Crosshair_HUD

Thanks, I spent some time reading through it and trimming it down to just the essentials and renaming things a bit. Here's what I ended up with:

UpdateTargeter(integer link, key target)
{
    if (target) // Is valid
    {
        vector targetPos = llList2Vector(llGetObjectDetails(target,[OBJECT_POS]), 0);
        vector childPos  = Region2HUD(targetPos, llGetCameraPos(), llGetCameraRot());
        
        if (childPos != <-999, -999, -999>)
        {
            vector hudLocal = llGetLocalPos();
                   hudLocal = <hudLocal.x, hudLocal.y, hudLocal.z>;
            
            llSetLinkPrimitiveParamsFast(link,[
                PRIM_POSITION, childPos - hudLocal,
                PRIM_COLOR, ALL_SIDES, <1,1,1>, 1]);
        }
        else llSetLinkPrimitiveParamsFast(link,[
            PRIM_POSITION, <0.02, 0, 0>,
            PRIM_COLOR, ALL_SIDES, <1,1,1>, 0]);
    }
    else // Target not a valid key.
    {
        llSetLinkPrimitiveParamsFast(link,[
            PRIM_POSITION, <0.02, 0, 0>,
            PRIM_COLOR, ALL_SIDES, <1,1,1>, 0]);
    }
}
// The root prim has to be attached to Center with ZERO_ROTATION
vector Region2HUD(vector targetPos, vector camPos, rotation camRot)
{
    targetPos = (targetPos - camPos) * (ZERO_ROTATION / camRot);
    targetPos = <-targetPos.y, targetPos.z, targetPos.x>;
    
    float FOV = 1.7320508075688774;
    float posX = (targetPos.x * FOV) / targetPos.z;
    if (posX > -3 && posX < 3)
    {
        float posY = (targetPos.y * FOV) / targetPos.z;
        if (posY > -1 && posY < 1)
        {
            float posZ = (targetPos.z - 2) / targetPos.z;
            if (posZ > -1 && posZ < 1) return <0, -posX/2, posY/2>;
        }
    }
    return <-999, -999, -999>;
}

And now it's as simple as doing:

timer()
{
    UpdateTargeter(2, target);
}

I added a link number because I want multiple links to hover near specific targets.

Edited by Wulfie Reanimator
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2485 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...