I got a mathematical problem and I cant quite get the solution.

I got an raster where the cells are 20x20 pixels big. The whole raster is 300x300 px big. So my raster got 15x15 cells.

On pressing the return button, a block at x=random(0,300) y=0 gets spawned with speed = random(1,20).

This block is stored in the table blocks = {} and gets deleted when exceeding the maximum y of 300.

Draw function:

Code: Select all

```
for i,block in ipairs(blocks) do
love.graphics.rectangle("fill", math.floor(block.x/20)*20,math.floor(block.y/20)*20, 20, 20)
end
```

As the code shows, every block is drawn in on cell as long his position is in it's cells coordinates.

So if the blocks position is between 20 and 40 pixels, the block will be drawn in the 20-40 raster. If his position is 41, he will be drawn in the 40-60 cell and so on.

Now I wanted to add an alternating alpha value so that the block will get darker the more he reaches the cells boundaries. From 20 to 30 the alpha value would change from 0 to 255. When the blocks y position reaches 31 the alpha value will decrease linear to 0 so that it's 0 when the blocks y equals 40.

However I can't do the math. First I thought a modulus division would do it but it didn't work since the modulus isn't alternating.

alpha = math.abs(math.sin(block.y*9))*255

The *9 is for converting the y position to a degree value the sin can work with.

I thought that way:

If y=10*9 = 90 then sin would be 1, times 255 would be 255 which is what I want.

If y= 20*9 = 180 then sin would be 0, times 255 would be 255 which is also what I want.

I think the math is correct but I don't know why it still does not work.

This is now true alternating but it's way to fast for the blocks actual movement. It blinks like ten times before the blocks position changes to the next cell beneath.

I attached the project to the post. So if you know how to solve this issue, then please tell me.

Press enter to spawn a block