Hello everyone, I have a question about my car door script. The door is not opening properly as it rotates 90 degrees and its pivot point or hinge is in the middle. I tried adjusting the x-axis of the vector open_position variable, but it seems to adjust its position first and then perform the rotation.. Please see the video here.
Could someone please suggest a solution to fix the pivot/hinge and other issues? Any advice or tips would be greatly appreciated.
Thank you in advance for your help.
// config
vector close_position = <-0.570, 0.190, 1.865>;
rotation close_rotation = <0.0, 0.90, 0.0, -1.0>;
vector open_position = <-0.770, 0.190, 1.865>;
rotation open_rotation = <0.00, 0.00, 0.00, 0.00>;
key close_sound = "876d3492-b8b8-7745-f5ae-dc5176ec6105";
key open_sound = "9a076511-9aa8-f50d-e34c-1396a07a66a9";
key lock_sound = "37e8d62e-ba8a-3faf-49cc-7cb9267686da";
key unlock_sound = "37e8d62e-ba8a-3faf-49cc-7cb9267686da";
key chime_sound = "b2aa9559-4e32-8321-09f8-86572fd5bc00";
float close_volume = 1.0;
float open_volume = 1.0;
float lock_volume = 0.5;
float unlock_volume = 0.5;
float chime_volume = 0.5;
integer channel = 0;
integer auto_close = 0;
float movement_time = 1.0;
integer preload_sounds = FALSE;
integer sit_check = FALSE;
list whitelist = [""];
string link_name = "";
vector link_close_position = <0.00000, 0.00000, 0.00000>;
rotation link_close_rotation = <0.00000, 0.00000, 0.00000, 0.00000>;
vector link_open_position = <0.00000, 0.00000, 0.00000>;
rotation link_open_rotation = <0.00000, 0.00000, 0.00000, 0.00000>;
//
integer close_open;
integer handle;
integer link;
vector link_time_position;
rotation link_time_rotation;
integer locked;
integer sitting;
float time;
vector time_position;
rotation time_rotation;
// http://wiki.secondlife.com/wiki/Interpolation/Linear/Vector
vector vLin(vector x, vector y, float t){
return x*(1-t) + y*t;
}
// http://wiki.secondlife.com/wiki/Slerp
rotation slerp( rotation a, rotation b, float t ) {
return llAxisAngle2Rot( llRot2Axis(b /= a), t * llRot2Angle(b)) * a;
}
close_or_open()
{
if(llGetLinkNumber() < 2)
return;
if(llGetLocalPos() != close_position)
{
llStopSound();
llResetTime();
while(llGetTime() < movement_time)
{
time = llGetTime() / movement_time;
time_position = vLin(open_position, close_position, time);
time_rotation = slerp(open_rotation, close_rotation, time);
if(link != 0)
{
link_time_position = vLin(link_open_position, link_close_position, time);
link_time_rotation = slerp(link_open_rotation, link_close_rotation, time);
}
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POS_LOCAL, time_position, PRIM_ROT_LOCAL, time_rotation]);
if(link != 0)
llSetLinkPrimitiveParamsFast(link, [PRIM_POS_LOCAL, link_time_position, PRIM_ROT_LOCAL, link_time_rotation]);
}
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POS_LOCAL, close_position, PRIM_ROT_LOCAL, close_rotation]);
if(link != 0)
llSetLinkPrimitiveParamsFast(link, [PRIM_POS_LOCAL, link_close_position, PRIM_ROT_LOCAL, link_close_rotation]);
if(close_sound != "")
llTriggerSound(close_sound, close_volume);
}
else
{
if(open_sound != "")
llTriggerSound(open_sound, open_volume);
llResetTime();
while(llGetTime() < movement_time)
{
time = llGetTime() / movement_time;
time_position = vLin(close_position, open_position, time);
time_rotation = slerp(close_rotation, open_rotation, time);
if(link != 0)
{
link_time_position = vLin(link_close_position, link_open_position, time);
link_time_rotation = slerp(link_close_rotation, link_open_rotation, time);
}
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POS_LOCAL, time_position, PRIM_ROT_LOCAL, time_rotation]);
if(link != 0)
llSetLinkPrimitiveParamsFast(link, [PRIM_POS_LOCAL, link_time_position, PRIM_ROT_LOCAL, link_time_rotation]);
}
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POS_LOCAL, open_position, PRIM_ROT_LOCAL, open_rotation]);
if(link != 0)
llSetLinkPrimitiveParamsFast(link, [PRIM_POS_LOCAL, link_open_position, PRIM_ROT_LOCAL, link_open_rotation]);
if(auto_close != 0)
llSetTimerEvent(auto_close);
if(chime_sound != "")
llLoopSound(chime_sound, chime_volume);
}
}
find_link()
{
integer prims = llGetNumberOfPrims();
integer index;
while(prims--)
{
string name = llGetLinkName(index);
if(name == link_name)
link = index;
index++;
}
}
default
{
state_entry()
{
llSetMemoryLimit(llGetUsedMemory() + 1024);
handle = llListen(channel, "", llGetOwner(), "");
find_link();
if(preload_sounds == TRUE)
{
if(close_sound != "")
llPreloadSound(close_sound);
if(open_sound != "")
llPreloadSound(open_sound);
if(lock_sound != "")
llPreloadSound(lock_sound);
if(unlock_sound != "")
llPreloadSound(unlock_sound);
if(chime_sound != "")
llPreloadSound(chime_sound);
}
}
on_rez(integer start_param)
{
llResetScript();
}
touch_start(integer total_number)
{
key id = llDetectedKey(0);
string name = llGetUsername(id);
if(id == llGetOwner())
{
close_or_open();
return;
}
if(locked == FALSE)
close_or_open();
else if(locked == TRUE)
{
integer find = llListFindList(whitelist, [name]);
if(find != -1)
close_or_open();
}
}
timer()
{
llSetTimerEvent(0.0);
if(llGetLocalPos() != close_position)
close_or_open();
}
listen(integer channel, string name, key id, string message)
{
if(sit_check == TRUE)
if(sitting == FALSE)
return;
message = llToLower(message);
if(message == "close")
if(llGetLocalPos() != close_position)
close_or_open();
if(message == "open")
if(llGetLocalPos() != open_position)
close_or_open();
if(message == "unlock")
{
locked = FALSE;
llTriggerSound(unlock_sound, unlock_volume);
}
if(message == "lock")
{
locked = TRUE;
llTriggerSound(lock_sound, lock_volume);
}
}
changed(integer change)
{
if(change & CHANGED_LINK)
{
find_link();
key av = llAvatarOnLinkSitTarget(LINK_ROOT);
if(av == llGetOwner())
if(av)
sitting = TRUE;
else
sitting = FALSE;
}
}
}