Jump to content

[solved] Trigonometry help needed


LaurisFashion
 Share

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

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

Recommended Posts

Hi fellow scripters and builders!

I'm working on a control HUD to one of my upcoming projects, and I've kind of hit a dead end thanks to my insufficient trigonometry. What I need is the following:
I've got a face on the HUD. It's middle point represents a pivot. The HUD wearer can click the face, and I need to be able to get the angle (calculated from the "upwards" Z axis) from that.

The click provides coordinates in the form of a vector <u,v,0> where U is the horizontal coord, V is the vertical coord, and they both are a range from 0.0 to 1.0 - using function llDetectedTouchUV()

I'm attaching a visualization of what I have in mind. I'm sure this is a trivial problem, but for someone who had long forgotten everything about trigonometry, it's a total blocker.

Thank you to whoever will be able to help me out! ♥

 

 

face-trigonometry.png

Edited by LaurisFashion
Solved
Link to comment
Share on other sites

I would use llDetectedTouchST() to get the coordinates (X,Y) and use the formula cosv= vector(A) x vector(B)/length(A)*length(B) to find the angle.

To my knowledge there is no easy way to do it  - unless there are some handy LSL functions B|

Edited by Rachel1206
Link to comment
Share on other sites

Alright, never mind!

I've opened some old math books and ran through LSL documentation, and this is a working solution. Feel free to use it if you need to achieve a similar effect.

integer TouchF;
vector TouchST;
float Radian;
float Angle;

default
{
    state_entry()
    {
        llSetText("",ZERO_VECTOR,0);
    }

    touch_start(integer total_number)
    {
        TouchF = llDetectedTouchFace(0);
        // We are using llDetectedTouchST(), because that one gets coords from the face
        // while llDetectTouchUV() gets coords from texture. Since we're rotating the
        // texture, that would yield ever changing wrong results.
        TouchST = llDetectedTouchST(0);
        
        // Only apply when the touch is on the interactive face
        if(TouchF == 0)
        {
            Radian = llAtan2(TouchST.x - 0.5, TouchST.y - 0.5);
            llRotateTexture(Radian*-1,0);
            
            // We can use the degrees of rotation for anything later.
            Angle = Radian * RAD_TO_DEG;
        }
    }
}

 

Link to comment
Share on other sites

2 minutes ago, Rachel1206 said:

I would use llDetectedTouchST() to get the coordinates (X,Y) and use the formula cosv= vector(A) x vector(B)/length(A)*length(B) to find the angle.

Thanks!

The llDetectedTouchST() is definitely a good call. I tried the UV version and since I'm rotating the texture on that face, it was giving me crazy wild results. ;)
On the other hand I used llAtan2(), which seems to be made specifically for such a purpose :)

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2174 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...