linear path in c language

Asked

Viewed 75 times

0

Hello, I’m Lísias and I was wondering if someone could help me in this difficult task to finish my library.

I made a game engine, and in it I have already managed to implement all the basic functions for a primary use. Making simple game creation through it.

But I’m developing a role-playing game, and I’ve decided to get the characters to shoot from a long distance. However, for that to happen, I have to make the object to be thrown always run straight.

I created a function that makes all objects move on the screen, but it doesn’t work in a linear way. What can I change in this role so that it provides me with this?

PLACE_CALL STATUS PLACE_TYPE place_user_route(CHAINED * user, BP32 x, BP32 y) {
    if (user && USER_C(user->it)->object && bit_is_on(USER_C(user->it)->object->status, OBJECT_VISIBLE)) {
        OBJECT_SET * U = USER_C(user->it)->object;
        if (U->x_route == x && U->y_route == y) {
            obj_where_stop_all(U);
            return (On);
        }
        //printf("xs %f ys %f\n",object->x_speed,object->y_speed);
        if (U->x_route == x) {
            if (U->y_route > y) {
                U->y_route += -U->y_speed;
                U->where_stop_but(U, NORTH);
                if (U->y_route < y) {
                    U->y_route = y;
                    U->where_stop(U);
                    return (On);
                }
            }
            if (U->y_route < y) {
                U->y_route += U->y_speed;
                U->where_stop_but(U, SOUTH);
                if (U->y_route > y) {
                    U->where_stop(U);
                    U->y_route = y;
                    return (On);
                }
            }
            return (Off);
        }
        if (U->y_route == y) {
            if (U->x_route > x) {
                U->x_route += -U->x_speed;
                U->where_stop_but(U, WEST);
                if (U->x_route < x) {
                    U->where_stop(U);
                    U->x_route = x;
                    return (On);
                }
            }
            if (U->x_route < x) {
                U->x_route += U->x_speed;
                U->where_stop_but(U, EAST);
                if (U->x_route > x) {
                    U->where_stop(U);
                    U->x_route = x;
                    return (On);
                }
            }

            return (Off);
        }
        if (U->x_route > x) {
            U->x_route += -U->x_speed;
            if (U->x_route < x)U->x_route = x;
            if (U->y_route > y) {
                U->y_route += -U->y_speed;
                U->where_stop_but_and(U, NORTH, WEST);
                if (U->y_route < y)U->y_route = y;
            }
            if (U->y_route < y) {
                U->where_stop_but_and(U, SOUTH, WEST);
                U->y_route += U->y_speed;
            }
        }
        if (U->x_route < x) {
            U->x_route += U->x_speed;
            if (U->x_route > x)U->x_route = x;
            if (U->y_route < y) {
                U->y_route += U->y_speed;
                U->where_stop_but_and(U, SOUTH, EAST);
                if (U->y_route > y)U->y_route = y;
            }
            if (U->y_route > y) {
                U->y_route += -U->y_speed;
                U->where_stop_but_and(U, NORTH, EAST);
                if (U->y_route < y)U->y_route = y;
            }
        }
    }
    return (Off);
}

this is my attempt to change this function:

PLACE_CALL void straight_line(CHAINED * user, BP32 x, BP32 y) {
    if (user) {
        OBJECT_SET * U = USER_C(user->it)->object;
        BP32 x_distancy = (x > U->x_route) ? round(x - U->x_route) : round(U->x_route - x);
        BP32 y_distancy = (y > U->y_route) ? round(y - U->y_route) : round(U->y_route - y);
        if (x_distancy && y_distancy) {
            if (x_distancy != y_distancy) {
                //if (U->x_speed_old != U->x_speed)U->x_speed_old = U->x_speed;
                //if (U->y_speed_old != U->y_speed)U->y_speed_old = U->y_speed;
                U->x_speed = (x_distancy / y_distancy) * U->resistance;
                U->y_speed = (y_distancy / x_distancy) * U->resistance;
            } else {
                if (U->x_speed != U->y_speed) {
                    //if (U->x_speed_old != U->x_speed)U->x_speed_old = U->x_speed;
                    //if (U->y_speed_old != U->y_speed)U->y_speed_old = U->y_speed;
                    U->x_speed = (U->x_speed > U->y_speed) ? U->x_speed : U->y_speed;
                    U->y_speed = (U->y_speed > U->x_speed) ? U->y_speed : U->x_speed;
                }
            }
        } else {
            if (!x_distancy && !y_distancy) {
                //U->x_speed = U->x_speed_old;
                //U->y_speed = U->y_speed_old;
                U->x_route = x;
                U->y_route = y;
            }
            if (x_distancy && !y_distancy) {
                U->x_speed = (x_distancy / (x_distancy * U->resistance));
                //U->y_speed = U->y_speed_old;
                U->y_route = y;
            }
            if (y_distancy && !x_distancy) {
                U->y_speed = (y_distancy / (y_distancy * U->resistance));
                //U->x_speed = U->x_speed_old;
                U->x_route = x;
            }
            if (U->x_speed < 1 && U->y_speed < 1) {
                //U->x_speed = U->x_speed_old;
                //U->y_speed = U->y_speed_old;
                U->x_route = x;
                U->y_route = y;
            }
        }
    }
}

That’s what I did with the engine: https://www.youtube.com/watch?v=nuLi_lB6c4Y&feature=youtu.be

  • When you write "but it doesn’t work in a linear way" what does it mean? Geometrically the Sprite is bouncing between frames, the movement is not smooth or the trajectory is an arc?

  • When I say this, I mean that if the distance between one of the Cartesian coordinates is farther than the other, it will arrive faster at the nearest destination. For example, if the distance x is less than a y it will arrive first at point x, causing movement asymmetry. I want this movement to be symmetrical, as for example a bullet that travels in the same direction in a straight line. I’ve seen a function called bresenha, but I couldn’t fully adapt it in my project.

  • Look for Algorithm of Bresenham. Which is a technique used to draw a straight trajectory in planes whose base vectors of the ordinate or abscissa are continuously discrete.

  • It was like I said, I had already studied this algorithm, but because it contains a loop for, I can’t adapt it in the main loop of the game.

  • But in your case, game engine, instead of using a laço for you divide the algorithm into steps and use the frame rate ideal to know how many interpolar steps per second and each frame you draw you use the frame rate real to know how many steps to skip and which step to use to update the projectile position.

  • 1

    yes, that’s the idea, but to put it into practice I’m having problems

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.