Following enemy CSFML

Started by
18 comments, last by polpel 5 years ago

Here's my code now:


timer = get_time(timer);
mouse = sfMouse_getPosition(win);
dir.x = mouse.x - enemy[0]->pos.x;
dir.y = mouse.y - enemy[0]->pos.y;
if (timer->seconds >= 0.01/* && vector_dist(mouse, enemy[0]->pos) < 200*/) {
	dir = normalize_dir(dir);
	enemy[0]->pos.x += dir.x /* vector_dist(mouse, enemy[0]->pos)*/;
	enemy[0]->pos.y += dir.y /* vector_dist(mouse, enemy[0]->pos)*/;
	sfClock_restart(timer->clock);
}
display_sprites(enemy, 1, win);

 

Advertisement

What is normalize_dir()? That doesn't look like a SFML function. Also, as a side note, does CSFML not have functions for adding subtracting vectors? It would make your code easier to read.

I'm quite sure that there aren't any.

normalize_dir() is my own implemented version to normalize my vector

37 minutes ago, polpel said:

normalize_dir() is my own implemented version to normalize my vector

There are various possible issues here, but the next step is probably to post the code for normalize_dir().

Here's the normalize dir function:


sfVector2i     normalize_dir(sfVector2i dir, int len)
{
    dir.x = dir.x / len;
    dir.y = dir.y / len;
    return (dir);
}

 

That doesn't show how the length is computed, but regardless, to use this method you'll need to use arbitrary real numbers (e.g. sfVector2f from the library you're using) rather than integers (at least as an intermediary).

Unless there's a specific reason to do otherwise, generally for something like this you'd do everything in floating point and then convert to other representations only as needed (e.g. for rendering if you need integer coordinates). Movement on a grid would be a case where you might need to use integers, but in that case you'd need a different algorithm than the one under discussion.

It's actually movement in a window with pixels

1 minute ago, polpel said:

It's actually movement in a window with pixels

That doesn't matter - the same thing still applies. (If you're not sure why/how, feel free to ask for clarification.)

Thanks man it worked!!! I just had to change the integers to floats... I feel so stupid ..

This topic is closed to new replies.

Advertisement