Feb 5th, 2022
1,239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. integer     CHANNEL         = -1;
2.
3. //vector      base_initial_pos;
4. vector      cur_base_pos;
5. rotation    base_initial_rot;
6. rotation    cur_base_rot;
7.
8. vector      object_initial_pos=<-1,-1,-1>;
9. rotation    object_initial_rot;
10.
11.
12.
13. list parsecmd(string msg)
14. {
15.     return llParseStringKeepNulls(msg,["|"],["|"]);
16. }
17. rotate_around_point()
18. {
19.
20.     vector center = cur_base_pos;
21.     llOwnerSay("base pos: "+(string)cur_base_pos+ " center "+(string)center);
22.     vector point = object_initial_pos;
23.     // divide current rotation by the initial_rotation
24.     // gives the actual rotation made
25.
26.     rotation diffrot = cur_base_rot / base_initial_rot;
27.     llOwnerSay("cur_base_pos: "+(string)cur_base_pos+"object_initial_pos: "+(string)point+" rot:"+(string)cur_base_rot+" base_initial_rot:"+(string)base_initial_rot+" diffrot:"+(string)diffrot);
28.     // get actual rotation in radians
29.     vector eulero = llRot2Euler(diffrot);
30.     float theta = eulero.z;
31.
32.     vector object_pos = llGetPos();
34.     llOwnerSay("Rotating by "+(string)(theta));
35.
36.     // 1) first we need to get transform such that center is the center of coordinate
37.     float sin = llSin(theta);
38.     float cos = llCos(theta);
39.
40.     // translate point back to origin:
41.     float px = point.x - center.x;
42.     float py = point.y - center.y;
43.     llOwnerSay("px: "+(string)px+" py: "+(string)py+" sin: "+(string)sin+" cos: "+(string)cos);
44.
45.     // rotate point this is the core of rotation
46.     float xnew = px * cos - py * sin;
47.     float ynew = px * sin + py * cos;
48.
49.     // translate point back:
50.     float nx = xnew + center.x;
51.     float ny = ynew + center.y;
52.     vector newpos =  <nx,ny,point.z>;
53.     llOwnerSay("would change pos to  "+(string)newpos);
54.     //llOwnerSay("rotated by "+(string)(theta*RAD_TO_DEG)+" degrees ");
56.         PRIM_POSITION, newpos,
57.         PRIM_ROTATION, object_initial_rot*diffrot]);
58.
59. }
60.
61. default
62. {
63.     state_entry()
64.     {
65.         llListen(CHANNEL,"","","");
66.     }
67.     listen(integer channel, string name, key id, string msg)
68.     {
69.         if(object_initial_pos==<-1,-1,-1>)
70.         {
71.             llOwnerSay("MUST reset the base before touching!");
72.             return;
73.         }
74.         list result=parsecmd(msg);
75.         //llOwnerSay(llList2CSV(result));
76.         string cmd = llList2String(result,0);
77.         cur_base_pos = (vector)llList2String(result,1);
78.         //llOwnerSay("cur_base_pos: "+(string)cur_base_pos);
79.         cur_base_rot = (rotation)llList2String(result,2);
80.
81.         if(cmd=="RESET")
82.         {
83.             llOwnerSay("Reset");
84.
85.             base_initial_rot = cur_base_rot;
86.             //base_initial_pos = pos;
87.             object_initial_pos = llGetPos();
88.             object_initial_rot = llGetRot();
89.
90.         }
91.         if(cmd=="ROTATED")
92.         {
93.             rotate_around_point();
94.         }
95.
96.
97.     }
98. }