Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  direction inidicating sprites...  (Read 1045 times)
0 Members and 1 Guest are viewing this topic.
Offline moogie

JGO Strike Force
***

Posts: 775
Medals: 5


Java games rock!


« on: 2005-04-10 20:14:33 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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?
Offline Deadcow

JGO n00b
*

Posts: 34


Back from beyond ... Moo!


« Reply #1 on: 2005-04-11 02:02:17 »

What you mean by "This is not working" ? Is the player's viewport a square ? If not, the angle isn't 45.
Offline rdcarvallo

Sr. Member
**

Posts: 300


2D Java games forever!


« Reply #2 on: 2005-04-11 13:06:35 »

 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.-

Games published by our own members! Go get 'em!
Offline moogie

JGO Strike Force
***

Posts: 775
Medals: 5


Java games rock!


« Reply #3 on: 2005-04-11 18:12:08 »

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....
Offline rdcarvallo

Sr. Member
**

Posts: 300


2D Java games forever!


« Reply #4 on: 2005-04-12 13:04:00 »

  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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
      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.-
 
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.077 seconds with 20 queries.