Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

That's some major math for sure.

By the way, this is what I came up with when I was making Abyssal Infants.

[code]

void fire_bullet_to_player(Enemy *e, s16 x, s16 y, fix16 speed) {

    Player *p = get_closest_x_player(e);

    fix16 vx = fix16Div(fix16Sub(p->obj.x, FIX16(x)), F320);

    fix16 vy = fix16Div(fix16Sub(p->obj.y, FIX16(y)), F320);

    fix16 length = fix16Add(fix16Mul(vx, vx), fix16Mul(vy,vy));

    length = fix16Sqrt(length);

    if (length != 0) {

        vx = fix16Div(vx, length);

        vy = fix16Div(vy, length);

        vx = fix16Mul(vx, speed);

        vy = fix16Mul(vy, speed);

       
        add_enemy_bullet(x, y, vx, vy);

    }

}

[/code]

F320 is just the FIX16(320) I use it so, the numbers do not pass the f16 limits, this probably can be replaced with F16 or bit shifting.

But I think what is really missing from your code is the use of fix16Sqrt that underneath is just indexing an array. Check it out.

(+1)

Thanks for sharing! I liked Abyssal Infants a lot.

You know, I tried making a 2^16 length lookup table at one point and the rom stopped compiling (presumably I ran out of space on the cart). I wonder how the sqrt table does it.

Thanks.

Probably something else happened, I think 2^16 of s16 table is (2^16) * 2 bytes.

But SGDK have us covered on this with the fix16Sqrt function.

The main headache when I tried it was that I was getting above the f16 limits with multiplication, that's why I have the division with F320. I also looked at the source of fix16Div and it looks like that div is forced in assembly. I haven't tried to remove the div with shifting, but if this is possible, vector normals can become too cheap.

(2 edits) (+1)

Coming from the future just to say you don't need fix16Add and fix16Sub anymore. The new SGDK library had then added in operators "+" and "-". Other than that, you two are great, thanks for the masterclass here, it helped me a lot. :D