Y
Y
Yoshinon Eared2016-10-08 21:18:39
Graphic arts
Yoshinon Eared, 2016-10-08 21:18:39

Get area in texture atlas solely by its id?

Good day.
There is an atlas of textures of a certain size (128x128 for example), on which sprites of the same size (32x32) are located, for a total of 16 sprites.
Is there any way to get the correct X and Y offset using only the X offset?
I tried to do like this:

if (indexTile * 32)>= y * 128)
{
    y+=1;
}
Рисовать(indexTile * 32, y * 32);

But it doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2016-10-08
@Aquinary

function getCoordByNumber(n, width, heigth, totalWidth, totalHeigth){
  if(n == 0) return {x:0,y:0};
    var nx = ~~(totalWidth/width);
    var ny = ~~(totalHeigth/heigth);
    return {
        x:(n%nx)*width,
        y:(~~(n/ny))*heigth
    };
}

console.log(getCoordByNumber(/*номер текстурки*/9, 32, 32, 128, 128));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question