1
\$\begingroup\$

i have an interesting question about geometry. I have two or more moving objects outside of my stage's viewport (blue and red circle in the image below), which i want to point at with two arrows on the border of the screen (or better, viewport bounds). The black dot never moves, because it represents viewport's center.

So, arrows must always point at the objects and must always be "locked" on the border of the screen.

enter image description here

Can someone help me with this?

UPDATE Following @Stormwind answer, i managed to get a Ray from objects to screen center. Now my problem is defining the BoundingBox. I tried declaring it like this:

Vector3 v1 = new Vector3(0, 0, 0)
Vector3 v2 = new Vector3(screen.width, screen.height, 0)
BoundingBox box = new BoundingBox(v1, v2)

and then get the intersection by using the IntersectRayBounds() method, but behaviour is funny. I think i missed out something or mistakenly declared the BoundingBox.

\$\endgroup\$
  • \$\begingroup\$ Do you have any samples of code you've tried? Is this just a question about the mathematics behind getting this to work, or is it specific to LibGDX in some way? \$\endgroup\$ – Root Vegetable Apr 12 '16 at 18:40
  • \$\begingroup\$ What you're looking for is line segment intersection. \$\endgroup\$ – Vaillancourt Apr 12 '16 at 18:48
  • \$\begingroup\$ @RootVegetable Sincererly, i didn't tried anything yet, because i don't have a clue where to start, geometrically. I know i have to start with the line from screen center to objects, but how to obtain that in LibGDX? \$\endgroup\$ – Davide Cattani Apr 12 '16 at 20:09
1
\$\begingroup\$

I solved my problem by using Intersector.intersectLines method.

Basically the first line is the segment between object and screen center. Then i use an if else to check which side of the screen rectangle is intersecting the first segment.

Too bad that Intersector class doesn't have this method!

Storwind's answer Intersector.intersectRayBounds seemed ok, but for some reasons it didn't work, maybe because it is expecially for 3d geometry.

\$\endgroup\$
0
\$\begingroup\$
Depending on what program you are using, a simple draw line command would be the simplest of ways.

To provide the user with more target info, use the following.

P1x=230;
P1y=120;
P2x=20;
P2y=40;
Bearing=atan(No/Ea)*180/Pi;
Distance=sqrt((P1x-P2x)^2+(P1y-P2y)^2);
\$\endgroup\$
  • 1
    \$\begingroup\$ This does not answer the OP's question. This will give the angle and a line segment between the player and the other object. In no way it answers the user's question of where to draw the arrow. \$\endgroup\$ – Vaillancourt Apr 12 '16 at 19:19
0
\$\begingroup\$

According to LibGDX documentation, it does this for you:

intersectRayBounds

public static boolean intersectRayBounds(Ray ray,
                                         BoundingBox box,
                                         Vector3 intersection)

Intersects a Ray and a BoundingBox, returning the intersection point in intersection. This intersection is defined as the point on

the ray closest to the origin which is within the specified bounds.

The returned intersection (if any) is guaranteed to be within the bounds of the bounding box, but it can occasionally diverge slightly

from ray, due to small floating-point errors.

If the origin of the ray is inside the box, this method returns true and the intersection point is set to the origin of the ray,

accordingly to the definition above.

Parameters:
    ray - The ray
    box - The box
    intersection - The intersection point (optional)
Returns:
    Whether an intersection is present.

Hence you should be able to define a bounding box and shoot a ray from object position towards world origin, and get the closest intersection point. This would be the arrow end position; arrow start is (0,0). If you work on 2D, the depth dimension (or one of the 3 dimensions) and it's values should be irrelevant in the whole, 3D -> 2D should remain consistent.

\$\endgroup\$
  • \$\begingroup\$ Ok i tried your method and got some doubts on BoundingBox declaration. In the wiki it says that it is defined by a minimum and a maximum Vector3. I tried setting it like this: Vector3 v1 is viewport's lower-left point (0,0,0) Vector3 v2 is viewport's higher-right point (screenwidth, screenheight,0) I declared box = (v1, v2) but the intersection point i get is wrong \$\endgroup\$ – Davide Cattani Apr 21 '16 at 5:43
  • \$\begingroup\$ Perhaps give the z-dimension a size too? Like box corners to (0,0,-1) and (swidth, sheight,1)? Then just set 0 for the object.z's and ignore answer.z? \$\endgroup\$ – Stormwind Apr 22 '16 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.