Show Posts
|
|
Pages: [1]
|
|
2
|
Games Center / WIP games, tools & toy projects / Re: State of Landscape
|
on: 2013-02-17 23:18:40
|
When i'm trying to run applet i'm getting this exception 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| java.io.EOFException: Unexpected end of ZLIB input stream at java.util.zip.InflaterInputStream.fill(Unknown Source) at java.util.zip.InflaterInputStream.read(Unknown Source) at java.util.zip.ZipInputStream.read(Unknown Source) at com.sun.deploy.net.HttpDownloadHelper.decompressWrite(Unknown Source) at com.sun.deploy.net.HttpDownloadHelper.download(Unknown Source) at com.sun.deploy.cache.Cache$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.deploy.cache.Cache.downloadResourceToCache(Unknown Source) at com.sun.deploy.cache.DeployFileOutputStream.close(Unknown Source) at java.io.FilterOutputStream.close(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.close(Unknown Source) at org.lwjgl.util.applet.AppletLoader.downloadJars(AppletLoader.java:1520) at org.lwjgl.util.applet.AppletLoader.run(AppletLoader.java:871) at java.lang.Thread.run(Unknown Source) This occurred while 'Downloading packages' failed to download landscapeTest.jar after 3 attempts java.lang.Exception: failed to download landscapeTest.jar after 3 attempts at org.lwjgl.util.applet.AppletLoader.downloadJars(AppletLoader.java:1555) at org.lwjgl.util.applet.AppletLoader.run(AppletLoader.java:871) at java.lang.Thread.run(Unknown Source) |
|
|
|
|
|
6
|
Games Center / Showcase / Re: BlockWars - Multiplayer FPS for PC
|
on: 2012-05-20 11:38:34
|
The game looks quite promising. I realy like this Team Fortress style and too bad I can't test it. And I'm really pissed off from people thinking that every sandbox game is a copy. Think of minecraft as a Infiniminer clone...
I don't want to offend anybody Minecraft it's a great game (i'm playing to) but for me Minecraft it will always be a clone of "Love" game made by Quel Solaar.
|
|
|
|
|
7
|
Game Development / Newbie & Debugging Questions / Re: Need help with quanterion (cube rotate)
|
on: 2012-03-05 18:37:41
|
Oh gosh!!! Stranger and Roquen your code works the same I mean PROPERLY  . The mistake was in this part of code 1 2 3
| array[i] = change.x; array[i+1] = change.y; array[i+2] = change.z; |
during rotation around axis Z I shouldn't do this around axis Y I shouldn't do this and during rotation around axis X. I really appreciate your help. Thanks all of you!
|
|
|
|
|
8
|
Game Development / Newbie & Debugging Questions / Re: Need help with quanterion (cube rotate)
|
on: 2012-03-05 08:30:24
|
Humm...okay, something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static Quaternion setAxisAngle(Quaternion q, float x, float y, float z, float a) { a *= 0.5f;
float sinA = (float)Math.sin(a); float cosA = (float)Math.cos(a);
x *= sinA; y *= sinA; z *= sinA;
q.set(x,y,z,cosA);
return q; } |
This method doing the same what roll.setFromAxisAngle(new Vector4f(0, 0, 1, rollAngle * DEGTORAD)); and result is the same. Try this. 1 2 3 4 5
| change = new Quaternion(array[i], array[i+1], array[i+2], 1.0f); Quaternion.mul(roll, change, change); array[i] = change.x / change.w; array[i+1] = change.y / change.w; array[i+2] = change.z / change.w; |
( http://en.wikipedia.org/wiki/Homogeneous_coordinates) and this in general does terrible things which I will not describe 
|
|
|
|
|
11
|
Game Development / Newbie & Debugging Questions / Need help with quanterion (cube rotate)
|
on: 2012-03-04 20:20:31
|
Hello. I need help with quanterion. I have spent on this all saturday and couple hour today and still I don't know how to do it properly (I feel so dumb). I want to rotate cube (around on own x,y or z axis) in fact on the beginning only two wall front and right. //front wall -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, //right wall 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, -0.5f, -1.0f, 0.5f, 0.5f, -1.0f So if I want to make a rotation on Z axis (roll) I must to do something with 8 vectors and I did something 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 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
| private final static float DEGTORAD = (float)(Math.PI/180); private static float rollAngle = 4; private static Quaternion roll = new Quaternion(); private Quaternion change; private float[] array = new float[]{ -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, -0.5f, -1.0f, 0.5f, 0.5f, -1.0f}; private FloatBuffer vertexData;
private void update(){ roll(); for(int i = 0;i<array.length;i+=3) { change = new Quaternion(array[i], array[i+1], array[i+2], 0.0f); Quaternion.mul(roll, change, change); array[i] = change.x; array[i+1] = change.y; array[i+2] = change.z; } vertexData.put(array); vertexData.flip(); }
public static void roll() { roll.setFromAxisAngle(new Vector4f(0, 0, 1, rollAngle * DEGTORAD)); roll.normalise(); }
public void gameLoop(){ ... while(running) { update(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); GL11.glTranslatef(0.0f, 0.0f, -6.0f); GL11.glRotatef(80, 0.5f, 0.5f, -0.5f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(vertexSize, 0, vertexData); glColorPointer(colorSize, 0, colorData); glDrawArrays(GL_QUADS, 0, amountOfVertices); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); Display.sync(60); Display.update(); } glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); Display.destroy(); } |
and I must admit that the front wall rotate properly also right but unfrtunateli something bad happening with right wall namely is getting smaller and smaller. The same happens with front wall if I change rotation e.g. roll.setFromAxisAngle(new Vector4f(1, 0, 0, rollAngle * DEGTORAD));  What's wrong how to do this properly? I think i should create this topic in 'LWJGL Development' it's more appropriate place. Can someone move it.
|
|
|
|
|
12
|
Java Game APIs & Engines / OpenGL Development / Re: VertexArray and 10k objects
|
on: 2012-02-12 12:54:50
|
I didn't expect so many advice, THANKS A LOT all of you! One is to simply batch your cube parts together. In your case you would create a big enough ByteBuffer / FloatBuffer to hold all the data of all your cubes, in your case two buffers since you have 2 cube parts. For each cube you would "draw" its vertex data to the two buffers and then at the end just draw them all with a single call to glDrawArrays(...) for each buffer. That way you'll have a constant number of draw calls regardless of the number of cubes. This will reduce the CPU load a lot, but you'll still probably be CPU bottlenecked before you're vertex bottlenecked (assuming you don't get fragment limited).
Woow! I'm shock 10k is like nothing 50k also. With 90k I'm getting 24FPS (cube with one texture). For 60K cubs with two textures I'm getting 24 FPS all this on my laptop Core2Duo T9300 2.50GHz, 3GHz RAM, WinXP Like Lhkbob said the ultimate way of doing this is with instancing. Instancing allows you to make a single draw call and have the same geometry replicated a number of times at an extremely low CPU cost. You can keep the cube vertex data in a VBO and then draw each cube instance at different locations using a per-instance attribute. Like I said this is ridiculously fast since the GPU is pretty much guaranteed to be the bottleneck, but it has two drawbacks. The first is that this requires OpenGL 3 support, meaning that it won't work on really old computers, computers with Intel graphics cards or OSes that don't support it. Secondly you need a very simple shader to move each cube instance to its own location or they'll all end up in the same place.
That sounds very interesting. Already start searching and reading about Geometry Instancing this is totally new for me. I started from this http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter03.htmlShader is also somthing new for my I run ones simple example wrom LWJGL Wiki. lhkbob - dynamic level of detail - single quad with a texture holding a rendering of their current animation. Ehhh, I saw this so many times in games so it must be good solution. Still so many to learn. I have hope you understand cubs are only for testing I'm not trying to creating new Minecraft. When I'm toking about cube I'm thinking about trees, bushes, buildings and so on which are more complicated than one cube.
|
|
|
|
|
13
|
Java Game APIs & Engines / OpenGL Development / Re: VertexArray and 10k objects
|
on: 2012-02-10 18:33:34
|
10k it is a moment when I start having seriously problem. I joust wont to know how to handle with so many object 10k or even more OK, here it is (I hope it's enough) 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
| public class MainCanvas extends Canvas { private static MainCanvas canvas; private Thread gameThread; boolean running = false; private GameState gameState; long lastFrame; int fps; long lastFPS; public static MainCanvas get(){ if(canvas == null) { canvas = new MainCanvas(); } return canvas; } public final void addNotify(){ super.addNotify(); startLWJGL(); } public final void removeNotify(){ stopLWJGL(); super.removeNotify(); } public void startLWJGL(){ gameThread = new Thread (){ public void run(){ running = true; try { Display.setParent(canvas); Display.create(); Display.setVSyncEnabled(true); initGL(); gameState = GameState.prepare(); getDelta(); lastFPS = getTime(); } catch (LWJGLException e) { e.printStackTrace(); } gameLoop(); } }; gameThread.start(); } public void stopLWJGL(){ running = false; try { gameThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } }
private void initGL() { glViewport(0, 0, Conf.W,Conf.H); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); GL11.glEnable(GL11.GL_TEXTURE_2D); glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); } public void gameLoop(){ while(running) { Display.sync(60); int delta = getDelta(); gameState.update(delta); gameState.render(); Display.update(); updateFPS(); } Display.destroy(); }
public int getDelta() { long time = getTime(); int delta = (int) (time - lastFrame); lastFrame = time;
return delta; } public long getTime() { return (Sys.getTime() * 1000) / Sys.getTimerResolution(); } public void updateFPS() { if (getTime() - lastFPS > 1000) { System.out.println("--"+fps); fps = 0; lastFPS += 1000; } fps++; } } |
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
| public class GameState { private static GameState gameState; private List<Entity> entities = new ArrayList<Entity>(); public static GameState prepare(){ if(gameState == null) { gameState = new GameState(); } return gameState; } public GameState(){ float x = 0f; float y =0f; for (int i = 0;i<100;i++){ for(int j = 0;j<100;j++){ entities.add(EntityCreator.get().getEntity(EntityType.BOX, x, y, 0.0f)); y+=2.0f; } x+=2.0f; y=0; } } public void render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); for(Entity e : entities) { GL11.glPushMatrix(); e.render(); GL11.glPopMatrix(); } GL11.glLoadIdentity(); } public void update(float delta){ PlayerKeyboad.checkInput(delta); GL11.glRotatef(PlayerKeyboad.lookupdown, 1.0f, 0, 0); GL11.glRotatef(360.0f - PlayerKeyboad.yrot, 0, 1.0f, 0); GL11.glTranslatef(-PlayerKeyboad.xpos - PlayerKeyboad.leftRight, -PlayerKeyboad.walkbias - 0.25f - PlayerKeyboad.upDown-3, -PlayerKeyboad.zpos); } public void createBox(float x, float y, float z){ entities.add(EntityCreator.get().getEntity(EntityType.BOX, x, y, z)); } } |
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
| public class Box extends AbstractEntity {
private FloatBuffer textureData; private FloatBuffer vertexData; private FloatBuffer vertexData2; private final int amountOfVertices = 24; private final int vertexSize = 3; private final int textureVertexSize = 2; private int[] textures; public Box(int[] textures, float x, float y, float z, FloatBuffer vertexData, FloatBuffer vertexData2, FloatBuffer textureData) { this.textures = textures; this.positionX = x; this.positionY = y; this.positionZ = z; this.vertexData = vertexData; this.vertexData2 = vertexData2; this.textureData = textureData; }
@Override public void render() { GL11.glTranslatef(positionX, positionY, positionZ); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[0]); glVertexPointer(vertexSize, 0, vertexData); glTexCoordPointer(textureVertexSize, 0, textureData); glDrawArrays(GL_QUADS, 0, amountOfVertices); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[1]); glVertexPointer(vertexSize, 0, vertexData2); glTexCoordPointer(textureVertexSize, 0, textureData); glDrawArrays(GL_QUADS, 0, amountOfVertices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } } |
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
| public class EntityCreator {
private static EntityCreator creator; private FloatBuffer textureData; private FloatBuffer vertData; private FloatBuffer vertData2; private final int textureVertexSize = 2; private final int amountOfVertices = 24; private final int vertexSize = 3; private int[] textures = new int[2]; public static EntityCreator get(){ if(creator == null) { creator = new EntityCreator(); } return creator; } private EntityCreator(){ float size = 1f; vertData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize); vertData.put(CubeUtil.margeWalls(CubeUtil.getFrontWall(size, size, size))); vertData.flip(); vertData2 = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize); vertData2.put(CubeUtil.margeWalls(CubeUtil.getRightWall(size, size, size), CubeUtil.getBottomWall(size, size, size), CubeUtil.getBackWall(size, size, size), CubeUtil.getLeftWall(size, size, size), CubeUtil.getTopWall(size, size, size))); vertData2.flip(); textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureVertexSize); textureData.put(CubeUtil.getCubeTexture(1.0f,1.0f)); textureData.flip(); textures[0] = TextureUtil.texture("png", "res/Glass.png",TextureUtil.FILTER_MIPMAP); textures[1] = TextureUtil.texture("png", "res/Crate.png",TextureUtil.FILTER_MIPMAP); } public Entity getEntity(EntityType type, float x, float y, float z) { Entity entity = null; switch (type) { case BOX: entity = new Box(textures, x, y, z, vertData, vertData2, textureData); break; default: break; } return entity; } } |
1 2 3 4 5 6
| public interface Entity { public void render(); public float getPositionX(); public float getPositionY(); public float getPositionZ(); } |
and this is how I'm getting texture 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
| public class TextureUtil {
public static int FILTER_NONE = 0; public static int FILTER_LINEAR = 1; public static int FILTER_MIPMAP = 2; public static int texture(String extension, String fileName, int filter){ Texture texture = null; try { PNGDecoder decoder = new PNGDecoder(ResourceLoader.getResourceAsStream(fileName)); ByteBuffer data = ByteBuffer.allocateDirect(4 * decoder.getWidth()* decoder.getHeight()); decoder.decode(data, decoder.getWidth() * 4,Format.RGBA); data.rewind(); texture = TextureLoader.getTexture(extension, ResourceLoader.getResourceAsStream(fileName), false, filter); if (filter == FILTER_NONE) { GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data); } else if (filter == FILTER_LINEAR) { GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data); } else if (filter == FILTER_MIPMAP) { GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); } } catch (IOException e) { e.printStackTrace(); } return texture.getTextureID(); } } |
|
|
|
|
|
14
|
Java Game APIs & Engines / OpenGL Development / VertexArray and 10k objects
|
on: 2012-02-10 14:40:24
|
Hello. I have in my plans to create world for my game so I start testing how vertexarray deal with meany objects and I must say no weary well but only because I'm still learning LWJGL. Now with 10k objects (cube with 2 texture) I have poor 10fps. Of course I know about VBO and List but I'm sure the result should be better. Right now my app doing something like this (it's only part of 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 35 36 37 38 39 40 41 42 43 44 45 46 47
| private List<Entity> entities = new ArrayList<Entity>();
private void initGL() { glViewport(0, 0, Conf.W,Conf.H); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); GL11.glEnable(GL11.GL_TEXTURE_2D); glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); }
public void generateBox(){ float x = 0f; float y =0f; for (int i = 0;i<100;i++){ for(int j = 0;j<100;j++){ entities.add(EntityCreator.get().getEntity(EntityType.BOX, x, y, 0.0f)); y+=2.0f; } x+=2.0f; y=0; } player = EntityCreator.get().getEntity(EntityType.PLAYER, 0.0f, 0.0f, 3.0f); entities.add(player); }
public void gameLoop(){ generateBox(); ... while(running) { ... entities.render(); Display.update(); ... } } Display.destroy(); } |
I know the biggest problem is loop with 'entities.render();' and 10k objects. So how to optimize this code? I'm guessing that solution is renderer only this what I'm seeing not all (behind and under me). So can you give me some advice?
|
|
|
|
|
17
|
Java Game APIs & Engines / OpenGL Development / Vertex Array - beginner problem
|
on: 2012-02-01 09:19:23
|
Hello. I wont to create cube using Vertex Array but unfortunately I have problem. The problem is that the only one wall appear with second (left wall) something bad happening. I also want to add that I haven't any problem with creating cube using immediate mode. OK, this is 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 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
| package com.applet;
import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY; import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; import static org.lwjgl.opengl.GL11.GL_LEQUAL; import static org.lwjgl.opengl.GL11.GL_MODELVIEW; import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT; import static org.lwjgl.opengl.GL11.GL_PROJECTION; import static org.lwjgl.opengl.GL11.GL_QUADS; import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY; import static org.lwjgl.opengl.GL11.glClear; import static org.lwjgl.opengl.GL11.glClearDepth; import static org.lwjgl.opengl.GL11.glColorPointer; import static org.lwjgl.opengl.GL11.glDepthFunc; import static org.lwjgl.opengl.GL11.glDisableClientState; import static org.lwjgl.opengl.GL11.glDrawArrays; import static org.lwjgl.opengl.GL11.glEnable; import static org.lwjgl.opengl.GL11.glEnableClientState; import static org.lwjgl.opengl.GL11.glLoadIdentity; import static org.lwjgl.opengl.GL11.glMatrixMode; import static org.lwjgl.opengl.GL11.glShadeModel; import static org.lwjgl.opengl.GL11.glVertexPointer; import static org.lwjgl.opengl.GL11.glViewport; import static org.lwjgl.util.glu.GLU.gluPerspective;
import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.nio.FloatBuffer;
import javax.swing.JApplet;
import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11;
import com.Conf;
public class CubeVA extends JApplet { private static final long serialVersionUID = -730031275017948496L; private Canvas canvas; private Thread gameThread; boolean running = false; public void startLWJGL(){ gameThread = new Thread (){ public void run(){ running = true; try { Display.setParent(canvas); Display.create(); Display.setVSyncEnabled(true); initGL(); } catch (LWJGLException e) { e.printStackTrace(); } gameLoop(); }
}; gameThread.start(); } public void stopLWJGL(){ running = false; try { gameThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } }
public void destroy(){ remove(canvas); super.destroy(); } public void init(){ setLayout(new BorderLayout()); canvas = new Canvas(){ private static final long serialVersionUID = 5972822387678455353L;
public final void addNotify(){ super.addNotify(); startLWJGL(); } public final void removeNotify(){ stopLWJGL(); super.removeNotify(); } }; canvas.setSize(Conf.W, Conf.H); setSize(Conf.W, Conf.H); canvas.setBackground(Color.BLACK); canvas.setFocusable(true); canvas.requestFocus(); canvas.setIgnoreRepaint(false); add(canvas); } private void initGL() { glViewport(0, 0, Conf.W,Conf.H); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); } float rquad; public void gameLoop(){ final int amountOfVertices = 8; final int vertexSize = 3; final int colorSize = 3; FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize); vertexData.put(new float[]{-0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, -0.5f, -1.0f, 0.5f, 0.5f, -1.0f}); vertexData.flip();
FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize); colorData.put(new float[]{1, 0.2f, 0.2f, 0.2f, 1, 0.2f, 0.2f, 0.2f, 1}); colorData.flip(); while(running) { update(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); GL11.glTranslatef(0.0f, 0.0f, -6.0f); GL11.glRotatef(rquad, 0.0f, 1.0f, 0.5f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(vertexSize, 0, vertexData); glColorPointer(colorSize, 0, colorData); glDrawArrays(GL_QUADS, 0, amountOfVertices); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); Display.sync(60); Display.update(); } Display.destroy(); } private void update(){ rquad -= 1.55f; }
} |
|
|
|
|
|
20
|
Game Development / Newbie & Debugging Questions / Re: Problem with animation speed
|
on: 2012-01-22 21:49:40
|
Do you see the constant "1" in getxPos() + "1". If you make that "1" into an int variable, you can set the speed of it via pixels. That is a lot more accurate. If you make "1" into a double variable, you can make the bullet go faster and slower. But all your functions and storage will have to be doubles. Threads are really not very good at keeping different speeds for objects, but numbers are. Keep the Thread constant at 1 or 50 or whatever, and change the movement coordinates to control the speed.
Good Luck and hope this helps.
Ahhh. I totally forgot to mention about this, yes I know I can do this but I was wondering if it's a good practice why, because bullet can miss target only because skip some position (target position). Thanks ctomni231
|
|
|
|
|
21
|
Game Development / Newbie & Debugging Questions / Re: Problem with animation speed
|
on: 2012-01-22 20:10:20
|
I'm very sorry for doubel post. I know main problem of this topic has been resolved but I have next question which is a little different but stil related with this one. My question is how can i obtain bigger speed? I know the problem was in BulletCreator in this part of code 1 2 3 4 5
| if (canvas.isFire()) { Bullet player1Bullet = new Bullet(10,45, canvas.getImage(".\\images\\bullet.gif")); Thread.sleep(500); canvas.getBullets().add(player1Bullet); } |
should be 1 2 3 4 5
| if (canvas.isFire()) { Bullet player1Bullet = new Bullet(10,45, canvas.getImage(".\\images\\bullet.gif")); canvas.getBullets().add(player1Bullet); } Thread.sleep(1); |
but Thread.sleep(1) is max speed which I can obtein. Of course bullet moving fast but I want to somthing a little faster if I set 0 bullet moving so fast that make her almost invisible. So how can I control this speed?
|
|
|
|
|
22
|
Game Development / Networking & Multiplayer / Re: Incorrect data from server
|
on: 2012-01-16 11:02:35
|
Well then you have your answer. Make sure to properly synchronized access to the Output streams  You right ra4king. After few hours I back to this problem and now I see, Output streams wasn’t properly synchronized (he wasn't synhtonized at all). Thanks for help
|
|
|
|
|
24
|
Game Development / Networking & Multiplayer / Incorrect data from server
|
on: 2012-01-16 10:19:26
|
Hello. I'm working on simple real time 2D multiplayer shooter and now I started working on server. To connect with server I decide to use DataInputStream and DataOutputStream. For now I have simple test: - if space key is pressed data are sending to server - server recognizing data type and sending this data to others player (in theory) - client/player receiving data from server Connecting is working sending data to server and receiving also. Unfortunately weary often I'm getting wrong data and I don’t know why. Some code I'm sending this data when space is pressed (this work fine) 1 2 3 4
| Connector.getInstance().getOutToServer().writeInt(667); Connector.getInstance().getOutToServer().writeInt(13); Connector.getInstance().getOutToServer().flush(); Connector.getInstance().getOutToServer().close(); |
Server thread (also working fine) 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
| public void run() { int gameId = 0; int playerData = 0; try { iss = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); oss = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream())); while (true){ gameId = iss.readInt(); if(gameId == 667) { playerData = iss.readInt(); for (int i = 0; i <= 9; i++) { if (clients[i] != null){ System.out.println("---sending: "+gameId+" "+playerData); clients[i].oss.writeInt(667); clients[i].oss.writeInt(13); clients[i].oss.flush(); } } } } } catch (IOException e) { e.printStackTrace(); } } |
Recipient thread (place where I'm geting bad data) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public void run(){ try { while(true){ data = Connector.getInstance().getFromServer().readInt(); if(data == 667) { int i = Connector.getInstance().getFromServer().readInt(); System.out.println("byte b: " + data + " int: "+i); } Thread.sleep(1); } } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } |
The 'i' variable very often displaying strange number (0 and 155) but always should be 13 why this happening and how to fix it? I hope it's enough information.
|
|
|
|
|
25
|
Game Development / Newbie & Debugging Questions / Re: Problem with animation speed
|
on: 2012-01-12 22:49:02
|
You are abusing threads! I strongly recommend that you only use a single thread, since synchronization between threads is pretty advanced.
Your problem lies in the BulletMovement class. You're sleeping PER BULLET, not per update. The more bullets you have, the less times you will call update() on your Bullets per second. Get rid of the threads, remove all the sleep()s from bullet movement and creation and call that code from the main loop. Threads are not meant to be used in this way.
Oh my goodness it's true, how could I miss it. From what I can see, you are creating a NEW bullet class in the update method every 500 milliseconds. The creation of the new object is what's causing the slow down. One approach, you can create the bullet in the init and create an array (int locations[] bulletXLocations = new int[100] and one for Y location) with enough space for the number of bullet you think you will need. Each time a bullet fires keep track of the total number of bullets for the player. Then in paint loop through the location array up to the number of bullet for the player, using the single bullet object you created, with the locations from the arrays.
You right. Thanks a lot.
|
|
|
|
|
26
|
Game Development / Newbie & Debugging Questions / Problem with animation speed
|
on: 2012-01-12 20:27:51
|
Hello I am very new to graphic and I have problem with speed animation. I have simple „game” with one player, when I press key space player start shooting, unfortunately bullet animation start slow down. All bullets are holding in list and they are creating in separate thread which detecting pressed space. Bullet position also is updating in separated thread. Animation start slowing even if in bullet list i have two objects. Whay this happening and how to fix it? Ok, here it is some part of 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 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
| public class CanvasTest extends Canvas implements KeyListener { public void init(){ setSize(500, 500); setBackground(Color.BLACK); addKeyListener(this); setVisible(true); BulletCreator bc = new BulletCreator(this); Thread b = new Thread(bc); b.start(); BulletMovement bm = new BulletMovement(this); Thread tm = new Thread(bm); tm.start(); } public void paint(Graphics g){ for(Bullet bullet : getBullets()){ bullet.drow(g); } repaint(); }
@Override public void update(Graphics g) { if(image == null) { image = createImage(500, 500); doubleGraphic = image.getGraphics(); } doubleGraphic.setColor(getBackground()); doubleGraphic.fillRect(0, 0, 500, 500); doubleGraphic.setColor(getForeground()); paint(doubleGraphic); g.drawImage(image, 0, 0, this); } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { fire = true; } e.consume(); }
@Override public void keyReleased(KeyEvent e) { fire = false; e.consume(); }
public boolean isFire() { return fire; }
public CopyOnWriteArrayList<Bullet> getBullets() { return bullets; } ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class BulletCreator implements Runnable {
private CanvasTest canvas; public BulletCreator(CanvasTest canvas) { this.canvas = canvas; } @Override public void run() { while (true) { try { if (canvas.isFire()) { Bullet player1Bullet = new Bullet(10,45, canvas.getImage(".\\images\\bullet.gif")); Thread.sleep(500); canvas.getBullets().add(player1Bullet); } } catch (InterruptedException e) { e.printStackTrace(); } } } } |
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
| public class BulletMovement implements Runnable {
private CanvasTest canvas;
public BulletMovement(CanvasTest canvas) { this.canvas = canvas; }
@Override public void run() { while (true) { try { if (canvas.getBullets().size() > 0) { for (Bullet bullet : canvas.getBullets()) { bullet.update(canvas); Thread.sleep(10); } } Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } }
} |
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
| public class Bullet { protected Image img; public Rectangle rect; private int xPos; private int yPos; public Bullet (final int xPos, final int yPos, final Image img){ setxPos(xPos); setyPos(yPos); this.rect = new Rectangle(getxPos(), getyPos(), 4, 4); this.img = img; } public void drow (Graphics g){ g.drawImage(img, getxPos(), getyPos(), 4, 4, null); } public Image getImage(String img) { return Toolkit.getDefaultToolkit().getImage(img); } public void update(CanvasTest canvas) { if (getxPos() < 5 || getxPos() > Config.WIDTH-5) { canvas.getBullets().remove(this); this.rect = new Rectangle(getxPos(), getyPos(), 4,4); } else { setxPos(getxPos() + 1); rect.x += 1; } } public int getxPos() { return xPos; }
public void setxPos(int xPos) { this.xPos = xPos; }
public int getyPos() { return yPos; }
public void setyPos(int yPos) { this.yPos = yPos; }
} |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|