Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Alignment

Kimmy Gable
Registered User
Join date: 8 Apr 2006
Posts: 14
12-16-2009 03:30
Hello all, I have a question regarding an objects position.

Ok Say you have your object rezzed and its facing 130 degrees or what ever it might be. Is there a way that I can touch the object or use a menu to have it find the nearest 90 degree? so it would turn on its own to the closest 90 degree angle.

So say its sitting at 64, you touch it an it rotates to 90. So its straight.

Any help would be great thank you.
DR Dahlgren
Content Creator
Join date: 27 Aug 2006
Posts: 79
Besides the obvious?
12-16-2009 04:35
I assume you mean besides the obvious of using the edit panel. You could do a fairly simple script that would get the objects rotation, compare that rotation on the world Z axis to a set of values that would show which 90 point it was closest to and then move to the nearest world 90 rotation. I use this type of script to orient my wind turbines to the SL wind direction and for drag racers so they are aligned with the start line.

DRD
_____________________
DR Dahlgren
Dahlgren Engineering and Design
Connecting Your Worlds
Kimmy Gable
Registered User
Join date: 8 Apr 2006
Posts: 14
12-16-2009 04:39
Yes exactly what I am talking about. But I have no idea how to even start it. I am wanting my car to line up straight. So say I put it in third gear it would find the nearest 90 and straigten up. ANY help in the right direction would be great.
Pete Olihenge
Registered User
Join date: 9 Nov 2009
Posts: 315
12-16-2009 05:09
This is a basic approach to what you're after, I think:

CODE

default
{
touch_start (integer count)
{
vector rot = llRot2Euler (llGetRot ()) * RAD_TO_DEG;
if (rot.z < 0.0) rot.z += 360.0;
if (rot.z <= 45.0 || rot.z > 315.0) llSetRot (llEuler2Rot (< rot.x, rot.y, 0.0> * DEG_TO_RAD));
else if (rot.z <= 135.0) llSetRot (llEuler2Rot (< rot.x, rot.y, 90.0> * DEG_TO_RAD));
else if (rot.z <= 225.0) llSetRot (llEuler2Rot (< rot.x, rot.y, 180.0> * DEG_TO_RAD));
else llSetRot (llEuler2Rot (< rot.x, rot.y, 270.0> * DEG_TO_RAD));
}
}
Kimmy Gable
Registered User
Join date: 8 Apr 2006
Posts: 14
12-16-2009 05:33
Thank you, semi what I was looking for but I can fill in the blanks!
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-16-2009 09:21
CODE

float vFltFacing;
float vIntModify;
float vIntPIk = (integer)(PI * 1000);

//-- in radians (preferred)
vFltFacing *= 1000;
vFltFacing += vIntPIk;
if ((vIntModify = llRound( vFltFacing ) % (vIntPIk / 2)) < (vIntPIk / 4)){
vFltFacing -= vIntModify;
}else{
vFltFacing += (vIntPIk / 4) - vIntModify;
}
vFltFacing -= vIntPIk;
vFltFacing /= 1000.0;

//-- in degrees
vFltFacing *= 1000; //-- increase range to preserve 3 decimal places
vFltFacing += 180000; //-- shift range to get a positive numbers
if ((vIntModify = llRound( vFltFacing ) % 90000) < 45000){ //-- check which is the closer movement
vFltFacing -= vIntModify; //-- subtract difference if closer to the next lowest
}else{
vFltFacing += 45000 - vIntModify; //-- add missing difference if closer to next highest
}
vFltFacing -= 180000; //-- shift range to get balanced +/-
vFltFacing /= 1000.0; //-- decrease total range back to normal preserving decimal places


ETA:
corrected for modulus clashes with floats and fractional preservation to 3 places

alternative to above
CODE

vFltModify = vFltFacing + PI;
while (vFltModify < PI_BY_TWO) vFltModify -= PI_BY_TWO; //-- modulus for floats
if (vFltModify < (PI_BY_TWO / 2.0)){
vFltModify = -vFltModify;
}else{
vFltModify = PI_BY_TWO - vFltModify
}
vFltFacing += vFltModify;
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Tharkis Olafson
I like cheese
Join date: 17 Nov 2004
Posts: 134
12-16-2009 11:39
From: Void Singer
CODE

float vFltFacing;
float vIntModify;
float vIntPIk = (integer)(PI * 1000);

//-- in radians (preferred)
vFltFacing *= 1000;
vFltFacing += vIntPIk;
if ((vIntModify = llRound( vFltFacing ) % (vIntPIk / 2)) < (vIntPIk / 4)){
vFltFacing -= vIntModify;
}else{
vFltFacing += (vIntPIk / 4) - vIntModify;
}
vFltFacing -= vIntPIk;
vFltFacing /= 1000.0;

//-- in degrees
vFltFacing *= 1000; //-- increase range to preserve 3 decimal places
vFltFacing += 180000; //-- shift range to get a positive numbers
if ((vIntModify = llRound( vFltFacing ) % 90000) < 45000){ //-- check which is the closer movement
vFltFacing -= vIntModify; //-- subtract difference if closer to the next lowest
}else{
vFltFacing += 45000 - vIntModify; //-- add missing difference if closer to next highest
}
vFltFacing -= 180000; //-- shift range to get balanced +/-
vFltFacing /= 1000.0; //-- decrease total range back to normal preserving decimal places


ETA:
corrected for modulus clashes with floats and fractional preservation to 3 places

alternative to above
CODE

vFltModify = vFltFacing + PI;
while (vFltModify < PI_BY_TWO) vFltModify -= PI_BY_TWO; //-- modulus for floats
if (vFltModify < (PI_BY_TWO / 2.0)){
vFltModify = -vFltModify;
}else{
vFltModify = PI_BY_TWO - vFltModify
}
vFltFacing += vFltModify;


I envy you Void.. Just trying to read that makes my brain hurt, let alone try to make sense of it. Rotational maths are just not my thing.
_____________________
Proprietor of Steel Wolf Forge.
All around nice guy, if I get my sleep.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-16-2009 15:35
it only looks complex.... but it boils down to
divide by 90deg and get the remainder.
if the remainder is closer to 0 than 90 subtract the remainder, otherwise add enough to take it up to 90....

all the rest is because of how the ranges are expressed in LSL (+/- 180 vs 0-360) and conversion to radians.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -