Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  direction inidicating sprites...  (Read 1269 times)
0 Members and 1 Guest are viewing this topic.
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Posted 2005-04-11 02: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

Senior Newbie




Back from beyond ... Moo!


« Reply #1 - Posted 2005-04-11 08: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

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #2 - Posted 2005-04-11 19: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #3 - Posted 2005-04-12 00: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

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #4 - Posted 2005-04-12 19: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (9 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!