Jump to content

Math skill for LSL


Calamari
 Share

You are about to reply to a thread that has been inactive for 3646 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Ive been having a lot of fun learning LSL over time, but now I'm thinking that improving my math skill would help out with scripting n genral.  I was wondering if any one had suggestion on what math topicks would be usefull to learn more about.

I thought this might be an interesting topic to start, if any one else has math question feel free to add them.

Link to comment
Share on other sites

Don't bother brushing up on your tensor calculus, but it's probably worthwhile to go back and review basic trigonometry and matrix operations.  Most of the difficult scripting tasks involve one or the other, and the rest of the "math" you'll need isn't much harder than analytical geometry.  In fact, I'd guess that except for the rotation errors that we all suffer through when we mess up the matrix operations, most math errors in LSL are not math problems at all.  They're simple arithmetic errors or -- more embarrassing -- counting mistakes, or they are errors in logic.

Link to comment
Share on other sites

Thanks Rolig for the reply that will give me some thing to look into.

here's some thing I started playing with based on a post you made on timer loops. I had to look up what the % operator was in LSL and started to play with all the combination of the use of Modulo operations to see how many way I could control single timer in different ways at one time.  I had know Idea what Modulo operations were until I looked it up, made me wonder what else I din't know. Here's what I've come up with and added to your original script I found here. http://community.secondlife.com/t5/LSL-Scripting/The-timer-loop-how-it-works/td-p/948641

 

//  http://community.secondlife.com/t5/LSL-Scripting/The-timer-loop-how-it-works/td-p/948641//  %  = Modulus/Cross-Product//  https://en.wikipedia.org/wiki/Modulo_operationinteger count;integer ON;default{    touch_start(integer total_number)    {        llSetTimerEvent(1.0* (ON = !ON)); //Turns timer ON and OFF    }    timer()    {        ++count;        //llOwnerSay("count = " + (string)count);        if(count%1 == 0)//if count diveted by 1 has not remainder         {            llOwnerSay("1");//every count         }        if(count%2 != 0)//if count diveted by 2 has remainder = odd         {            llOwnerSay("odd");//every other count odd        }        if (count%2 == 0)//if count diveted by 2 has not remainder = even        {            llOwnerSay("even");//every other count even        }        if(count%4 == 0)//if count diveted by 4 has not remainder        {            llOwnerSay("4");//ever 4th count        }        if(count == 6)//do some thing once        {            llOwnerSay("6");//on count of 6 only        }        if(count == 8)//end and reset        {            count = 0;            llOwnerSay("end");            llSetTimerEvent(0);        }    }}

 that what I've come up with to add to it so far.

 

Link to comment
Share on other sites

Thanks irihapeti, and I agree thats good place to start, I would like to learn more about using the bitwise operators they are still a bit of a mystery to me.  I did some searching online and found a good blog post about math for programing with lost of other links in it.

http://inventwithpython.com/blog/2012/03/18/how-much-math-do-i-need-to-know-to-program-not-that-much-actually/

Link to comment
Share on other sites

you on a good path bc you want to improve your skills/knowledge which is always a good thing (:

in the codes you posted are these:

if (count % 2 == 0)
if (count % 2 != 0)

as a exercise think about how these might be re-written

for example the NOT operator (!)

what happens when write them as:

if (count % 2)
if (!(count % 2))

and think about how/why does this work as it does

the why of things sometimes eludes us when starting out with bitwise. We just do and it works (: Dunno why sometimes. Just that it does. But after a while then it starts to make sense

 You on the right path tho I think. Can learn heaps doing what you already doing. Get a script written by expert/experienced person and play with it

  • Like 1
Link to comment
Share on other sites

Thanks irihapeti,  I went back over my timer  test script and tried your variations, and then tried similar changes to the other conditions.  Besides finding several equivalences, I all so found a new way to use the timer.

if(count % 4 )
            llOwnerSay("skip every 4th count");

 

 

Link to comment
Share on other sites

you on it (:

ok. time to move up. AND operator (&)

whats happens: if (count & 4)

+

eta:

think about what is the value of count when (count % 4) is true and when is false

then think about the value of count when (count & 4)

to help you get started. I do the table for %

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3
8 % 4 = 0

do the table for & and see what you get

 

Link to comment
Share on other sites

I kept track of if it triggered or not for 16 counts, here's what I got, 

        if(count & 0 ) //= 0000 0000 0000 0000           
        if(count & 1 ) //= 1010 1010 1010 1010
        if(count & 2 ) //= 0110 0110 0110 0110
        if(count & 3 ) //= 1110 1110 1110 1110
        if(count & 4 ) //= 0001 1110 0001 1110
        if(count & 5 ) //= 1011 1110 1011 1110
        if(count & 6 ) //= 0111 1110 0111 1110
        if(count & 7 ) //= 1111 1110 1111 1110
        if(count & 8 ) //= 0000 0001 1111 1110

could be very useful for times.  MUCH to think about here

Thanks again irihapeti !

Link to comment
Share on other sites

 (:

thinking about what the value being operated on by an operand, opens up a world of discovery and possibilities. Adventure even (:

consider something a little more simple. A 3 bit binary string. In 3 bits can store 8 values. 0 to 7. In binary:

0 = 000
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
6 = 110
7 = 111

the rightmost bit is the 1 column. Middle bit is the 2 column. Left bit the 4 column. When extend this left then the column value doubles. 4 bits 8. 5 bits 16. 6 bits 32. etc

back to the 3 bits. What happens if AND 1. AND 1 evaluates the rightmost bit. 0 & 1 = 0. 1 & 1 = 1

0 000 & 1 = 0
1 001 & 1 = 1
2 010 & 1 = 0
3 011 & 1 = 1
4 100 & 1 = 0
5 101 & 1 = 1
6 110 & 1 = 0
7 111 & 1 = 1

can see from this that all even numbers evaluate to 0. all odd numbers evaluate to 1

code example:

if (n & 1) {then n is odd} else {n is even}

+

ok. next. what can we learn about n when AND 4. 4 in binary is 100. So we now evaluating the value of n based on the 4 column bit

if (n & 4) {then n is ?} else {n is ?}

yes. n is either high or low

+

this the adventure. While AND is often used to evaluate as TRUE or FALSE. It can also be used to discover ODD or EVEN. HIGH or LOW and other things. Like what do we get if AND 2?

we get 2 numbers that are low, of which one is odd and the other even. And also get 2 high numbers. one odd. one even. We now starting to think about them as sets

[2,3,6,7] [0,1,4,5]

+

to experiment with learning about this can write a test loop. Something like:

integer top = 7; // or higher as you like
integer n;
for (n = 0; n <= top; n++)
{
   if (n & 1) llOwnerSay((string(n) + " n&1 = " + (string)(n & 1));
   if (n | 2) llOwnerSay((string(n) + "n|2 = " + (string)(n | 2));
   // ... etc
}

+

the OR (|) operator also need to be proficient with. Can think of it like bitwise addition. Refer the binary table above

n = 2 | 1. set the 2 bit and the 1 bit. n = 3

n = 4 | 2. (n=6)

where this gets really useful is with stuff like:

MOVE = 1;
FAST = 2;
ZOOM = 4;

m = MOVE;
m = m | FAST;
m = m | ZOOM;

we doing bitwise addition to go faster

if (m == (MOVE | FAST | ZOOM)) { brmmm! woohoo! }
else if (m == (MOVE | FAST)) { shout go faster! }
else if (m == MOVE) { blinking snail lol }
else if (!m) { aww man! goooooooo ok !!! }
else { o.m.g !!! you broke it. get off quick!! before somebody comes (: }

+

once you comfortable with AND OR NOT (& | !) then you be a long way down the road to where you want to be

then can tackle things like (~) (<<) (>>) etc. Once get into these then will be getting way down into bitwise math and logic programming techniques. So learn the 3 commons (& | !) bc without them then the more deeper stuff isnt going to make much sense

also bc the commons are used in heaps of LSL code examples and scripts. And also bc many LSL functions use bitsets as parameters

  • Like 2
Link to comment
Share on other sites

I started thinking about which LSL functions use bitwise OR , and first thing that comes to mind are particle mask flags, and request for permisions.  Until now I haden't thought of the | operator adding the paramiter together
 
http://wiki.secondlife.com/wiki/LlRequestPermissions

integer perms = PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION;llRequestPermissions(AvatarID, perms); 


under particles there was a good example that puts a lot of this together, much to think about here.

http://wiki.secondlife.com/wiki/LlParticleSystem

integer ColorAlphatoRGBA(vector color, float alpha) {    return (((integer)(alpha   * 255.0) & 0xFF) << 24) |           (((integer)(color.x * 255.0) & 0xFF) << 16) |           (((integer)(color.y * 255.0) & 0xFF) <<  8) |            ((integer)(color.z * 255.0) & 0xFF);} vector RGBAtoColor(integer rgba) {    return < ((rgba >> 16) & 0xFF) / 255.0, ((rgba >> 8) & 0xFF) / 255.0, (rgba & 0xFF) / 255.0 >;} float RGBAtoAlpha(integer rgba) {    return ((rgba >> 24) & 0xFF) / 255.0;}


I had to look up what 0xFF was so now reading about hex and LSL

Link to comment
Share on other sites

I had a li'l time on my hands, and whipped together a little demonstration of some "math" that is not really computery (no bitwise stuff), but is magical. It's a simple bit of arithmetic that demonstrates the inextricable relationship between trigonometry (sinusoids, as in water waves and pendulums) and logarithms/exponentials (as with populations of bacteria and bank account balances).

I don't offer this as a suggestion for math skill pursuit, but I hope any of you who try this feel at least some small sense of wonder that such a simple equation as...

targetPosition = currentPosition * 1.99 - pastPosition;

...yields a sine wave oscillation, so long as you keep track of where you are (currentPosition) and where you were (pastPosition), and that simply changing from 1.99 to 2.01 switches from an oscillation to exponential growth.

My Dad taught me this when I was perhaps 12, and I made shapes on the screen of his Apple II using this very equation. It's also easy to do in Excel, where you can chart the values. Unfortunately, SL is too slow to really do a good demonstration of this. Run on the bare metal of a modern PC, this equation computes so fast that you can generate sine waves at radio frequencies. Magical math like this is the foundation of "digital signal processing", where simple math yields astonishing variety and complexity.

Nature is far, far ahead of us in her use of magical math. What fun it is trying to catch up.

Here's the script...

float pastPosition = 0;float currentPosition = 1;//float magic =  2.00;  // linear growth//float magic =  2.01;  // exponential growth//float magic =  1.99;  // sinusoidal oscillationfloat magic = -1.99;  // binary oscillation with sinusoidal envelope//float magic = -2.01;  // binary oscillation with exponential growth//float magic = -2; // binary oscillation with linear growthfloat time = 0.3;default{    state_entry()    {        llSetTimerEvent(time);    }    touch_start(integer num_detected){        pastPosition=0;        currentPosition=1;    }        timer(){        float targetPosition = currentPosition * magic - pastPosition;        llSetLinkPrimitiveParamsFast(2, [PRIM_POS_LOCAL,<0,0,targetPosition/10>] );        pastPosition = currentPosition;        currentPosition = targetPosition;     }}

Rez two prims (I used a 0.5,0.5.0.01 green cylinder for the root and a <0.1,0.1,0.1> red sphere for the child) and link them together, then drop this script into the root prim. There are six magic constants at the top of the script. Uncomment one of them at a time to see what happens when you change them. Touch the root prom to re-zero the simulation.

 

  • Like 2
Link to comment
Share on other sites

Thanks for sharing this Madelaine, I'm very interested in any type of math that is useful for programing not just the bitwise math.  until I started playing around with LSL I've never had a use for math, I wish I had found an interest in it when I was younger, but having fun catching up :)   I love how with mixing programing and math for learning you get results that you can see makes experimenting and learning much more fun.


I just happen to have a big interest in ocean waves,  tidal cycles, and Theremin music (not at the same time lol) Can't wait to test your example!

Link to comment
Share on other sites

what you will notice in the posted code is how the author is using hex notation for bitwise (& |) operands and using decimal notation for operands that use math operators (* /)

can know from this that this code writer is a professional

is sometimes tempting when first get into hex notation to go all hex. Can get really murky tho when start writing code like:

0x10 / 0xFF

when 16 / 255 is a whole lot clearer. So follow the practice that this code writer has shown and you will be all good. Will save yourself a bunch of headaches as well (:

 

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3646 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...