首页 > 解决方案 > c中的线轨迹

问题描述

我正在创建一个引擎。几乎一切工作正常。我什至可以用它做游戏。

我正在做一个 rpg 游戏,并决定让这些点互相射击。我已经有了移动点的功能。但是这个函数不是线性的。

所以,那是我不能去的地方。我需要更改该功能以使这些悲剧线性化。所以我可以在点中发射子弹或箭头,然后直接射向他们。

这是我现在拥有的功能:

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);
}

我可以更改哪些代码以使其行为正常?

这是游戏工作的一部分:

https://www.youtube.com/watch?v=nuLi_lB6c4Y&feature=youtu.be

这是我制作的一个函数,但它并不完全是直的;

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;
            }
        }
    }
}

标签: c

解决方案


推荐阅读