Finding opposite edge (wraparound) location given vector and location

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












A drawing of my current situation



I am making a game that has a wraparound effect for some objects. The objects re-spawn at the opposite 'point' of the screen that they exit. I need to find
this opposite point $(x_2, y_2)$ only given $(x_1, x_2)$, the velocity $(v_1, v_2)$ and the 'window size' $w text width , h text height $. Some other examples have been drawn in different colors.



I do have a working implementation of this, but it only works in the horizontal direction. I'd like to extend it to work at any angle. I have tried using the unit vector but I'm not sure what calculations to perform on it.










share|cite|improve this question





















  • After the object reenters at $(x_2,y_2)$, will it eventually reach $(x_1,y_1)$ again if it continues straight on its path?
    – amd
    Sep 6 at 20:51














up vote
0
down vote

favorite












A drawing of my current situation



I am making a game that has a wraparound effect for some objects. The objects re-spawn at the opposite 'point' of the screen that they exit. I need to find
this opposite point $(x_2, y_2)$ only given $(x_1, x_2)$, the velocity $(v_1, v_2)$ and the 'window size' $w text width , h text height $. Some other examples have been drawn in different colors.



I do have a working implementation of this, but it only works in the horizontal direction. I'd like to extend it to work at any angle. I have tried using the unit vector but I'm not sure what calculations to perform on it.










share|cite|improve this question





















  • After the object reenters at $(x_2,y_2)$, will it eventually reach $(x_1,y_1)$ again if it continues straight on its path?
    – amd
    Sep 6 at 20:51












up vote
0
down vote

favorite









up vote
0
down vote

favorite











A drawing of my current situation



I am making a game that has a wraparound effect for some objects. The objects re-spawn at the opposite 'point' of the screen that they exit. I need to find
this opposite point $(x_2, y_2)$ only given $(x_1, x_2)$, the velocity $(v_1, v_2)$ and the 'window size' $w text width , h text height $. Some other examples have been drawn in different colors.



I do have a working implementation of this, but it only works in the horizontal direction. I'd like to extend it to work at any angle. I have tried using the unit vector but I'm not sure what calculations to perform on it.










share|cite|improve this question













A drawing of my current situation



I am making a game that has a wraparound effect for some objects. The objects re-spawn at the opposite 'point' of the screen that they exit. I need to find
this opposite point $(x_2, y_2)$ only given $(x_1, x_2)$, the velocity $(v_1, v_2)$ and the 'window size' $w text width , h text height $. Some other examples have been drawn in different colors.



I do have a working implementation of this, but it only works in the horizontal direction. I'd like to extend it to work at any angle. I have tried using the unit vector but I'm not sure what calculations to perform on it.







trigonometry vectors






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Sep 6 at 10:16









Trontor

33




33











  • After the object reenters at $(x_2,y_2)$, will it eventually reach $(x_1,y_1)$ again if it continues straight on its path?
    – amd
    Sep 6 at 20:51
















  • After the object reenters at $(x_2,y_2)$, will it eventually reach $(x_1,y_1)$ again if it continues straight on its path?
    – amd
    Sep 6 at 20:51















After the object reenters at $(x_2,y_2)$, will it eventually reach $(x_1,y_1)$ again if it continues straight on its path?
– amd
Sep 6 at 20:51




After the object reenters at $(x_2,y_2)$, will it eventually reach $(x_1,y_1)$ again if it continues straight on its path?
– amd
Sep 6 at 20:51










2 Answers
2






active

oldest

votes

















up vote
0
down vote













This can be done via modulo arithmetic:



$$x_2 = x_1 + tv_1 pmodw$$



$$y_2 = y_1 + tv_2 pmodh$$






share|cite|improve this answer






















  • Thanks for the reply. Is $t$ = mod w and mod h respectively?
    – Trontor
    Sep 6 at 14:23










  • $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
    – Siong Thye Goh
    Sep 6 at 14:27











  • I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
    – Trontor
    Sep 6 at 14:36










  • You manage to get the program to work for horizontal direction right? how does it look like?
    – Siong Thye Goh
    Sep 6 at 14:38










  • $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
    – Trontor
    Sep 6 at 14:40


















up vote
0
down vote













It looks from your examples that you basically need to find the other point at which the line through $(x_1,y_1)$ with direction vector $(v_x,v_y)$ intersects the bounding box. Effectively, you are clipping the line against the box. You can find many algorithms for doing so, such as Cohen-Sutherland, Cyrus-Beck, &c. Here you know that one of the endpoints is already clipped to the box, so you can make some simplifications to these algorithms. For example, the Skala algorithm classifies the vertices of the bounding box against the equation $ax+by+c=0$ of the line, represented as a homogeneous vector, to determine which edge is crossed, after which computing the crossing point is straightforward. For your application, a vector that represents the line is $$mathbf l = (v_x,v_y,0)times(x_1,x_2,1) = (v_y,-v_x,v_xy_1-v_yx_1).$$ For each vertex $mathbf p = (x_p,y_p,1)$ compute the sign of $mathbf lcdotmathbf p = v_yx_p-v_xy_p+(v_xy_1-v_yx_1).$ (This is the left-hand side of the equation of the line evaluated at $mathbf p$.) If any of these are zero, then the line passes through that vertex. If the signs for adjacent vertices differ, then the line intersects the edge with those endpoints. Of course, one of these crossings will be $(x_1,y_1)$ itself. If the other crossing is not at a vertex (in which case you’re done), it will cross exactly one of the other edges. For a horizontal edge of the form $y=y_e$, the $x$-coordinate of the intersection is easily found to be $x_1+v_xover v_y(y_e-y_1)$. Similarly, for a vertical edge $x=x_e$, the intersection $y$-coordinate is $y_1+v_yover v_x(x_e-x_1)$.






share|cite|improve this answer






















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "69"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2907303%2ffinding-opposite-edge-wraparound-location-given-vector-and-location%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This can be done via modulo arithmetic:



    $$x_2 = x_1 + tv_1 pmodw$$



    $$y_2 = y_1 + tv_2 pmodh$$






    share|cite|improve this answer






















    • Thanks for the reply. Is $t$ = mod w and mod h respectively?
      – Trontor
      Sep 6 at 14:23










    • $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
      – Siong Thye Goh
      Sep 6 at 14:27











    • I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
      – Trontor
      Sep 6 at 14:36










    • You manage to get the program to work for horizontal direction right? how does it look like?
      – Siong Thye Goh
      Sep 6 at 14:38










    • $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
      – Trontor
      Sep 6 at 14:40















    up vote
    0
    down vote













    This can be done via modulo arithmetic:



    $$x_2 = x_1 + tv_1 pmodw$$



    $$y_2 = y_1 + tv_2 pmodh$$






    share|cite|improve this answer






















    • Thanks for the reply. Is $t$ = mod w and mod h respectively?
      – Trontor
      Sep 6 at 14:23










    • $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
      – Siong Thye Goh
      Sep 6 at 14:27











    • I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
      – Trontor
      Sep 6 at 14:36










    • You manage to get the program to work for horizontal direction right? how does it look like?
      – Siong Thye Goh
      Sep 6 at 14:38










    • $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
      – Trontor
      Sep 6 at 14:40













    up vote
    0
    down vote










    up vote
    0
    down vote









    This can be done via modulo arithmetic:



    $$x_2 = x_1 + tv_1 pmodw$$



    $$y_2 = y_1 + tv_2 pmodh$$






    share|cite|improve this answer














    This can be done via modulo arithmetic:



    $$x_2 = x_1 + tv_1 pmodw$$



    $$y_2 = y_1 + tv_2 pmodh$$







    share|cite|improve this answer














    share|cite|improve this answer



    share|cite|improve this answer








    edited Sep 6 at 13:43

























    answered Sep 6 at 13:36









    Siong Thye Goh

    82.6k1456104




    82.6k1456104











    • Thanks for the reply. Is $t$ = mod w and mod h respectively?
      – Trontor
      Sep 6 at 14:23










    • $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
      – Siong Thye Goh
      Sep 6 at 14:27











    • I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
      – Trontor
      Sep 6 at 14:36










    • You manage to get the program to work for horizontal direction right? how does it look like?
      – Siong Thye Goh
      Sep 6 at 14:38










    • $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
      – Trontor
      Sep 6 at 14:40

















    • Thanks for the reply. Is $t$ = mod w and mod h respectively?
      – Trontor
      Sep 6 at 14:23










    • $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
      – Siong Thye Goh
      Sep 6 at 14:27











    • I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
      – Trontor
      Sep 6 at 14:36










    • You manage to get the program to work for horizontal direction right? how does it look like?
      – Siong Thye Goh
      Sep 6 at 14:38










    • $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
      – Trontor
      Sep 6 at 14:40
















    Thanks for the reply. Is $t$ = mod w and mod h respectively?
    – Trontor
    Sep 6 at 14:23




    Thanks for the reply. Is $t$ = mod w and mod h respectively?
    – Trontor
    Sep 6 at 14:23












    $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
    – Siong Thye Goh
    Sep 6 at 14:27





    $t$ refers to the time. If $t$ is huge, you might like to take modulo first to make the number smaller.
    – Siong Thye Goh
    Sep 6 at 14:27













    I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
    – Trontor
    Sep 6 at 14:36




    I'm not sure if I follow correctly. I don't know what time we're referring to here. Can I ignore $t$ if $v$ is a unit vector?
    – Trontor
    Sep 6 at 14:36












    You manage to get the program to work for horizontal direction right? how does it look like?
    – Siong Thye Goh
    Sep 6 at 14:38




    You manage to get the program to work for horizontal direction right? how does it look like?
    – Siong Thye Goh
    Sep 6 at 14:38












    $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
    – Trontor
    Sep 6 at 14:40





    $x_2 = x_1 + sgn(z_x)times w$ where $z=textunit vector opposite to v$
    – Trontor
    Sep 6 at 14:40











    up vote
    0
    down vote













    It looks from your examples that you basically need to find the other point at which the line through $(x_1,y_1)$ with direction vector $(v_x,v_y)$ intersects the bounding box. Effectively, you are clipping the line against the box. You can find many algorithms for doing so, such as Cohen-Sutherland, Cyrus-Beck, &c. Here you know that one of the endpoints is already clipped to the box, so you can make some simplifications to these algorithms. For example, the Skala algorithm classifies the vertices of the bounding box against the equation $ax+by+c=0$ of the line, represented as a homogeneous vector, to determine which edge is crossed, after which computing the crossing point is straightforward. For your application, a vector that represents the line is $$mathbf l = (v_x,v_y,0)times(x_1,x_2,1) = (v_y,-v_x,v_xy_1-v_yx_1).$$ For each vertex $mathbf p = (x_p,y_p,1)$ compute the sign of $mathbf lcdotmathbf p = v_yx_p-v_xy_p+(v_xy_1-v_yx_1).$ (This is the left-hand side of the equation of the line evaluated at $mathbf p$.) If any of these are zero, then the line passes through that vertex. If the signs for adjacent vertices differ, then the line intersects the edge with those endpoints. Of course, one of these crossings will be $(x_1,y_1)$ itself. If the other crossing is not at a vertex (in which case you’re done), it will cross exactly one of the other edges. For a horizontal edge of the form $y=y_e$, the $x$-coordinate of the intersection is easily found to be $x_1+v_xover v_y(y_e-y_1)$. Similarly, for a vertical edge $x=x_e$, the intersection $y$-coordinate is $y_1+v_yover v_x(x_e-x_1)$.






    share|cite|improve this answer


























      up vote
      0
      down vote













      It looks from your examples that you basically need to find the other point at which the line through $(x_1,y_1)$ with direction vector $(v_x,v_y)$ intersects the bounding box. Effectively, you are clipping the line against the box. You can find many algorithms for doing so, such as Cohen-Sutherland, Cyrus-Beck, &c. Here you know that one of the endpoints is already clipped to the box, so you can make some simplifications to these algorithms. For example, the Skala algorithm classifies the vertices of the bounding box against the equation $ax+by+c=0$ of the line, represented as a homogeneous vector, to determine which edge is crossed, after which computing the crossing point is straightforward. For your application, a vector that represents the line is $$mathbf l = (v_x,v_y,0)times(x_1,x_2,1) = (v_y,-v_x,v_xy_1-v_yx_1).$$ For each vertex $mathbf p = (x_p,y_p,1)$ compute the sign of $mathbf lcdotmathbf p = v_yx_p-v_xy_p+(v_xy_1-v_yx_1).$ (This is the left-hand side of the equation of the line evaluated at $mathbf p$.) If any of these are zero, then the line passes through that vertex. If the signs for adjacent vertices differ, then the line intersects the edge with those endpoints. Of course, one of these crossings will be $(x_1,y_1)$ itself. If the other crossing is not at a vertex (in which case you’re done), it will cross exactly one of the other edges. For a horizontal edge of the form $y=y_e$, the $x$-coordinate of the intersection is easily found to be $x_1+v_xover v_y(y_e-y_1)$. Similarly, for a vertical edge $x=x_e$, the intersection $y$-coordinate is $y_1+v_yover v_x(x_e-x_1)$.






      share|cite|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        It looks from your examples that you basically need to find the other point at which the line through $(x_1,y_1)$ with direction vector $(v_x,v_y)$ intersects the bounding box. Effectively, you are clipping the line against the box. You can find many algorithms for doing so, such as Cohen-Sutherland, Cyrus-Beck, &c. Here you know that one of the endpoints is already clipped to the box, so you can make some simplifications to these algorithms. For example, the Skala algorithm classifies the vertices of the bounding box against the equation $ax+by+c=0$ of the line, represented as a homogeneous vector, to determine which edge is crossed, after which computing the crossing point is straightforward. For your application, a vector that represents the line is $$mathbf l = (v_x,v_y,0)times(x_1,x_2,1) = (v_y,-v_x,v_xy_1-v_yx_1).$$ For each vertex $mathbf p = (x_p,y_p,1)$ compute the sign of $mathbf lcdotmathbf p = v_yx_p-v_xy_p+(v_xy_1-v_yx_1).$ (This is the left-hand side of the equation of the line evaluated at $mathbf p$.) If any of these are zero, then the line passes through that vertex. If the signs for adjacent vertices differ, then the line intersects the edge with those endpoints. Of course, one of these crossings will be $(x_1,y_1)$ itself. If the other crossing is not at a vertex (in which case you’re done), it will cross exactly one of the other edges. For a horizontal edge of the form $y=y_e$, the $x$-coordinate of the intersection is easily found to be $x_1+v_xover v_y(y_e-y_1)$. Similarly, for a vertical edge $x=x_e$, the intersection $y$-coordinate is $y_1+v_yover v_x(x_e-x_1)$.






        share|cite|improve this answer














        It looks from your examples that you basically need to find the other point at which the line through $(x_1,y_1)$ with direction vector $(v_x,v_y)$ intersects the bounding box. Effectively, you are clipping the line against the box. You can find many algorithms for doing so, such as Cohen-Sutherland, Cyrus-Beck, &c. Here you know that one of the endpoints is already clipped to the box, so you can make some simplifications to these algorithms. For example, the Skala algorithm classifies the vertices of the bounding box against the equation $ax+by+c=0$ of the line, represented as a homogeneous vector, to determine which edge is crossed, after which computing the crossing point is straightforward. For your application, a vector that represents the line is $$mathbf l = (v_x,v_y,0)times(x_1,x_2,1) = (v_y,-v_x,v_xy_1-v_yx_1).$$ For each vertex $mathbf p = (x_p,y_p,1)$ compute the sign of $mathbf lcdotmathbf p = v_yx_p-v_xy_p+(v_xy_1-v_yx_1).$ (This is the left-hand side of the equation of the line evaluated at $mathbf p$.) If any of these are zero, then the line passes through that vertex. If the signs for adjacent vertices differ, then the line intersects the edge with those endpoints. Of course, one of these crossings will be $(x_1,y_1)$ itself. If the other crossing is not at a vertex (in which case you’re done), it will cross exactly one of the other edges. For a horizontal edge of the form $y=y_e$, the $x$-coordinate of the intersection is easily found to be $x_1+v_xover v_y(y_e-y_1)$. Similarly, for a vertical edge $x=x_e$, the intersection $y$-coordinate is $y_1+v_yover v_x(x_e-x_1)$.







        share|cite|improve this answer














        share|cite|improve this answer



        share|cite|improve this answer








        edited Sep 7 at 7:28

























        answered Sep 7 at 7:23









        amd

        26.8k21046




        26.8k21046



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2907303%2ffinding-opposite-edge-wraparound-location-given-vector-and-location%23new-answer', 'question_page');

            );

            Post as a guest













































































            這個網誌中的熱門文章

            Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

            Why am i infinitely getting the same tweet with the Twitter Search API?

            Carbon dioxide