Finding opposite edge (wraparound) location given vector and location
Clash 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.
trigonometry vectors
add a comment |Â
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.
trigonometry vectors
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
add a comment |Â
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.
trigonometry vectors
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
trigonometry vectors
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
add a comment |Â
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
add a comment |Â
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$$
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
 |Â
show 3 more comments
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)$.
add a comment |Â
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$$
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
 |Â
show 3 more comments
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$$
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
 |Â
show 3 more comments
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$$
This can be done via modulo arithmetic:
$$x_2 = x_1 + tv_1 pmodw$$
$$y_2 = y_1 + tv_2 pmodh$$
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
 |Â
show 3 more comments
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
 |Â
show 3 more comments
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)$.
add a comment |Â
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)$.
add a comment |Â
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)$.
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)$.
edited Sep 7 at 7:28
answered Sep 7 at 7:23
amd
26.8k21046
26.8k21046
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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