简体   繁体   中英

Lua development math.sin function and ball moving across the rod

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.

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

在此处输入图片说明

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

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM