Home Game Development sfml – Raycasting : computing x place of sprite on the display screen

sfml – Raycasting : computing x place of sprite on the display screen

0
sfml – Raycasting : computing x place of sprite on the display screen

[ad_1]

I’m attempting to create a raycasting engine utilizing C and CSFML, I have already got the partitions and textures rendering performed and proper now I would really like to have the ability to render sprites into the scene.
The problem I’m going through presently is discovering the x place of the sprite on the display screen.
I’ve already seemed up a variety of tutorials and it considerably works however not fully.
When I transfer the participant left and proper, the sprite place will get calculated proper, however after I look on the left and proper by altering the participant digicam angle, the sprites strikes on the left or proper. I can not determine what’s incorrect.

Here’s the code I’m utilizing to seek out the sprite place on the display screen, it’s from Liam Wynn tutorial :

void draw_entity3d(core_t *c, entity_t *entity)
{
    sfVector2f h;
    sfVector2f sprite_screen;
    sfVector2f scale;

    h.x = entity->pos.x - c->player->pos.x;
    h.y = entity->pos.y - c->player->pos.y;
    float p = rad_to_deg(atan2(-h.y, h.x));
    if (p > 360)
        p -= 360;
    if (p < 0)
        p += 360;
    float q = ((rad_to_deg(c->player->angle) + (c->render3d.fov / 2))) - p;
    scale.x = 1000 / (dist_from(entity->pos, c->player->pos));
    scale.y = 1000 / (dist_from(entity->pos, c->player->pos));
    sprite_screen.x = q * (c->render.w_size.x / c->render3d.fov);
    sprite_screen.y = (c->render.w_size.y / 2);
    sfSprite_setPosition(entity->sprite, (sfVector2f){sprite_screen.x, 
    sprite_screen.y});
    sfSprite_setOrigin(entity->sprite, get_sprite_center(entity->sprite));
    sfSprite_setScale(entity->sprite, (sfVector2f){scale.x, scale.y});
    sfRenderWindow_drawSprite(c->render.window, entity->sprite, NULL);
}

Here is what it looks in game

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here