Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Line direction  (Read 281 times)
0 Members and 1 Guest are viewing this topic.
Online bilznatch

Senior Member


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Posted 2013-05-04 10:36:11 »

It's 1:32 in the morning and I'm not amazing at programming. Does anyone have any idea what I should do to make
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
            if (Mouse.isButtonDown(0)) {
               int mx = Mouse.getX();
               int my = Display.getHeight() - Mouse.getY();
               if (Math.sqrt(Math.pow((mx - cx), 2) + Math.pow((my - cy), 2)) <= 150){
                  GL11.glColor3f(0.0f, 1.0f, 0.0f);
                  GL11.glLineWidth(2.5f);
                  GL11.glBegin(GL11.GL_LINE_STRIP);
                  GL11.glVertex2f(mx, my);
                  GL11.glVertex2f(cx + 8, cy + 8);
                  GL11.glEnd();
               }

work so that if you leave the 150 distance boundary you'd still draw the line, in that direction, but stopping after that distance?
I thought about using slope but slope doesn't really work in a non 0,0 centered plane. Basically I'm a tired newb, and I'd appreciate some help on how I need to set this up.

Oh, and most of this code is from guides, yes I understand it, I am trying to learn as well, so it might not be optimal, and I didn't write all of it, and I don't understand all aspects of LWJGL, that is of course why I'm asking for help here. Or maybe that's just my lack of algebra skills.

Extra info: The area I have rendered is 512 x 512.
The rest of the code:
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  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;

public class Tile {
   static int cx =  248;
   static int cy = 248;
   static int mDelta = 1;
     
      /**
       * Start the example
       */

      public void start() {
         initGL(512,512);
         
         while (true) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            render();
            pollInput();
            update();
           
            Display.update();
            Display.sync(60);

            if (Display.isCloseRequested()) {
               Display.destroy();
               System.exit(0);
            }
         }
      }
     
      /**
       * Initialize the GL display
       *
       * @param width The width of the display
       * @param height The height of the display
       */

      private void initGL(int width, int height) {
         try {
            Display.setDisplayMode(new DisplayMode(width,height));
            Display.create();
            Display.setVSyncEnabled(true);
         } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
         }

         GL11.glEnable(GL11.GL_TEXTURE_2D);              
           
         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);          
           
              // enable alpha blending
             GL11.glEnable(GL11.GL_BLEND);
              GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
           
              GL11.glViewport(0,0,width,height);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);

         GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
         GL11.glOrtho(0, width, height, 0, 1, -1);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
      }
      /**
       * draw a room with openings
       */

      public void render() {
         for(int y = 0; y < 32; y += 1){
            for(int x = 0; x < 32; x += 1) {
               if(((x >= 14 && x <= 17) && (y == 0 || y == 31))||((x == 0 || x == 31) && (y >= 14 && y <= 17))){
                  Color.black.bind();
               }else if(((x < 14 || x > 17) && (y == 0 || y == 31))||((x == 0 || x == 31) && (y < 14 || y > 17))){
                  Color.darkGray.bind();
               }else{
               Color.white.bind();
               }
               GL11.glBegin(GL11.GL_QUADS);
                  GL11.glVertex2f((x * 16), (y * 16));
                  GL11.glVertex2f((x * 16), 16 + (y * 16));
                  GL11.glVertex2f(16 + (x * 16), 16 + (y * 16));
                  GL11.glVertex2f(16 + (x * 16), (y * 16));
               GL11.glEnd();
            }
         }
      }
      public static void pollInput(){
         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
            if (cy <= 20 && cx >= 216 && cx  <= 280){
            cy = 248;
            cx = 248;
            }else if(cy >= 20){
            cy -= 4 * mDelta;
            }else{
            cy += 0;
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            if (cx <= 20 && cy >= 216 && cy  <= 280){
            cy = 248;
            cx = 248;
            }else if (cx >= 20){
            cx -= 4 * mDelta;
            }else{
            cx += 0;
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
            if (cy >= 476 && cx >= 216 && cx  <= 280){
            cy = 248;
            cx = 248;
            }else if (cy <= 476){
            cy += 4 * mDelta;
            }else{
            cy += 0;
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
            if (cx >= 476 && cy >= 216 && cy  <= 280){
            cy = 248;
            cx = 248;
            }else if (cx <= 476){
            cx += 4 * mDelta;
            }else{
            cx += 0;
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
            mDelta = 2;
         }else if (!Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
         mDelta = 1;  
         }
            if (Mouse.isButtonDown(0)) {
               int mx = Mouse.getX();
               int my = Display.getHeight() - Mouse.getY();
               if (Math.sqrt(Math.pow((mx - cx), 2) + Math.pow((my - cy), 2)) <= 150){
                  GL11.glColor3f(0.0f, 1.0f, 0.0f);
                  GL11.glLineWidth(2.5f);
                  GL11.glBegin(GL11.GL_LINE_STRIP);
                  GL11.glVertex2f(mx, my);
                  GL11.glVertex2f(cx + 8, cy + 8);
                  GL11.glEnd();
               }
         }
      }
      public static void update(){
         Color.magenta.bind();
         GL11.glBegin(GL11.GL_QUADS);
               GL11.glVertex2f(cx,cy);
               GL11.glVertex2f(cx,cy + 16);
               GL11.glVertex2f(cx + 16,cy + 16);
               GL11.glVertex2f(cx + 16,cy);
            GL11.glEnd();
      }

     
      /**
       * Main Class
       */

      public static void main(String[] argv) {
         Tile tile = new Tile();
         tile.start();
      }
   }

Offline quew8

JGO Coder


Medals: 29



« Reply #1 - Posted 2013-05-04 14:44:28 »

Not sure why you would want to do this. If it is about not drawing beyond the viewing volume, OpenGL will do that far more quickly (and automatically) than you ever could.
If you still want to, you will need to produce a vector representing the line (as if it started from the origin) and another vector representing a line perpendicular to the screen (parallel to the viewing direction) of magnitude 150. Obtain the dot product of these two, then if it is lequal than 1, draw the line normally, if it is greater than 1, scale the vector by (1 / dotProduct) and draw that instead.
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.

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

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

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

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

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
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!