Feb 5th, 2022
1,161
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. rotation    base_initial_rot;
5.
6. vector      object_initial_pos;
7. rotation    object_initial_rot;
8.
9.
10.
11. list parsecmd(string msg)
12. {
13.     return llParseStringKeepNulls(msg,["|"],["|"]);
14. }
15. rotate_around_point(rotation rot, vector pos)
16. {
17.     vector center = pos;
18.     vector point = object_initial_pos;
19.     // divide current rotation by the initial_rotation
20.     // gives the actual rotation made
21.
22.     rotation diffrot = rot / base_initial_rot;
23.
24.     // get actual rotation in radians
25.     vector eulero = llRot2Euler(diffrot);
26.     float theta = eulero.z;
27.
28.     vector object_pos = llGetPos();
31.
32.     // 1) first we need to get transform such that center is the center of coordinate
33.     float sin = llSin(theta);
34.     float cos = llCos(theta);
35.
36.     // translate point back to origin:
37.     float px = point.x - center.x;
38.     float py = point.y - center.y;
39.
40.     // rotate point this is the core of rotation
41.     float xnew = px * cos - py * sin;
42.     float ynew = px * sin + py * cos;
43.
44.     // translate point back:
45.     float px = xnew + center.x;
46.     float py = ynew + center.y;
47.     vector newpos =  <px,py,point.z>;
48.     //llOwnerSay("rotated by "+(string)(theta*RAD_TO_DEG)+" degrees ");
50.         PRIM_POSITION, newpos,
51.         PRIM_ROTATION, object_initial_rot*diffrot]);
52. }
53.
54. default
55. {
56.     state_entry()
57.     {
58.         llListen(CHANNEL,"","","");
59.     }
60.     listen(integer channel, string name, key id, string msg)
61.     {
62.         list result=parsecmd(msg);
63.         llOwnerSay(llList2CSV(result));
64.         string cmd = llList2String(result,0);
65.         vector pos = llList2Vector(result,1);
66.         rotation rot = llList2Rot(result,2);
67.         if(cmd=="RESET")
68.         {
69.             llOwnerSay("Reset");
70.
71.             base_initial_rot = rot;
72.             //base_initial_pos = pos;
73.             object_initial_pos = llGetPos();
74.             object_initial_rot = llGetRot();
75.
76.         }
77.         if(cmd=="ROTATED")
78.         {
79.             //llOwnerSay("Rotated");
80.             rotate_around_point(rot,pos);
81.         }
82.
83.
84.     }
85. }