Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Game Development / Newbie & Debugging Questions / Re: Angle of slope for mouse movement. on: 2012-05-07 17:08:03
What theagentd said is correct, but ultimately you don't really want that angle since it will have to be transformed back into Cartesian coordinates to move your player.  Here is how you would achieve what you want.  Have three variables, dx, dy, and speed. The variables dx and dy represent the change in the players X and Y coordinate per game loop.  In case you are curious, they are named this way with the d because of calculus.  Not really important actually, you could name them "changeInX, changeInY" if you prefer.  Each game loop, you calculate what dx and dy are:
1  
2  
dx = mouseX - playerX
dy = mouseY - playerY

So now you have the proper direction, but not the proper speed.  If you add dx and dy to your players location now they will instantly teleport to the mouse location, which isn't what you want.  To fix this, we scale the values of dx and dy.  Think of dx and dy as the sides of a right triangle, and the hypotenuse is the direction you are going to travel.  We want this hypotenuse to be the same length no matter which direction the player is heading.   We can use the Pythagorean Theorem to figure out that what we want is
1  
dx^2 + dy^2 = speed^2

So what we will do is normalize the d variables: scale the triangle they currently make to have hypotenuse of length one.  We do this by calculating the distance using the Pythagorean theorem and then dividing both variables by that distance.  Finally, we multiply dx and dy by speed, which will give us the result we want!  Here it all is:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
double hypotenuse = Math.sqrt(dx*dx + dy*dy);
dx /= hypotenuse;
dy /= hypotenuse;
dx *= speed;
dy *= speed;

// now we have the correct dx and dy, so add those to the players position!

playerX += dx;
playerY += dy;


You can of course do a slight optimization here by dividing speed by hypotenuse and multiplying dx and dy by that.  If you are heart set on using trigonometry, then the code would look like this:
1  
2  
3  
4  
5  
6  
double theta = Math.atan2(mouseY - playerY, mouseX - playerX);
dx = speed*Math.cos(theta);
dy = speed*Math.sin(theta);

playerX += dx;
playerY += dy;


This will work just fine as well and basically does the same thing, however, it involves these trig functions which are slow.  In a small game it probably won't matter though.  If I were you, I would avoid using math that you haven't learned about yet since you will not be able to fix any bugs that show up.  Hope this helps!  Grin
2  Game Development / Game Play & Game Design / Re: [Help] Block Class Design on: 2012-04-28 17:41:55
Have a 3D point which stores the block's location and translate to that location before you begin drawing.  Next, bind your texture and draw the six faces of the cube as quads (or two triangles if you prefer).  How you actually go about doing this really depends on how you implement drawing, but the basic idea is the same.  A cube has eight verticies:
1  
2  
3  
4  
5  
6  
7  
8  
(0.5, 0.5, 0.5)
(0.5, -0.5, 0.5)
(-0.5, -0.5, 0.5)
(-0.5, 0.5, 0.5)
(0.5, 0.5, -0.5)
(0.5, -0.5, -0.5)
(-0.5, -0.5, -0.5)
(-0.5, 0.5, -0.5)

You use these verticies to draw the quads.  Each face of the cube has one coordinate that is constant, so for instance, the four points
1  
2  
3  
4  
(0.5, 0.5, 0.5)
(0.5, -0.5, 0.5)
(-0.5, -0.5, 0.5)
(-0.5, 0.5, 0.5)

make up the front of the cube because they all have the same z coordinate.  You get the other five faces the same way.  If you don't want to use +/-0.5 for all the coordinates you don't have to, you could use 1 and 0, 1 and -1, or whatever you want.  I chose 0.5 because this will give you a cube of unit side length (each edge is one unit long) and centered at the origin (the point (0,0,0)).  Because you translated before drawing, the cube on the screen will be centered at (x, y, z) which is exactly what you want Smiley Just remember to translate back to the origin before drawing the next cube.

Hope this helps.
3  Java Game APIs & Engines / OpenGL Development / Re: Object Oriented OpenGL on: 2012-04-28 04:53:11
libgdx has classes that you can use instead of direct OpenGL. Mesh, Camera, etc.
I'll look into learning about that.  Is libgdx just a Java library?  Actually, as I'm typing this I realized I can look this up myself Smiley
4  Java Game APIs & Engines / OpenGL Development / Re: Is this the correct way to do textures? on: 2012-04-27 03:07:18
Thank you for the tip!  I have added the following into my 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  
      int vShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vShader, easyTexVert);
      glCompileShader(vShader);
     
      int success = glGetShader(vShader, GL_COMPILE_STATUS);      
      if(success != 1) {
         String s = glGetShaderInfoLog(vShader, 1024);
         System.out.println("Error Compiling Shader: "+s);
         System.exit(1);
      }
     
      int fShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fShader, easyTexFrag);
      glCompileShader(fShader);
     
      success = glGetShader(fShader, GL_COMPILE_STATUS);      
      if(success != 1) {
         String s = glGetShaderInfoLog(fShader, 1024);
         System.out.println("Error Compiling Shader: "+s);
         System.exit(1);
      }
     
      int program = glCreateProgram();
      glAttachShader(program, vShader);
      glAttachShader(program, fShader);
      glLinkProgram(program);
      glValidateProgram(program);
     
      success = glGetProgram(program, GL_LINK_STATUS);      
      if(success != 1) {
         String s = glGetProgramInfoLog(program, 1024);
         System.out.println("Error Linking Program: "+s);
         System.exit(1);
      }


Not sure what else to do if there is an error, aside from just exit the program.
5  Java Game APIs & Engines / OpenGL Development / Is this the correct way to do textures? on: 2012-04-27 01:42:18
Okay so let me start off by saying that this code does work.  It does exactly what I expected it to do, no problem (whew!) My question is, is this the proper way?  I know I shouldn't be using a static context here, but this is my first foray into textures so I wanted the entire program to be in one simple class.  I am more interested in things like, am I putting "glUseProgram()" in the proper place.  I know, I know, if it works who cares, but I will eventually want to divide a lot of this into objects and I don't want to put code places where it shouldn't go.

Here is 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  
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL33.*;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class TextureTestWindow {
   
   private static final float[] verts = {
      0.8f, 0.8f, 0f, 1f,
      -0.8f, 0.8f, 0f, 1f,
      -0.8f, -0.8f, 0f, 1f,  
      0.8f, -0.8f, 0f, 1f,
      };
   
   private static final float[] texCords = {
      1f, 0f,
      0f, 0f,
      0f, 1f,
      1f, 1f};
   
   private static final byte[] texData = {
      (byte) 255, (byte) 0, (byte) 0, (byte) 255,
      (byte) 0, (byte) 255, (byte) 0, (byte) 255,
      (byte) 255, (byte) 255, (byte) 0, (byte) 255,
      (byte) 0, (byte) 0, (byte) 255, (byte) 255};
   
   private static final String easyTexVert =
         "#version 330\n" +
         "layout (location = 0) in vec4 position;\n" +
         "layout (location = 1) in vec2 texCord;\n" +                        
         "varying vec2 outTexCord;\n" +                            
         "void main() {\n" +
         "   gl_Position = position;\n" +
         "   outTexCord = texCord;\n" +
         "}";
   
   private static final String easyTexFrag =
         "#version 330\n" +
         "varying vec2 outTexCord;\n" +
         "out vec4 outputColor;\n" +                    
         "uniform sampler2D tex;\n" +                
         "void main() {\n" +
         "    outputColor = texture(tex, outTexCord);\n" +
         "}";

   public static void main(String[] args) {
      try {
         Display.setDisplayMode(new DisplayMode(500, 500));
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(1);
      }
     
      // Create the program      

      int vShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vShader, easyTexVert);
      glCompileShader(vShader);
      int fShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fShader, easyTexFrag);
      glCompileShader(fShader);
     
      int program = glCreateProgram();
      glAttachShader(program, vShader);
      glAttachShader(program, fShader);
      glLinkProgram(program);
      glValidateProgram(program);

      // Set up the buffers
     
      FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(verts.length+texCords.length);
      vertBuffer.put(verts);
      vertBuffer.put(texCords);
      vertBuffer.flip();
     
      int vertBufferObj = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vertBufferObj);
       glBufferData(GL_ARRAY_BUFFER, vertBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      ByteBuffer texBuffer = BufferUtils.createByteBuffer(texData.length);
      texBuffer.put(texData);
      texBuffer.flip();
     
      // Create the texture      

      int texture = glGenTextures();
      glBindTexture(GL_TEXTURE_2D, texture);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, texBuffer);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
       glBindTexture(GL_TEXTURE_2D, 0);
       
      // get the Uniform location

       int textureUnif = glGetUniformLocation(program, "tex");
       
      // set up the sampler

      int sampler = glGenSamplers();
      glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
      int i = 0;   // using the first texture
     
      // Put the texture into the uniform

      glUseProgram(program);
      glUniform1i(textureUnif, i);
      glUseProgram(0);
     
      // make the VAO

      int vao = glGenVertexArrays();
      glBindVertexArray(vao);
      glBindBuffer(GL_ARRAY_BUFFER, vertBufferObj);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
      glEnableVertexAttribArray(1);
      glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, Float.SIZE/8 * verts.length);
      glBindVertexArray(0);

      // Finally reader to render!! :)
     
      while(!Display.isCloseRequested()) {
         
         glClear(GL_COLOR_BUFFER_BIT);
         
         // Set up the context
        glUseProgram(program);
         glActiveTexture(GL_TEXTURE0 + i);
         glBindTexture(GL_TEXTURE_2D, texture);
         glBindSampler(i, sampler);
         glBindVertexArray(vao);
         glDrawArrays(GL_QUADS, 0, 4);
         // Clean up
        glBindSampler(i, 0);
         glBindTexture(GL_TEXTURE_2D, 0);
         glBindVertexArray(0);
         glUseProgram(0);
         
         Display.update();
         Display.sync(30);
      }
     
   }

}


Any help is appreciated!  Grin (EDIT: I added some comments to the code)
6  Game Development / Shared Code / Re: [Help] Unlimited terrain on: 2012-04-26 17:44:33
I am also wondering about this.

Here is my guess about what you need to do.  I'm sure someone else who knows more about this will be able to tell you more useful information.  I assume that you have to do some sort of memory management by removing things from memory when they are far away.  I believe Minecraft does this by storing "chunks" of some size and only bothering to draw the chunks that are close enough to the player.  Everything else gets deleted from the GPU's memory, so you have to constantly be loading/deleting chunks as the player moves around.  Again, this is only my guess about this, maybe the GPU is smarter than I think it is?
7  Discussions / Miscellaneous Topics / Re: TheAgentD´s sob thread on: 2012-04-26 03:06:32
I really like your signature.  I have written a lot of music in the shower.  Smiley Also mowing the lawn, although that one won't make you smell better.
8  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-25 16:32:49
You said you are using lwjgl's matrix class?  You can extract those numbers like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
Matrix3f threeFromFour(Matrix4f in) {  // You should use a better name than this
 Matrix3f out = new Matrix3f();

  /* You can access the individual elements of a Matrix object directly
     by matrix.mAB where A is the column and B is the row.
     (I might have that backwards... ? This will still work though)
     Remember, indexing starts at zero!                                    */

  out.m00 = in.m00;
  out.m01 = in.m01;
  out.m02 = in.m02;
  out.m10 = in.m10;
  out.m11 = in.m11;
  out.m12 = in.m12;
  out.m20 = in.m20;
  out.m21 = in.m21;
  out.m22 = in.m22;
 
  return out;
}


Hope this helps.  Smiley
9  Java Game APIs & Engines / OpenGL Development / Re: Object Oriented OpenGL on: 2012-04-25 07:24:44
Thank you very much for the info.  I'll look up about scene graphs and try to make some sense out of this.   Smiley
10  Java Game APIs & Engines / OpenGL Development / Object Oriented OpenGL on: 2012-04-25 04:24:54
Let me start off by saying that I am not overly concerned with optimization right now (maybe I should be?), it's more important that I get something that just works.  I have spent a lot of time with 2D programming and am trying to make the leap into 3D.  I am concentrating on OpenGL 3.  While I've enjoyed making floating triangles and cubes, I think it's time for something a little more substantial.  I am right now trying to develop a 3D entity class and I am not totally sure what goes where.  In a 2D setting, I am very comfortable with something like the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Entity2D {
     private float x, y;
     Sprite sprite;

     ...                    // other instance variables and methods

     public void drawMe(Graphics g) {
          sprite.draw(g, x, y);                // maybe you also translate by some world coordinates, whatever!
         ...                                        // draw other things that have to do with this entity, HP bars, armor, etc.
    }

     ...                    // more stuff , update(float delta), etc...
}

and this kind of structure makes sense to me.  But now in OpenGL there is a lot more information, and I am having a difficult time deciding who (which object) owns that data.  Is a Mesh object just a collection of 3d points, a VAO, a VAO and a texture, a VAO a texture and what shader program to use, etc... It doesn't seem unreasonable to me that different entities would use the same vertex data, but a different texture for instance.  Or that sometimes you would want to render a mesh with one shader program, and other times another....

So my question is, is there some sort of convention for this type of thing?  My fear is that I will get the reply "it depends" and this is my biggest fear.  So I'll preemptively reply to this answer with "depends on what?"  Undecided

As always, I appreciate all replies. Sorry if this post sounded a little exasperated, I promise I am usually a jolly fellow.  Grin
11  Java Game APIs & Engines / OpenGL Development / Re: glGenVertexArrays(); on: 2012-04-23 00:42:53
Thank you very much.  I'm sure I will have more questions at some point.  Emo  Tongue
12  Java Game APIs & Engines / OpenGL Development / Re: glGenVertexArrays(); on: 2012-04-22 10:04:54
Hooray!  It works like a charm.  Grin  Thanks again for your help.
13  Java Game APIs & Engines / OpenGL Development / Re: glGenVertexArrays(); on: 2012-04-21 19:35:56
Thank you for your reply.  I think I understand now what I am supposed to do.  When I change the code to look like this:

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  
@Override
   protected void init() {
      initializeProgram();
      initializeVertexBuffer();

      vao = glGenVertexArrays();
      glBindVertexArray(vao);

      glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
      glDisableVertexAttribArray(0);
      glBindVertexArray(0);

      glClearColor(0f, 0f, 0f, 1.0f);
   }
   
     
   @Override
   protected void display() {  
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(theProgram);

      glBindVertexArray(vao);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      glBindVertexArray(0);

      glUseProgram(0);
   }


Now I get nothing but a black screen.  Is this the right idea or am I still off?  Thanks again!
14  Java Game APIs & Engines / OpenGL Development / glGenVertexArrays(); on: 2012-04-21 00:50:03
So I am working through the tutorials here: http://arcsynthesis.org/gltut/Basics/Tut01%20Making%20Shaders.html, and I don't fully understand something.  Here is the lwjgl port which someone posted in another thread someplace.

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  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
package rosick.mckesson.I.tut01;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL32.*;

import java.nio.FloatBuffer;
import java.util.ArrayList;

import org.lwjgl.BufferUtils;

import rosick.LWJGLWindow;


/**
 * Visit https://github.com/rosickteam/OpenGL for project info, updates and license terms.
 *
 * I. The Basics
 * Chapter 1. Hello, Triangle!
 * http://www.arcsynthesis.org/gltut/Basics/Tutorial%2001.html
 * @author integeruser
 */

public class HelloTriangle01 extends LWJGLWindow {
   
   public static void main(String[] args) {
      new HelloTriangle01().start();
   }

   
   
   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   
   private final float vertexPositions[] = {
         0.75f,  0.75f, 0.0f, 1.0f,
         0.75f, -0.75f, 0.0f, 1.0f,
         -0.75f, -0.75f, 0.0f, 1.0f
   };

   private final String strVertexShader =
         "#version 330 \n" +
               "\n" +
               "layout(location = 0) in vec4 position;\n" +
               "void main()\n" +
               "{\n" +
               "    gl_Position = position;\n" +
               "}";

   private final String strFragmentShader =
         "#version 330\n" +
               "\n" +
               "out vec4 outputCol;\n" +
               "void main()\n" +
               "{\n" +
               "   outputCol = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n" +
               "}";
     
   private int theProgram;
   private int positionBufferObject;
   private int vao;

   

   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   
   private void initializeProgram() {  
      ArrayList<Integer> shaderList = new ArrayList<>();
      shaderList.add(createShader(GL_VERTEX_SHADER,   strVertexShader));
      shaderList.add(createShader(GL_FRAGMENT_SHADER,   strFragmentShader));

      theProgram = createProgram(shaderList);
     
       for (Integer shader : shaderList) {
          glDeleteShader(shader);
      }
   }
   
   private void initializeVertexBuffer() {
      FloatBuffer vertexPositionsBuffer = BufferUtils.createFloatBuffer(vertexPositions.length);
      vertexPositionsBuffer.put(vertexPositions);
      vertexPositionsBuffer.flip();
       
      positionBufferObject = glGenBuffers();      
      glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
       glBufferData(GL_ARRAY_BUFFER, vertexPositionsBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }

   
   @Override
   protected void init() {
      initializeProgram();
      initializeVertexBuffer();

      vao = glGenVertexArrays();
      glBindVertexArray(vao);
   }
   
     
   @Override
   protected void display() {  
      glClearColor(0f, 0f, 0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(theProgram);

      glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      glDisableVertexAttribArray(0);
      glUseProgram(0);
   }
   
   
   
   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   
   private int createShader(int eShaderType, String strShaderFile) {
        int shader = glCreateShader(eShaderType);
        glShaderSource(shader, strShaderFile);

        glCompileShader(shader);

        int status = glGetShader(shader, GL_COMPILE_STATUS);
        if (status == 0) {
          int infoLogLength = glGetShader(shader, GL_INFO_LOG_LENGTH);

          String strInfoLog = glGetShaderInfoLog(shader, infoLogLength);

          String strShaderType = null;
         switch (eShaderType) {
         case GL_VERTEX_SHADER:
            strShaderType = "vertex";
            break;
         case GL_GEOMETRY_SHADER:
            strShaderType = "geometry";
            break;
         case GL_FRAGMENT_SHADER:
            strShaderType = "fragment";
            break;
         }

          System.err.printf("Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
        }
       
      return shader;
   }
   
   
   private int createProgram(ArrayList<Integer> shaderList) {      
      int program = glCreateProgram();

      for (Integer shader : shaderList) {
         glAttachShader(program, shader);
      }

      glLinkProgram(program);
     
      int status = glGetProgram(program, GL_LINK_STATUS);
      if (status == 0) {
         int infoLogLength = glGetProgram(program, GL_INFO_LOG_LENGTH);

         String strInfoLog = null;
         strInfoLog = glGetProgramInfoLog(program, infoLogLength);
         
         System.err.printf("Linker failure: %s\n", strInfoLog);
      }
     
      for (Integer shader : shaderList) {
         glDetachShader(program, shader);
      }

      return program;
   }
}


I understand what everything does except for this one bit of code:

1  
2  
      vao = glGenVertexArrays();
      glBindVertexArray(vao);

I don't see "vao" referenced anywhere else in the program and when I comment these lines out the program still runs perfectly fine.  Can anyone either help me understand this or point me in the right direction.  I have been looking on google and whatnot for quite some time and still don't really see what's going on.

Thanks!
15  Game Development / Game Mechanics / Re: Auto turrets on: 2012-04-20 03:51:49
How I would do this is have a boolean lookingForTarget which is set to false when the turret is shooting at some thing already.  So something like:

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  
public class Turret {

  private float radius = 15;   // Set this to whatever your radius is

  private boolean lookingForTarget = true;
  private Guy target = null;

  ... // Other vars

  public update(ArrayList<Guy> guys) {

    if(lookingForTarget) {
      for(Guy g: guys) {
        if(distanceFromMeToAGuy(g) < raduis) {
          target = g;
        lookingForTarget = false;
          break;          // In case more than one guy entered the raduis this update
                           // just select the first one, you could also
                           // prioritize by HP remaining, or whatever you like
      }
      }

    if(!lookingForTarget) {
      attack(target);
      if(target.isDead()) {
        target = null;
        lookingForTarget = true;
      }
    }
  }

... // more methods here

}

}


Okay so I see that while I was typing all this out others have replied, but I figured I'd post it anyway because whatever!  Tongue
16  Games Center / Contests / Re: RPG Making Competition at 64digits.com on: 2012-04-14 00:38:17
This looks awesome.  I am going to sign up!
17  Game Development / Newbie & Debugging Questions / Re: Game Logic where? on: 2012-04-01 03:50:00
That's basically what I have been trying to do, but I haven't really had time to work on the project for a while.  If I have an afternoon and get something working I'll post what I ended up doing in this thread.  My main issue was that I wasn't sure if I should pull information from the player, or have the player push information toward the game.  Not sure if that makes sense at all....
18  Game Development / Newbie & Debugging Questions / Re: Game Logic where? on: 2012-04-01 03:01:18
Why does it have to keep asking for move?  Why can't you just have it update once the move is received?
I update the screen every frame (to do animation) so it seemed easier to poll for input every frame as well.
19  Game Development / Newbie & Debugging Questions / Re: Game Logic where? on: 2012-03-22 03:33:25
If it's turn based, how could there be undecided players ?

The game updates 30 times a second.  An undecided player is just someone who is still thinking of a move.  While waiting, the screen is animating (like a bounding arrow over the current player's picture).  Once the player decides, then the game does the actual move.

Thanks for the suggestions.  I was trying to implement the state machine already, but it was a little confusing.  I guess I need to just try harder!
20  Game Development / Newbie & Debugging Questions / Game Logic where? on: 2012-03-21 06:58:45
As I said in my last thread about sorting, I am working on a card game.  I guess I need a little advice about how to organize the game logic.  In the game, players take turns making a move.  Only certain moves are available to you on your turn depending on the current state of the game.  Occasionally, things happen automatically.

I am not sure where to put all this game logic from an OO point of view.  My original plan was to have each player object have a askForMove() method, which will return a Move enum.  Depending on what the player decided to do, different things happen.  If the player hasn't decided on  a move yet, it just returns a "NONE" type, and the loop goes around again.  This requires a lot of error checking because the player may request a move which isn't actually available.  So I have a MoveEvaluater class which decides if a requested move is legal.  If it is legal then the MoveEvaluater gives the thumbs up to the Table class to do whatever move was requested.

My question is, is this actually the best way to organize all this?  Anyone else have any experience with turned based card games?

I'd like to eventually write an AI which will play the game.  So I am worried about implementing this later on.
21  Game Development / Newbie & Debugging Questions / Re: Stupid Sorting Question on: 2012-03-14 20:09:27
I would:
1. identify valid card combinations for each hand, define classes for two-of-a-kind, three-of-a-kind, full-house, etc.
2. put a list together of those for each hand
3. sort the combination lists of each player by implementing proper Comparators

-> easy to modify or extend rules
-> operates on a more abstract level instead of sorting cards directly


That's not a bad idea except that hands can be an arbitrary number of cards.  In Poker, where you only have 5 cards hands there are a small number of possible combinations, but they grow like e^(sqrt(n))/n which grows pretty fast (see http://en.wikipedia.org/wiki/Partition_(number_theory) for math about this  Grin) While there is an upper limit to the size of a hand, the number of possible hands is too large for me to individually make a class for each one.
22  Game Development / Newbie & Debugging Questions / Re: Ideas for Online High Score System on: 2012-03-14 10:32:36
Personally, for security I like the first option best.  I've used php and Java, but never together, so I don't personally know off the top of my head how to implement this, but I am sure you can find something easily enough.

However, if it were me I would just use the .txt option and not bother about security at all.  Then if later it turns out to actually be a problem and people actually are hacking into the high scores list, then I'd change my implementation.  This is mostly because I am way lazy though. It really depends on how popular you think your game is going to be.

You should probably not take my advice here.  Tongue
23  Game Development / Newbie & Debugging Questions / Re: Stupid Sorting Question on: 2012-03-14 09:38:11
Found the problem!!!

This line
1  
2  
         //if ArrayLists 'b' and 'a' are the same lengths, test for lower value
        else if(repeats.get(b).size() == repeats.get(a).size() && repeats.get(b).get(0).compareTo(repeats.get(a).get(0)) < 0)


should be this (change 'a' to 'min'):

1  
 else if(repeats.get(b).size() == repeats.get(min).size() && repeats.get(b).get(0).compareTo(repeats.get(min).get(0)) < 0)


In all honesty though your code is pretty slick and I hope you don't feel bad at all for this very small error Smiley
24  Game Development / Newbie & Debugging Questions / Re: Stupid Sorting Question on: 2012-03-14 09:18:55
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
ra4King,

After trying out your code, I think there is a problem.

[code]ArrayList<Integer> list = new ArrayList<Integer>(10);
     
list.add(1);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
list.add(0);
list.add(1);
list.add(4);
list.add(3);
list.add(5);
     
weirdSort(list);
     
for(int i : list)
    System.out.print(i + " ");

produces this output:
1  
1 1 1 2 2 4 4 5 3 0 

which is not what I want.  Clueless I am looking over the code now to see if I can find a problem, but thought I'd let you know that there is an issue.  If I find a solution I will post it below.

Quote
The real question is, what things do you really want to do?  Do you really want to sort something and that's it or what?

This is for a card game I am inventing.  The purpose of this sorting thing is to find out who has the best hand, and to also print the cards out in a logical looking order.  So the sort is happening on these cards.  Now each card has a rank and a picture, where we have that

1  
2  
3  
public int compareTo(Card c) {
  return this.rank - c.getRank();
}


so the ordering is not consistent with Object.equals().  I know it says here: (http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) not to do this, but whatever!  Basically, for this sorting I don't care if the cards have different pictures or not.  But because of this I can't just store ordered pairs like (3, 1) for "three ones" because the ones may have different pictures and I don't want to lose that information.[/code]
25  Game Development / Newbie & Debugging Questions / Re: Stupid Sorting Question on: 2012-03-12 09:31:12
Thank you ra4king  Grin  That's great.
26  Game Development / Newbie & Debugging Questions / Re: Stupid Sorting Question on: 2012-03-11 18:50:03
They are not integers, they are just objects which implement the Comparable interface.  However, each object does have a "rank" which I am using to do the comparison and that ranges from 1 to 14.
27  Game Development / Newbie & Debugging Questions / Stupid Sorting Question on: 2012-03-10 19:49:58
Hi everyone!  I have been trying to decide how to do this for quite some time and I finally decided I just need some assistance.  I have an array of Comparable objects and I want to sort them, but I want to do it in a weird way.  I'd like to sort from highest to lowest, but also place repeated entries at the front of the array.  The more times the appear the more priority they get so something like

1  
unsorted = {7, 2, 3, 2, 1, 5}

will get sorted as
1  
sorted = {2, 2, 7, 5, 3, 1}

Where the 2s get put at the front because they appear twice.  Here is another example:
1  
unsorted = {1, 2, 3, 2, 4, 2, 3, 5, 3, 5}

will look like
1  
sorted = {3, 3, 3, 2, 2, 2, 5, 5, 4, 1}

where you can see that there are three 3s and three 2s, so the 3s appear first in the sorted array. 5 appears after this because there are only two of them.  Lastly we get 4 and 1.

Does this make sense?
28  Game Development / Game Mechanics / Re: Idea for collision detection between arbitrary shaped objects in 2D on: 2011-10-21 20:29:52
So After thinking about it for 2 seconds I realized it's better to just test if their intersection is empty.  DOH!  Tongue
29  Game Development / Game Mechanics / Idea for collision detection between arbitrary shaped objects in 2D on: 2011-10-21 20:14:32
How viable is this idea in a game?  You give each sprite (bouncing ball, bullet, monster, etc...) a java.awt.geom.Area object which represents it's bounds.  You could even have a list of them if you want to for animations which change shape.  Then to test for a collision you can just see if the areas union equals their xor.  If it does then no collision happened.

1  
2  
3  
4  
5  
6  
7  
8  
public boolean collision(Area a, Area b) {
 Area union = new Area(a); // you make copies of a since Area's add() and exclusiveOr() methods will edit the object
Area xor = new Area(a);
 union = aa.add(b);
 xor.exclusiveOr(b);

 return !union.equals(xor); // equals is built into Area
}


I don't know how fast or slow these Area methods are (no doubt they are slower than Rectangle.contains()), but I don't see why this wouldn't work, especially for a small number of sprites on the screen.  You could even have the Area object created automatically from your sprites Image.  Anyone have any thoughts on this method? I came up with this while working on my Tower Defense game and trying to figure out a way to set certain parts of the map "unbuildable" (like where the enemies walk, and where you have already built something).
30  Game Development / Game Play & Game Design / Re: Drawing sprites and animation on: 2011-10-18 17:38:45
Sure, I would appreciate it if you have the time.  Grin
Pages: [1] 2
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (26 views)
2013-05-17 21:29:12

alaslipknot (36 views)
2013-05-16 21:24:48

gouessej (66 views)
2013-05-16 00:53:38

gouessej (65 views)
2013-05-16 00:17:58

theagentd (75 views)
2013-05-15 15:01:13

theagentd (69 views)
2013-05-15 15:00:54

StreetDoggy (110 views)
2013-05-14 15:56:26

kutucuk (133 views)
2013-05-12 17:10:36

kutucuk (133 views)
2013-05-12 15:36:09

UnluckyDevil (142 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.692 seconds with 20 queries.