Pregunta

local b = (math.sin(t * 1.3 + 0.6) + 1) / 2

I have above line of code as a part of script that moves a ball on a vertical bar, In above code, what is the use of +1 added to output of math.sin ?, However, If i remove it, the ball tends to move beyond the length of the vertical bar, how is this +1 controlling that?Please?

then this b is actually used here

bluedot:setX(90 + b * 280)

The bar image had horizontal orientation so in code, take 'y' as 'X' and 'x' as 'Y'. Now, If I change 90 to something beyond 90. the ball while moving down exceeds length of bar in downward direction and if I change 90 to something less than that, ball crosses upper end of the rod while moving.Please explain this limiting ball movement concept here.

¿Fue útil?

Solución

The result of the sine function has a range from -1 to 1.

enter image description here

By adding 1 to it, you move the result above zero.

enter image description here

However, since the range is now 0 .. 2, the formula has a division by two, to normalize the result again.

This is really basic mathematics by the way. You should brush up on that, software development is not just putting together chunks of code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top