Vector value limits
|
|
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
|
05-10-2009 10:48
I'm slowly coming up against script memory limits in a project... one section that seams bloated is for clamping values in a vector, and another for wrapping around values.
Is there a more efficient way of doing these two limits/wrap arounds?
if (pose_offset.x > 5.0) pose_offset.x = 5.0; else if (pose_offset.x < -5.0) pose_offset.x = -5.0; if (pose_offset.y > 5.0) pose_offset.y = 5.0; else if (pose_offset.y < -5.0) pose_offset.y = -5.0; if (pose_offset.z > 5.0) pose_offset.z = 5.0; else if (pose_offset.z < -5.0) pose_offset.z = -5.0;
pose_rot = <llRound(pose_rot.x * 10), llRound(pose_rot.y * 10), llRound(pose_rot.z * 10)> / 10; if (pose_rot.x > 180.0) pose_rot.x -= 360.0; else if (pose_rot.x <= -180.0) pose_rot.x += 360.0; if (pose_rot.y > 180.0) pose_rot.y -= 360.0; else if (pose_rot.y <= -180.0) pose_rot.y += 360.0; if (pose_rot.z > 180.0) pose_rot.z -= 360.0; else if (pose_rot.z <= -180.0) pose_rot.z += 360.0;
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-10-2009 18:35
yes  ETA: wrapping versions should use the modulus operator when possible, which may require a shift before and after to get a lower bound of 0 while doing calculation. I'll add it to the TODO list for the above page
_____________________
| | . "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... | - 
|
|
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
|
05-10-2009 18:42
From: Void Singer yes  ETA: wrapping versions should use the modulus operator when possible, which may require a shift before and after to get a lower bound of 0 while doing calculation. I'll add it to the TODO list for the above page I'll take a look! Yeh, I had planned on using mod, but there was something it did, and I can't remember... I think it gave me 180 and -180, and just to be picky, I didn't want -180.
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-10-2009 18:49
although for reference... SL will correctly wrap out of range degrees. so 450 will still yield 90 inworld.
unfortunately you can't use modulus with floats in SL (only a few languages allow that), which is why I said IF possible... conversion to integers would probably cost you more when dealing with vectors.
_____________________
| | . "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... | - 
|
|
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
|
05-10-2009 21:12
From: Void Singer although for reference... SL will correctly wrap out of range degrees. so 450 will still yield 90 inworld.
unfortunately you can't use modulus with floats in SL (only a few languages allow that), which is why I said IF possible... conversion to integers would probably cost you more when dealing with vectors. Yeh, the -180 to 180 is more of a display/export issue than an application of the value issue. Looking at the logic of the other, I can use something similar to the clamp routine to wrap around... using the assumption that I'll never be more than 10 degrees over or under -180/180, I don't have to worry about doing a mod.
|
|
Paladin Pinion
The other one of 10
Join date: 3 Aug 2007
Posts: 191
|
05-10-2009 21:45
How come there is no min/max functions for integers and/or floats? I know about the one for list statistics but that's pretty limited. There are lots of times where min/max functions would be very helpful when not working with lists.
For example, in this case:
pose_offset.x = llMax(-5.0,llMin(pose_offset.x, 5.0))
Or did I miss it somewhere? I've needed this often.
_____________________
Mote Particle Script Generator - easier and faster than any HUD Also: Paladin's Sunbeam. Up at dawn, gone by dusk, day and night sounds, fully configurable See more at: www.paladinpinion.com
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-10-2009 22:38
nope, you didn't miss it. and actually the lists stats functions were added in the last batch of additions. there are ways to do it (if you know your likely overage, underage amounts, you can compress the range with division, and llFloor, or llCeil outside values, but you loose accuracy on floats)
_____________________
| | . "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... | - 
|
|
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
|
05-11-2009 05:58
BTW, if your range clamping doesn't have to be exactly what you're doing, consider using llVecNorm().
That is, if (5,5,5) is a relatively arbitrary limit, and the limit doesn't need to be a cube dimensionally, you can use this:
float LIMIT = 5.; if (llVecMag(myvec) > LIMIT) { myvec = LIMIT * llVecNorm(myvec); }
This isn't what you asked for, but might be a substitute.
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-11-2009 10:11
I don't think that going to work quite so well in capping/wrapping individual elements (in this case degrees)
_____________________
| | . "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... | - 
|
|
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
|
05-11-2009 15:34
Right: the OP mentions both clamping and wrapping. Using vector magnitude wouldn't do quite what's wanted for wrapping, and may or may not do what's wanted for clamping. It definitely isn't clamping individual values, as asked. But sometimes, we don't think of other possibilities and our questions are narrower than our problems.
|
|
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
|
05-12-2009 09:17
For wrapping a Euler intended for inworld display, where the value to be wrapped won't ever be more than 360 degrees past the limit, this works... vec_rot += <  vec_rot.x > 180) * -360 + (vec_rot.x <= -180) * 360, (vec_rot.y > 180) * -360 + (vec_rot.y <= -180) * 360, (vec_rot.z > 180) * -360 + (vec_rot.z <= -180) * 360>; I never thought of using the TRUE/FALSE evaluation as a multiplier in equations like these. Void, should I fear the warnings about the clamp routine not working reliably in Mono for the example you referenced?
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-12-2009 09:35
no, it's just llGetFreeMemory that is inconsistent... the clamp functions themselves run just fine... maybe I'll rewrite that note to be more clear.
_____________________
| | . "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... | - 
|
|
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
|
05-13-2009 08:10
From: Phate Shepherd For wrapping a Euler intended for inworld display, where the value to be wrapped won't ever be more than 360 degrees past the limit, this works... vec_rot += <  vec_rot.x > 180) * -360 + (vec_rot.x <= -180) * 360, (vec_rot.y > 180) * -360 + (vec_rot.y <= -180) * 360, (vec_rot.z > 180) * -360 + (vec_rot.z <= -180) * 360>; I never thought of using the TRUE/FALSE evaluation as a multiplier in equations like these. Void, should I fear the warnings about the clamp routine not working reliably in Mono for the example you referenced? How about this: myvec = llRot2Euler(llEuler2Rot(myvec * DEG_TO_RADIANS)) * RADIANS_TO_DEG; Probably slower, but might be smaller. Unfortunately, you'd also get the nonintuitive flipping that we see when rotating prims while watching the numbers in the build panel. Also, the ranges would be (-180, +180] rather than [0, 360).
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-13-2009 17:41
you can cheat the ranges by subtracting out degrees before and adding them after
_____________________
| | . "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... | - 
|
|
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
|
05-14-2009 11:26
From: Lear Cale How about this:
myvec = llRot2Euler(llEuler2Rot(myvec * DEG_TO_RADIANS)) * RADIANS_TO_DEG;
Probably slower, but might be smaller. Unfortunately, you'd also get the nonintuitive flipping that we see when rotating prims while watching the numbers in the build panel. Also, the ranges would be (-180, +180] rather than [0, 360). Lear, that is what I originally had, but I can't quite remember why I dropped that. I know there was a little error accumulated, but nothing that a simple rounding routine couldn't clear up.... I think I may go back and try it again just to see what it was that I didn't like.
|