Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (582)
Games in Android Showcase (163)
games submitted by our members
Games in WIP (633)
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: calculateBoundaries on: 2015-04-15 16:35:23
I'll just have to take some lessons on trigonometric functions. I never took trigonometry.  Lips Sealed

I found some homework. http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/3d-basics-r673

Thanks for your time.
2  Game Development / Newbie & Debugging Questions / Re: calculateBoundaries on: 2015-04-15 14:55:00
Sure, I understand that. I was just pointing out that the value '99999' is arbitrary. I'm sure it's fine in the given context, but for modern code I'd use the actual numeric limit for the type instead. I understand this is old code though.

The person who wrote this can be spotted riding a giant ant on his twitter. He wrote this specific function I would guess about 12 years ago.

Beyond that, honestly this doesn't look like the easiest code to unravel Tongue Assuming though that sceneX/Y/Z is the model position relative to the camera, maybe we can make sense of the first few lines.


sceneX/Y/Z is indeed the location relative to the camera. So model position is just the delta of the positions? I'm not familiar with that lexicon.

1  
2  
3  
sceneX = objectX - cameraX
sceneY = objectY - cameraY
sceneZ = objectZ - cameraZ


The variable 'a' looks like it's maybe the z value of the sceneX/Y/Z position after being rotated into view space to account for yaw. 'b' may be a similar correction for pitch.

I find it difficult to read rotations, how were you able to suspect that it is the z value? I see both sceneZ, sceneX, and the cameraYaw is used. But I find it hard to picture in my head the point of negating x * sin(yaw) from z * cos(yaw). View space is just another word for screen coordinates right?

The function then early-outs if the object is outside the projection range based on these values.

It looks like the code that follows does similar checks against the edges of the frustum and so on. It would take some time to make sense of it all, but that's a start at least.

That is precisely the functionality. I also forgot to mention that the constants NEAR_Z and FAR_Z equal 50 and 3500 respectively. The function returns any time the model is completely off the screen, but may also sometimes disappear earlier due to what I assume are precision errors or limitations on angles. But let's just ignore that information for now. Smiley

3  Game Development / Newbie & Debugging Questions / Re: calculateBoundaries on: 2015-04-15 12:40:53
Hi, first I'd like to thank you for replying. Grin

The elliptic paraboloid stuff is interesting, but I don't think it's directly relevant here (it's just incidental to the fact that you're performing length and squared length computations).

I suspected it to be that way. Was fun to look at tho!  Lips Sealed

1  
maxDepth = sqrt( x^2 + z^2 + minY^2 )

You've omitted the inclusion of 'minDepth' in the original code, although that may be intentional.

I forgot it.  Cheesy

The use of integers and the arbitrary boundary initializers (+/-99999) could also be problematic, depending on the circumstances, but maybe that's done for a reason as well.

That is done for a reason. For example, the minimum boundaries start as very high numbers because the vertices corresponding component should always be below it. The opposite goes for max boundaries.

If you need more info, perhaps you could tell us a little more about the context, such as where the code is from, what space the computations take place in, and so on.

The code is from an old RuneScape game client from 2004. calculateBoundaries is usually only called once after a model has been constructed. The fields it assigns are used in the drawing of the model. (The game has software based rendering)

I couldn't identify any of the variables a thru j because of my lack in knowledge. Ignore the right bit shifts by 16, the numbers use fixed-point integers.

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  
   public final void draw(int yaw, int sinCameraPitch, int cosCameraPitch, int sinCameraYaw, int cosCameraYaw, int sceneX, int sceneY, int sceneZ, int uid) {
      int a = ((sceneZ * cosCameraYaw) - (sceneX * sinCameraYaw)) >> 16;
      int b = ((sceneY * sinCameraPitch) + (a * cosCameraPitch)) >> 16;
      int c = (this.lengthXZ * cosCameraPitch) >> 16;
      int d = b + c;

      if (d <= Scene.NEAR_Z || b >= Scene.FAR_Z) {
         return;
      }

      int e = ((sceneZ * sinCameraYaw) + (sceneX * cosCameraYaw)) >> 16;
      int minScreenX = e - this.lengthXZ << 9; // screen width is 512. (1 << 9) = 512

      if (minScreenX / d >= Graphics2D.centerX) {
         return;
      }

      int maxScreenX = e + this.lengthXZ << 9; // screen width is 512. (1 << 9) = 512

      if (maxScreenX / d <= -Graphics2D.centerX) {
         return;
      }

      int f = ((sceneY * cosCameraPitch) - (a * sinCameraPitch)) >> 16;
      int g = (this.lengthXZ * sinCameraPitch) >> 16;

      int maxScreenY = (f + g) << 9; // screen width is 512. (1 << 9) = 512

      if (maxScreenY / d <= -Graphics2D.centerY) {
         return;
      }

      int h = g + (this.maxBoundY * cosCameraPitch >> 16);

      int minScreenY = (f - h) << 9; // screen width is 512. (1 << 9) = 512

      if (minScreenY / d >= Graphics2D.centerY) {
         return;
      }

      int i = c + (this.maxBoundY * sinCameraPitch >> 16);
      boolean project = false;

      if (b - i <= Scene.NEAR_Z) {
         project = true;
      }

      boolean hasInput = false;

      if (uid > 0 && allowInput) {
         int j = b - c;

         if (j <= Scene.NEAR_Z) {
            j = Scene.NEAR_Z;
         }

         if (e > 0) {
            minScreenX /= d;
            maxScreenX /= j;
         } else {
            maxScreenX /= d;
            minScreenX /= j;
         }

         if (f > 0) {
            minScreenY /= d;
            maxScreenY /= j;
         } else {
            maxScreenY /= d;
            minScreenY /= j;
         }

         int x = mouseX - Graphics3D.centerX;
         int y = mouseY - Graphics3D.centerY;

         if (x > minScreenX && x < maxScreenX && y > minScreenY && y < maxScreenY) {
            hasInput = true;
         }
      }

      int cx = Graphics3D.centerX;
      int cy = Graphics3D.centerY;

      int yawsin = 0;
      int yawcos = 0;

      if (yaw != 0) {
         yawsin = sin[yaw];
         yawcos = cos[yaw];
      }

      for (int v = 0; v < vertexCount; v++) {
         int x = vertexX[v];
         int y = vertexY[v];
         int z = vertexZ[v];

         if (yaw != 0) {
            int w = z * yawsin + x * yawcos >> 16;
            z = z * yawcos - x * yawsin >> 16;
            x = w;
         }

         x += sceneX;
         y += sceneY;
         z += sceneZ;

         int w = z * sinCameraYaw + x * cosCameraYaw >> 16;
         z = z * cosCameraYaw - x * sinCameraYaw >> 16;
         x = w;

         w = y * cosCameraPitch - z * sinCameraPitch >> 16;
         z = y * sinCameraPitch + z * cosCameraPitch >> 16;
         y = w;

         vertexDepth[v] = z - b;

         if (z >= Scene.NEAR_Z) {
            vertexScreenX[v] = cx + (x << 9) / z;
            vertexScreenY[v] = cy + (y << 9) / z;
         } else {
            vertexScreenX[v] = -5000;
            project = true;
         }

         if (project || texturedCount > 0) {
            projectSceneX[v] = x;
            projectSceneY[v] = y;
            projectSceneZ[v] = z;
         }
      }

      try {
         draw(uid, project, hasInput);
      } catch (Exception ignored) {

      }
   }
4  Game Development / Newbie & Debugging Questions / calculateBoundaries on: 2015-04-15 03:10:27
Before you begin reading the code below, let it be known that this is how the coordinate system works that the snippet is for:

+X = Right
-Y = Up
+Z = Forward

I'm not very familiar with geometry, but I'm confident in my understanding and research abilities to any information given to me.

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 void calculateBoundaries() {
      this.lengthXZ = 0;

      this.minBoundX = 999999;
      this.maxBoundX = -999999;

      this.maxBoundY = 0;
      this.minBoundY = 0;

      this.maxBoundZ = -99999;
      this.minBoundZ = 99999;

      for (int v = 0; v < this.vertexCount; v++) {
         int x = this.vertexX[v];
         int y = this.vertexY[v];
         int z = this.vertexZ[v];

         if (x < this.minBoundX) {
            this.minBoundX = x;
         }

         if (x > this.maxBoundX) {
            this.maxBoundX = x;
         }

         if (z < this.minBoundZ) {
            this.minBoundZ = z;
         }

         if (z > this.maxBoundZ) {
            this.maxBoundZ = z;
         }

         if (-y > this.maxBoundY) {
            this.maxBoundY = -y;
         }

         if (y > this.minBoundY) {
            this.minBoundY = y;
         }

         int lengthSquared = x * x + z * z;

         if (lengthSquared > this.lengthXZ) {
            this.lengthXZ = lengthSquared;
         }
      }

      this.lengthXZ = (int) Math.sqrt((double) this.lengthXZ);
      this.minDepth = (int) Math.sqrt((double) (this.lengthXZ * this.lengthXZ + this.maxBoundY * this.maxBoundY));
      this.maxDepth = this.minDepth + (int) Math.sqrt((double) (this.lengthXZ * this.lengthXZ + this.minBoundY * this.minBoundY));
   }


I'm trying to understand this more. lengthXZ is later multiplied by the sin(cameraPitch) and cos(cameraPitch) values for some algorithm to check if the model is within the screen. The most important part of this code to me is understanding the assignments to lengthXZ, minDepth, and maxDepth.

minDepth and maxDepth are used for sorting triangles by depth before being drawn.

I did some googling and found Elliptic Paraboloid which has the equation z = Ax^2 + By^2. I feel like it's related, as the Y and Z coordinates in this application have been flipped, so they must apply in the same way for y = Ax^2 + Bz^2. http://www.math.umn.edu/~rogness/quadrics/ellparab.shtml

By the looks of it, these are the equations for the assignments: (minDepth using maxY and maxDepth using minY was not an oversight)
1  
2  
3  
lengthXZ = sqrt( x^2 + z^2 )
minDepth = sqrt( x^2 + z^2 + maxY^2 )
maxDepth = sqrt( x^2 + z^2 + minY^2 )


Any input is appreciated. Thanks  Grin Grin
5  Game Development / Newbie & Debugging Questions / Re: Texture Space to World Space (Heavy Math) on: 2014-12-20 20:02:18
I watched his entire 3d software playlist. It's very interesting and a learning experience. Although, the math is different here because all calculations are done with integers and fixed-points. You may have provided a very useful resource, but it still does not answer my question. Thanks for your input though. Smiley
6  Game Development / Newbie & Debugging Questions / Texture Space to World Space (Heavy Math) on: 2014-12-20 01:58:13
I'm trying to reverse engineer some methods used for drawing textured triangles. I'm having an issue identifying certain values so I can properly name them.

All of the top fields from "a" to "i" are the most concerning.

Can anyone help me identify these?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
      // The difference between these points?
      sceneX1 = sceneX0 - sceneX1;
      sceneY1 = sceneY0 - sceneY1;
      sceneZ1 = sceneZ0 - sceneZ1;

      // Now what are these?
      sceneX2 -= sceneX0;
      sceneY2 -= sceneY0;
      sceneZ2 -= sceneZ0;

      // This is looking so difficult because I can't tell what the previous
      // values are about..
      int a = ((sceneX2 * sceneY0) - (sceneY2 * sceneX0)) << 14;
      int b = ((sceneX1 * sceneY0) - (sceneY1 * sceneX0)) << 14;
      int c = ((sceneY1 * sceneX2 - sceneX1 * sceneY2)) << 14;

      int d = ((sceneY2 * sceneZ0) - (sceneZ2 * sceneY0)) << 8;
      int e = ((sceneY1 * sceneZ0) - (sceneZ1 * sceneY0)) << 8;
      int f = ((sceneZ1 * sceneY2) - (sceneY1 * sceneZ2)) << 8;

      int g = ((sceneZ2 * sceneX0) - (sceneX2 * sceneZ0)) << 5;
      int h = ((sceneZ1 * sceneX0) - (sceneX1 * sceneZ0)) << 5;
      int i = ((sceneX1 * sceneZ2) - (sceneZ1 * sceneX2)) << 5;



(Was incredibly massive in my post so I puush'd it.)
Code: http://puu.sh/dBNEq/8060548eea.txt
7  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 12:53:08
I will.... I think I'll call it..... Lua!

But really, it's not thaaaat bad, riiiight?
8  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:34:38
I could add listeners to when you set variables too though!  Roll Eyes

Think about the possibilities!  Clueless
9  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:32:19
Because I always prematurely optimize things, I want to do the opposite just to mess around. I could make a component system like .add(Component.class, instance) and .get() etc.
10  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:27:52
HashMaps are pretty bad for cache locality, good ol' ArrayList has your back 99% of the time. And you know what they say about premature optimization. Have you attempted to time ArrayList<Point> etc. vs. Map? I'll bet List is faster.
It's also almost guaranteed that you don't need every last drop of perf anyway. Don't think too hard about it until you need to.

Actually I was trying to do the opposite of optimization. Giving myself the seemingly worst optimization case scenario for a 2d game I want to make. The point of using a Map is so I could freely add/remove keys and values of any type to my entities.
11  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:09:52
Oh. I switch them around and get about the same exact results. The long string in place of the short string, and vice versa. I guess this means I will have to put my hope on the optimizer then. Thanks for letting me know.

Do you have any opinion on using this KeyValue class for things like storing Entity X/Y which would be constantly read/written to? I imagine it would be more optimized in that case if I were to encapsulate the x/y coordinates in an Object like a Point or something as well. I know it would be muuuch better to just define a field, but I also want to be able to add/remove any variable with any name from an entity.
12  Game Development / Newbie & Debugging Questions / How does this work? on: 2014-11-17 03:58:25
Am I doing something wrong? I wouldn't expect the second one to have a faster result..

Output:
1  
2  
Average 1: 7.33694838
Average 2: 4.06036178


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  
package dane.game.model;

import java.util.HashMap;
import java.util.Map;

public class KeyValue {

   private Map<Object, Object> keyvalues = new HashMap<>();

   public <T> T set(Object key, T value) {
      keyvalues.put(key, value);
      return value;
   }

   public <T> T get(Object key) {
      return get(key, null);
   }

   @SuppressWarnings("unchecked")
   public <T> T get(Object key, T defaultValue) {
      if (!keyvalues.containsKey(key)) {
         keyvalues.put(key, defaultValue);
      }
      return (T) keyvalues.get(key);
   }

}


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  
package dane.game.world;

import dane.game.model.KeyValue;

public class Entity extends KeyValue {

   static final int ITERATIONS = 1_000_000;
   static final double NS_TO_MS = 1_000_000.0;

   public static void main(String[] args) {
      Entity e = new Entity();

      // set values
      e.set("x", 0);
      e.set("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", 0);

      double total1 = 0, total2 = 0;

      for (int n = 0; n < 100; n++) {
         long time = System.nanoTime();

         for (int i = 0; i < ITERATIONS; i++) {
            e.get("x");
         }

         total1 += (System.nanoTime() - time) / NS_TO_MS;

         time = System.nanoTime();

         for (int i = 0; i < ITERATIONS; i++) {
            e.get("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
         }

         total2 += (System.nanoTime() - time) / NS_TO_MS;
      }

      System.out.println("Average 1: " + (total1 / 100));
      System.out.println("Average 2: " + (total2 / 100));
   }

}
13  Game Development / Newbie & Debugging Questions / Re: Diagonal Movement on 2D Tilemap on: 2014-11-15 23:24:02
1  
2  
3  
4  
5  
6  
   boolean isSolid(final int x, final int y) {
      if (x < 0 || x >= MAP_SIZE || y < 0 || y >= MAP_SIZE) {
         return true;
      }
      return solid[x][y];
   }


The thing is, I'm trying to have a tile map with pixel movement. My goal is to have Legend of Zelda styled movement. The reason I overly complicated it was because I'm trying to also make it work for entities that are larger than just one tile.

I appreciate the input though.
14  Game Development / Newbie & Debugging Questions / Diagonal Movement on 2D Tilemap on: 2014-11-15 16:37:42
I need ideas on what I should be doing if I'm walking perfectly diagonal into a tile with my logic.

Here's the entire snippet processing the movement:

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  
   public void updatePlayerPosition() {
      int destX = playerX;
      int destY = playerY;

      // hardcoded for now
      final int speed = 2;

      if (Keyboard.isDown(KeyEvent.VK_W)) {
         destY -= speed;
      } else if (Keyboard.isDown(KeyEvent.VK_S)) {
         destY += speed;
      }

      if (Keyboard.isDown(KeyEvent.VK_A)) {
         destX -= speed;
      } else if (Keyboard.isDown(KeyEvent.VK_D)) {
         destX += speed;
      }

      // movement differences
      int dx = destX - playerX;
      int dy = destY - playerY;

      // we're not moving, so we're gunna stop here.
      if (dx == 0 && dy == 0) {
         return;
      }

      // Tile boundaries our player occupies
      final int minTileX = playerX / TILE_SIZE;
      final int minTileY = playerY / TILE_SIZE;

      final int maxTileX = (playerX + (TILE_SIZE - 1)) / TILE_SIZE;
      final int maxTileY = (playerY + (TILE_SIZE - 1)) / TILE_SIZE;

      // TODO: larger sized players
      // TODO: higher movement speeds?

      int srcX = dx < 0 ? maxTileX : minTileX;
      int srcY = dy < 0 ? maxTileY : minTileY;
      int dstX = srcX + (dx < 0 ? -1 : 1);
      int dstY = srcY + (dy < 0 ? -1 : 1);

      // we're moving diagonally
      if (dx != 0 && dy != 0) {
         // is that next diagonal tile solid?
         if (isSolid(dstX, dstY)) {
            // TODO: what can we do to prevent glitching on corners?
         }
      }

      // we're moving horizontally
      if (dx != 0) {
         boolean blocked = false;

         // check every tile vertically in that direction.
         for (int y = minTileY; y < maxTileY + 1; y++) {
            if (isSolid(dstX, y)) {
               blocked = true;
            }
         }

         if (blocked) {
            // we were blocked from going right, set ourselfs to the left of
            // the blocker.
            if (dx > 0) {
               playerX = (dstX - 1) * TILE_SIZE;
            }
            // vice versa
            else {
               playerX = (dstX + 1) * TILE_SIZE;
            }
         } else {
            playerX = destX;
         }
      }

      // we're moving vertically
      if (dy != 0) {
         boolean blocked = false;

         // check every tile horizontally in that direction
         for (int x = minTileX; x < maxTileX + 1; x++) {
            if (isSolid(x, dstY)) {
               blocked = true;
               break;
            }
         }

         if (blocked) {
            if (dy > 0) {
               playerY = (dstY - 1) * TILE_SIZE;
            } else {
               playerY = (dstY + 1) * TILE_SIZE;
            }
         } else {
            playerY = destY;
         }
      }
   }


This works perfectly both horizontally and vertically, but when you go diagonally then you phase into that tile. Here's an example of that happening:


This isn't an incredible problem, as you can't actually continue moving through the tile you've phased into. But it's not beautiful to see the flickering of your character as it phases in and out diagonally each tick.

This is the position you would be in right before this happens.


The orange rectangle is outlining your current tile boundaries. My collision detection only checks if you're moving either vertically or horizontally, and doesn't handle when you're going both. This means that when I'm going diagonal towards the top right, it checks the tile to my right, and the tile above me. Those are both translucent, so the code runs true and sets my position into that diagonal tile.

Are there any solutions you guys could think of? I thought of just making it so I don't move horizontally when I collide diagonally, which fixes it. But then that breaks movement for running against a wall while trying to move towards it parallel.
15  Game Development / Performance Tuning / Re: Can somebody explain this loop? on: 2013-07-02 21:02:05
It's actually the loop that was used in a game called RuneScape, but this is from their #317 revision client which is from 2006. It was originally obfuscated so all the names were like 'anInt4' 'anLong1'. So the variable names are most likely nowhere near the original.

If you read closely to what it's doing though, it makes somewhat sense. It's just a matter of figuring out what exactly they were trying to do. The original source probably had documentation on what was going on and an explanation.

Here are some points that I found could contain some information

1  
2  
3  
4  
5  
         // 2560 is 256 * 10
         // 2560 is also half of what ( interval * 256 ) should be.
         if (currentTime > operationTime[operationPos]) {
            ratio = (int) (2560 * (interval / (currentTime - operationTime[operationPos])));
         }


1  
2  
3  
4  
5  
6  
         // At this point, ratio should be 256 if everything went smoothly.
         // (1000 * 256) = 256000
         // (20 * 256) = 5120
         // 256000 / 5120 = 50 (Goal FPS)
         
         fps = (1000 * ratio) / (interval * 256);
16  Game Development / Performance Tuning / Re: Can somebody explain this loop? on: 2013-07-02 14:39:53
There's a lack of replies, and it's probably because of my lack of context, sorry. Specifically what I'd like to know is what exactly 'ratio' is suppose to be doing, as well as the 'operationTime(s)'.

Is the ratio used to tell whether or not it should wait a little longer or go faster in-case of lag?

Are the operation times used for lag compensation as well?
17  Game Development / Performance Tuning / Can somebody explain this loop? on: 2013-07-01 16:03:46
So here it is:

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  
package org.dane.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JApplet;
import javax.swing.JFrame;

public class TestLoop extends JApplet implements Runnable {

   private Thread thread;
   private boolean active;
   private BufferedImage image;

   // Other Variables
   int operationPos = 0;
   int ratio = 256;
   int delay = 1;
   int count = 0;
   int interruptionCount = 0;
   int interval = 1000 / 50; // 50 is the goal fps.
   int fps = 0;
   long[] operationTime = new long[10];
   long currentTime;

   public static void main(String[] args) {
      JFrame frame = new JFrame("Test");
      frame.setLayout(new BorderLayout());
      frame.setResizable(false);
      frame.add(new TestLoop(256, 256), BorderLayout.CENTER);
      frame.pack();
      frame.setVisible(true);
   }

   public TestLoop(int width, int height) {
      this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      this.setPreferredSize(new Dimension(width, height));
      this.thread = new Thread(this);
      this.thread.start();
   }

   @Override
   public void run() {

      this.active = true;

      for (int i = 0; i < operationTime.length; i++) {
         operationTime[i] = System.currentTimeMillis();
      }

      while (this.active) {

         ratio = 300;
         delay = 1;
         currentTime = System.currentTimeMillis();

         // What's this doing?
         if (currentTime > operationTime[operationPos]) {
            ratio = (int) (2560 * (interval / (currentTime - operationTime[operationPos])));
         }

         if (ratio < 25) {
            ratio = 25;
         }

         if (ratio > 256) {
            ratio = 256;
            delay = (int) (interval - (currentTime - operationTime[operationPos]) / 10L);
         }

         if (delay > interval) {
            delay = interval;
         }

         operationTime[operationPos] = currentTime;
         operationPos = (operationPos + 1) % 10;

         // ???
         if (delay > 1) {
            for (int i = 0; i < 10; i++) {
               if (operationTime[i] != 0L) {
                  operationTime[i] += delay;
               }
            }
         }

         if (delay < 1) {
            delay = 1;
         }

         try {
            Thread.sleep(delay);
         } catch (InterruptedException e) {
            interruptionCount++;
         }

         for (; count < 256; count += ratio) {
            // TODO: logic();
         }

         fps = (1000 * ratio) / (interval * 256);

         this.repaint();

      }

   }

   @Override
   public void paint(Graphics graphics) {
      Graphics g = this.image.getGraphics();

      g.setColor(Color.WHITE);
      g.fillRect(0, 0, this.getWidth(), this.getHeight());

      g.setColor(Color.BLACK);

      int x = 4;
      int y = 16;

      for (int i = 0; i < 10; i++) {
         int optim = (operationPos - i + 20) % 10;
         g.drawString("optim" + optim + ": " + operationTime[optim], x, y);
         y += 16;
      }

      g.drawString("fps: " + fps + " ratio: " + ratio + " count: " + count, x, y);
      y += 16;
      g.drawString("delay: " + delay + " interval: " + interval, x, y);
      y += 16;
      g.drawString("interruptions: " + interruptionCount + " opos: " + operationPos, x, y);

      graphics.drawImage(this.image, 0, 0, null);
      graphics.dispose();
   }

   private static final long serialVersionUID = 3633102351301301398L;

}
18  Game Development / Newbie & Debugging Questions / Re: Keyboard input wont work on: 2013-02-28 08:58:55
Also if you're going to use more than 3 if statements, you should use a switch:

1  
2  
3  
4  
5  
6  
7  
8  
9  
switch( keyCode )
{
   case KeyCode.VK_UP:
      ypos++;
      break;
   case KeyCode.VK_DOWN:
      ypos--;
      break;
}


For multiple keys doing the same thing:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
switch( keyCode )
{
   case KeyCode.VK_W:
   case KeyCode.VK_UP:
      ypos++;
      break;
   case KeyCode.VK_S:
   case KeyCode.VK_DOWN:
      ypos--;
      break;
}


Good night.  Grin
19  Game Development / Game Mechanics / Re: Single Thread Scheduled Executor For Processing on: 2013-02-27 23:41:55
Well actually I never said this was superior to anything, I just asked for your opinions of it.  Grin
20  Game Development / Game Mechanics / Single Thread Scheduled Executor For Processing on: 2013-02-27 21:28:48
I was wondering what your opinions are of this:

 Grin

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  
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class Game implements Runnable
{

   private ScheduledExecutorService service;
   private boolean running;

   public Game()
   {
      this.service = Executors.newSingleThreadScheduledExecutor();
      this.start();
   }

   public void start() {
      if (!running) {
         this.service.submit(this);
      }
   }

   public void stop() {
      this.running = false;
   }

   @Override
   public void run() {
      try {

         // Do stuff here.

         // Sleep
         Thread.sleep(50);

         // If we're still active, re-submit.
         if (this.running) {
            this.service.submit(this);
         }

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}
21  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-25 03:05:29
Huh

When I said writing your own rasterizer, I meant it as a joke. It will lead to pretty terrible performance compared to LibGDX and you will be nowhere closer to making a 2D game. Can I ask why you didn't like the library and/or how you feel it could be improved? 

Just curious. Wink

LibGDX may have an upper hand on performance, but that doesn't necessarily mean the rasterizer would be bad. Smiley

I didn't enjoy using the library because I feel like I should be learning how to create my own tools and use those tools to make my own content.

I've got a severe case of NIH and so far nothing has cured me of it. Sad


Edit: Actually, I'm thinking of using either LWJGL or some basic wrapper for OpenGL.

I know LibGDX is derived from LWJGL but it's just the way LibGDX is setup that I don't like very much.
22  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-25 01:42:30
After using libGDX, I've determined that it just isn't for me, instead I'll be checking out Relminator's link and just learning to create my own 3D rasterizer.

Thanks everybody for your input! Smiley
23  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-21 06:04:07
The main reason why I don't like using third party libraries is because I like to know how everything works.

For example; I've always wanted to create my own really basic 3D rendering. Similar to what RuneScape used back in 2003-2006. (Software Rendering)
But their client's rendering is so obfuscated that I couldn't learn from it.
24  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 21:35:41
I know what forms data can take.  Roll Eyes

I was just giving an idea of what he could do; which is send the image's pixels in the form of an integer and interpret them on the client as an integer.

Anyways, I don't think sending images from the client to the server is a great idea unless they're being cached/saved somewhere where they can be re-loaded, and if you plan on having those same images change then somehow a way to check if they're the same version as the servers. (Like a CRC check or something.)
25  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 21:22:11
An integer is a primitive data type that is made up of 32 bits. (4 bytes)   Shocked
26  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 16:38:12
After checking LibGDX's Wiki, I've decided to move on from tinkering with my 'while (running)' statement and spamming draw().
I'm comfortable and confident with saying I think this is going to go well. Smiley

You've all helped convinced me, I really appreciate it.  Grin
27  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 15:07:25
There is no spoon image. There are no integers or files or music files or whatever.

If you can send bytes from A to B you can send anything.

If you create a BufferedImage with the type INT_RGB/ARGB then the data is an integer, like a white pixel would be 0xFFFFFF.

Plus an integer is 4 bytes, so I don't see how that wouldn't be applicable.
28  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 06:40:41
Then try lwjgl. I am sure davedes will post his great tutorials on it.  Wink

If you don't like the idea of using libs then why are you using any of the built in java libs?

Haha you got me there, what I meant specifically were libraries other than the stock ones. Tongue
29  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 06:21:26
Here's an idea:

(Assuming the image is a BufferedImage)
Writing:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int[] data = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

// Write the length of the image.
net.writeInteger( data.length );
net.writeShort( img.getWidth() );
net.writeShort( img.getHeight() );

// Write the image's data.
for (int i : data) {
   net.writeInteger(i);
}


Reading:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int available = net.readInteger();
int width = net.readShort();
int height = net.readShort();

BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

for (int i = 0; i < available; i++)
{
   data[i] = net.readInteger();
}

30  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 05:52:15
Thanks, but I was looking for more of an alternative that wouldn't include using libraries that weren't made by myself.
Although- It does look interesting so I will definitely check it out and give it a go! Smiley

The reason why I'm looking for an alternative that doesn't use a library; is because I've got this thing where I don't feel as accomplished if I didn't make 100% of it.
Pages: [1] 2
 
Ralphanese (14 views)
2015-05-29 13:07:55

Gibbo3771 (11 views)
2015-05-29 06:54:17

Ralphanese (21 views)
2015-05-28 16:52:52

theagentd (31 views)
2015-05-27 22:21:17

theagentd (35 views)
2015-05-27 22:21:08

CopyableCougar4 (25 views)
2015-05-27 19:24:50

MrMapcom (24 views)
2015-05-23 20:26:16

MrMapcom (32 views)
2015-05-23 20:23:34

Waterwolf (37 views)
2015-05-20 15:01:45

chrislo27 (44 views)
2015-05-20 03:42:21
Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02
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!