PDA

View Full Version : sin output 0 to 1, instead of -1 to 1


james THP
05-15-2003, 10:09 AM
hiya, I'm trying to get the output of a sin function in an expression to vary from 0 to 1 instead of -1 to 1... i'm using it to control a blendshape and i don't want the negative value because of the effect it has on the blendshape...

i have tried clamp and abs functions, but these result in sharp flat sections in the output sin wave...

i also (in a very round about way - not too much experience with expressions...!!) tried using a setRange node, which worked although it meant the rest position for the wave was now at a value of 0.5!!!! half way thru my blendshape... :buttrock:

hopefully i'm missing somehting really obvious, anyone have any ideas...

tanks

james

alexx
05-15-2003, 11:34 AM
try this:

$mySin = sin(time);
$mySin = ($mySin + 1) / 2.0;

that should bring your sine value to a range of 0 to 1 without having the glitch in it.

cheers

alexx

james THP
05-15-2003, 12:58 PM
:eek:

pure magic!

got it to work, all set now... so simple and yet so confusing to my mathematically challenged brain! back to grade 1 maths i go... 1+1 =...

(thanks +1) / 2.0

;)

james

dstripinis
05-16-2003, 12:44 AM
You're better off multiplying by 0.5 than dividing by 2. Same results, but computationally less expensive

mark_wilkins
05-16-2003, 05:23 AM
David, unless you're making fun of me for always harping on performance... :D

Floating point multiply and divide take roughly the same amount of time. Time each of these with a stopwatch and see for yourself:

for ($x = 0; $x < 100000000; $x++)
{
float $z = 3;
float $y = ($z * 0.5);
}
print "done";


for ($x = 0; $x < 100000000; $x++)
{
float $z = 3;
float $y = ($z / 2);
}
print "done";

-- Mark

alexx
05-16-2003, 09:43 AM
cooool :)

two mel authors start the fight ;)

mark_wilkins
05-16-2003, 10:35 AM
No way. David's as knowledgeable as they come, and there's no way I'd contradict him without double and triple-checking my facts. :D

-- Mark

dstripinis
05-16-2003, 11:18 PM
I'm just coming off the mental place of games. It has to do with the number of cycles needed to calculate a divid vs the number of cycles needed to calculate a multiply. This is a tad more signifigant in games where every cycle is precious.

It could become much more of an issue if you had say 10,000 particles all running a expression with a divide in it vs a multiply.

Maybe our workstations are just too good? :)

Anyway, I've always been taught "don't divide", so, you know, I just don't.

mark_wilkins
05-17-2003, 11:11 AM
Getting off-topic a bit: don't FMUL and FDIV take the same number of cycles on a Pentium 4? Or is exception handling the issue?

-- Mark

James Jacobs
05-17-2003, 05:10 PM
you could always use "absolute value" as well.

float $myVar = abs(sin(time));

mark_wilkins
05-17-2003, 11:07 PM
note that absolute value gives you different motion, by making the object suddenly reverse direction at the low point.

For many things, this may be just what you want, of course.

-- Mark

larryvm
05-21-2003, 12:57 PM
i think the best way is

$value=($inputcontrol%3.14159);
$value=-1.0*cos ($value);
$value=($value+1)/2.0;


this way when inputcontrol=0 the blend shape is 0
and the range value is between 0 and 1

CGTalk Moderation
01-15-2006, 02:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.