direction inidicating sprites...

My brain has failed me…

I wish to have some sprites which move along the boundary of the player’s view screen what indicate the direction the play needs to travel to locate other players.

I currently have the following logic:


double screenX=background[zoom].getX()+background[zoom].getWidth()/2;
double screenY=background[zoom].getY()+background[zoom].getHeight()/2;
double screenHalfX=background[zoom].getWidth()/2;
double screenHalfY=background[zoom].getHeight()/2;

xdiff=currentPlayer.label.getX()-screenX;
ydiff=currentPlayer.label.getY()-screenY;

angle = Math.atan2(ydiff,xdiff)*180/PI;

if (angle>315 || angle<45)
{
    newX=background[zoom].getX()+background[zoom].getWidth();
    newY=screenY+ydiff*(Math.abs(screenHalfX/xdiff));
}
else if (angle>=45 && angle<135)
{
    newX=screenX+xdiff*(Math.abs(screenHalfY/ydiff));
    newY=background[zoom].getY()+background[zoom].getHeight();
}
else if (angle>=135 && angle<225)
{
    newX=background[zoom].getX();
    newY=screenY+ydiff*(Math.abs(screenHalfX/xdiff));                              
}
else
{
    newX=screenX+xdiff*(Math.abs(screenHalfY/ydiff));
    newY=background[zoom].getY();                              
}


where background is the a class representing the player’s current view.

so when the angle is greater than 315 or less than 45 then the x position of the sprite will be on the far right edge of the screen. The y position of the sprite is calculated using the ratio of half the screen width and the calculated difference.

Similar calculations are performed for the other quadrents.

This is not working… can anyone find a flaw in my logic?

What you mean by “This is not working” ? Is the player’s viewport a square ? If not, the angle isn’t 45.

If you have the coordinates of your player and the others, you can find the point of intersection between the line that connects both players and the borders of the screen.

You can take this point as the coordinates for your sprite.
(The dots are for formatting)
.................p2 ................./ ................/ ------------------- screen border ............../ -------------*----- ............/ .........../ ........../ .........p1
Here the (*) is the point to put your sprite, it’s not in the screen border, but a bit inside to display the sprite complete.

Hope It helps you!

Rafael.-

hmm, thats what i originally was doing but i guess my math was bad…

but maybe a combination of both? use the angle to determine which boundary line to use to intersect with the line between the player and anothe player…

Here I have the code for line clipping in a rectangle (From the late DooM4K).

In the code the rectangle goes from (-cx,0) to (cx,cy)

...................p2 .................../ +---------------+./ |...............|/ |...............* |............../| |............./.| |............/..| |.........../...| +----------*----+ ........../ .........p1

It generates the points (*) for each coordinate if needed.


      for (int i = 0; i < wallsp.length - 1; i++) {
        double px1 = wallsp[i][0];
        double py1 = wallsp[i][1];
        double px2 = wallsp[i + 1][0];
        double py2 = wallsp[i + 1][1];
        //The line equation parameters        
        double a = (py2 - py1);
        double b = (px1 - px2);
        double c = -a * px1 - b * py1;
        boolean inside = true;
        //clipping
        //Point 1
        //Coordinate y
        if (py1 < 0) {
          if (py2 < 0) {
            inside = false;
          }
          else {
            py1 = 0.1;
            px1 = (a == 0) ? px1 : (-b * py1 / a - c / a);
          }
        }
        else if (py1 > CY) {
          if (py2 > CY) {
            inside = false;
          }
          else {
            py1 = CY;
            px1 = (a == 0) ? px1 : (-b * py1 / a - c / a);
          }
        }
        //Coordinate x
        if (inside && px1 < -CX) {
          if (px2 < -CX) {
            inside = false;
          }
          else {
            px1 = -CX;
            py1 = (b == 0) ? py1 : (-a * px1 / b - c / b);
          }
        }
        else if (inside && px1 > CX) {
          if (px2 > CX) {
            inside = false;
          }
          else {
            px1 = CX;
            py1 = (b == 0) ? py1 : (-a * px1 / b - c / b);
          }
        }
        //Point 2
        //Coordinate y
        if (inside && py2 < 0) {
          if (py1 < 0) {
            inside = false;
          }
          else {
            py2 = 0.1;
            px2 = (a == 0) ? px2 : (-b * py2 / a - c / a);
          }
        }
        else if (inside && py2 > CY) {
          if (py1 > CY) {
            inside = false;
          }
          else {
            py2 = CY;
            px2 = (a == 0) ? px2 : (-b * py2 / a - c / a);
          }
        }
        //Coordinate x
        if (inside && px2 < -CX) {
          if (px1 < -CX) {
            inside = false;
          }
          else {
            px2 = -CX;
            py2 = (b == 0) ? py2 : (-a * px2 / b - c / b);
          }
        }
        else if (inside && px2 > CX) {
          if (px1 > CX) {
            inside = false;
          }
          else {
            px2 = CX;
            py2 = (b == 0) ? py2 : (-a * px2 / b - c / b);
          }
        }
        //If is inside, then put in the list of walls to be rendered.
        if (inside) {
.....
       }
     }

You should skip the code for the first coordinate (is the player, is inside).

I think the math here is correct (I had no rendering problems with the walls!)

Rafael.-