Show Posts
|
|
Pages: [1] 2 3
|
|
1
|
Game Development / Newbie & Debugging Questions / Re: Threads for long computations?
|
on: 2013-05-20 02:51:58
|
Here is an example with Futures. 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
| import java.util.concurrent.Future; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.Callable;
public class Test { static int fib(int n) { if(n < 2) return n; else return fib(n-1) + fib(n-2); } public static void main(String[] args) throws Exception { ForkJoinPool pool = new ForkJoinPool(); Future<Integer> future = pool.submit(new Callable<Integer>() { public Integer call() { System.out.println(Thread.currentThread()); return fib(45); } }); System.out.println(Thread.currentThread()); while(!future.isDone()) { System.out.println("running"); Thread.sleep(500); } System.out.println(future.get()); } } |
|
|
|
|
|
3
|
Game Development / Newbie & Debugging Questions / Re: 2D Isometric View
|
on: 2013-05-19 11:54:48
|
|
Yes, if you use libgdx you can use the Stage and Camera classes to do these things and more. But if you don't want to use any libraries and it's a relatively simple 2D game my solution is a good fit. However instead of having a class called CoordinateSystem it may be better to have a Camera class. A camera is basically defined oppositly to the CoordinateSystem class i.e. the camera's position and orientation are relative to the game world.
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / Re: [RPG] How do you move all of these things?
|
on: 2013-05-19 05:20:17
|
|
The player, all the characters and objects in the game and the camera all have a position. That position is not the absolute position on the screen. The positions are always relative to the game map. So when you move or zoom the camera the position of everything else in the game is not affected by that. The screen coordinates are calculated only in the rendering code.
|
|
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Re: 2D Isometric View
|
on: 2013-05-18 22:43:16
|
The last two arguments represent the x and y axis of the coordinate system. The x axis vector for example points from the origin to the point (1,0) in the target coordinate system. So if you define your isometric system as 1 2 3
| CoordinateSystem(Vec(500,100), Vec(1,1), Vec(-1,1)) |
the point (0,0) corresponds to the screen coordinates (500,100). And the two axis of the isometric system point downwards 45degrees like in this image.  So (1,0) in the game means (1,1) + origin in screen coordinates.
|
|
|
|
|
8
|
Game Development / Newbie & Debugging Questions / Re: Finding nearest objects
|
on: 2013-05-09 10:23:27
|
|
This thread is about collision detection. To find all collisions you'd have to call your method for every pair of objects in your game. e.g. if you have 1000 objects you need to call that method 1,000.000 times. With 10l,000 objects 100,000,000 times. And that 60 times per second. So for a large number of objects it's simply too slow. The sorting technique is orders of magnitude faster.
|
|
|
|
|
10
|
Game Development / Newbie & Debugging Questions / Re: Zoom in at mouse (SLICK2D)
|
on: 2013-05-06 21:12:05
|
First you need to have a camera position in your game. The virtual "camera" in a 2D game is simply a rectangle that's positioned somewhere in your 2D world. An example - your world is 10,000 * 10,000 pixels big and your drawing surface (the window you paint in or the entire screen in full screen mode) is 1000 * 1000. Then you might define your camera to have a position of (200, 300) and a size of (1000, 1000). Everything inside that rectangle is displayed on the screen. Most of the time it's sufficient to just remember the camera position and forget about it's size because the Graphics object doesn't need to know if an object is inside the visible area. Drawing stuff outside the screen is not causing any problems. So you can simply always draw all objects. Additionally to the position you also need to store the zoom level of the camera. The draw method will then look like this. 1 2 3 4 5 6 7 8 9 10
| public void draw(Graphics g) { g.scale(getZoomLevel(), getZoomLevel()); g.translate(-cameraPos.getX(), -cameraPos.getY()); for(Entity e:entities) { if(e!=null) { e.draw(g); } } } |
It's convinient to use vectors for positions. So cameraPos should be of type org.newdawn.slick.geom.Vector2f. So replace 1
| public float zX=0, zY=0; |
with 1
| public cameraPos = new Vector2f(0,0); |
Also it's important to remember that a game always has two coordinate systems. So you need to know which vectors are relative to the screen (absolute screen coordinates) and which vectors are relative to the coordinate system of your game. cameraPos is always relative to the game. Furthermore org.newdawn.slick.Graphics uses a coordinate system where (0, 0) is the upper left corner of the screen/window. org.lwjgl.input.Mouse however puts (0, 0) at the lower left corner. This video here demonstrates that. http://thenewboston.org/watch.php?cat=54&number=10So you need to convert your coordinates so (0, 0) is always at the upper left corner. You could do that like this. 1 2 3
| static Vector2f getMousePos() { return new Vector2f(Mouse.getX(), gameContainer.getHeight()-1-Mouse.getY()); } |
Now for the mouseWheelZoom method. I assume you call that every frame? Try this 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public void mouseWheelZoom() { int mouseWheel = Mouse.getDWheel(); if(mouseWheel==0) { return; } if(mouseWheel != 0) { Vector2f mousePosAbs = getMousePos(); Vector2f mousePosRel = mousePosAbs.copy().scale(1/zoomLevel).add(cameraPos); if (mouseWheel>0) { addZoomLevel(zoomStep); } else { addZoomLevel(-zoomStep); } cameraPos = mousePosRel.sub(mousePosAbs.scale(1/zoomLevel)); } } |
I wasn't able to test those modifications since I don't have all of your code but I hope it works.
|
|
|
|
|
11
|
Game Development / Game Mechanics / Re: Arrow Bow Physics - Help!
|
on: 2013-05-05 19:29:10
|
How did you get from arrows to black holes? Anyway, basically you can do it like this. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Arrow { double x, y, speedX, speedy; double gravity = 10; void setSpeedAndAngle(double speed, double theta) { speedX = Math.cos(theta) * speed; speedY = Math.sin(theta) * speed; } void update(double dt) { y += speedY * dt + 0.5 * gravity * dt * dt; speedY += gravity * dt; x += speedX * dt; } } |
|
|
|
|
|
12
|
Game Development / Newbie & Debugging Questions / Re: Finding nearest objects
|
on: 2013-05-05 18:35:51
|
Maybe 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 63 64 65 66 67 68 69 70 71 72 73
| import java.util.*;;
interface Getter { double get(GameObject go); }
class GameObject { public double x, y, sizeX, sizeY; GameObject(double x, double y, double sizeX, double sizeY) { this.x = x; this.y = y; this.sizeX = sizeX; this.sizeY = sizeY; } public double maxDistanceAtCollision(GameObject other) { return Math.sqrt(sizeX*sizeX + sizeY*sizeY) + Math.sqrt(other.sizeX*other.sizeX + other.sizeY*other.sizeY); } public final static Getter getX = new Getter() { public double get(GameObject go) { return go.x; } }; public final static Getter getY = new Getter() { public double get(GameObject go) { return go.y; } }; public String toString() { return "pos = ("+x+", "+y+") - size = ("+sizeX+", "+sizeY+")"; } }
public class Test { static void sortAndFilter(List<GameObject> list, final Getter getter) { if(list.isEmpty()) return; Collections.sort(list, new Comparator<GameObject>() { public int compare(GameObject go1, GameObject go2) { double g1 = getter.get(go1); double g2 = getter.get(go2); if(g1 < g2) return -1; else if(g1 > g2) return 1; else return 0; } }); ArrayList<GameObject> newList = new ArrayList<>(); boolean foundAPairLastTime = false; Iterator<GameObject> li = list.iterator(); GameObject last = li.next(); while(li.hasNext()) { GameObject next = li.next(); boolean foundAPair = Math.abs(getter.get(last) - getter.get(next)) <= last.maxDistanceAtCollision(next); if(foundAPair || foundAPairLastTime) newList.add(last); last = next; foundAPairLastTime = foundAPair; } if(foundAPairLastTime) newList.add(last); list.clear(); list.addAll(newList); } public static void main(String[] args) { List<GameObject> list = Arrays.asList( new GameObject(100,100,20,20), new GameObject(110,110,20,20), new GameObject(200,500,20,20), new GameObject(300,600,20,20), new GameObject(400,700,20,20), new GameObject(310,810,20,20)); List<GameObject> newList = new ArrayList<>(list); sortAndFilter(newList, GameObject.getX); sortAndFilter(newList, GameObject.getY); for(GameObject go: newList) System.out.println(go); } } |
|
|
|
|
|
13
|
Game Development / Newbie & Debugging Questions / Re: Need help moving projectiles with math...
|
on: 2013-01-03 18:22:31
|
Just thought I'd mention that from a computer science point of view, mathematical entities (e.g. Vectors, Matrices, etc) should always be represented by immutable objects ...
That's utter nonsense. Stop listening to whomever told you that. Beside java doesn't even have immutable object (no...it really doesn't). The widespread usefulness of immutables took a huge blow in the 80s with the introduction of SSA form, but even before that this statement would still be nonsense. SSA is just a technique used by compilers to optimize code. It has nothing to do with what I was saying. And it doesn't matter if Java has a keyword built in for defining immutable classes. You can still make objects de facto immutable. I'm even gonna go a step further with my claims and say that making vectors mutable is a form of premature optimization and nothing else. Of course in game/graphic engines it may make sense to do that but unless performance issues force me to optimize my code, I'm gonna stick with immutablity.
|
|
|
|
|
14
|
Game Development / Newbie & Debugging Questions / Re: Need help moving projectiles with math...
|
on: 2013-01-03 02:40:58
|
1 2 3 4 5 6 7
| Vector2D pos = new Vector2D(4, 5); Vector2D dir = new Vector2D(1, 1);
dir.normalize(); dir.multiply(2);
pos.add(dir); |
Using slope to do stuff is kind of complicated, and I feel that vectors can be achieved to do much more. =D Just thought I'd mention that from a computer science point of view, mathematical entities (e.g. Vectors, Matrices, etc) should always be represented by immutable objects except if it would hurt performance. You may notice that classes like Integer, Long, BigInteger, BigDecimal, etc. are all immutable. For simple code it doesn't make much of a difference but if you do complex vector math, mutable vectors can make the code really messy.
|
|
|
|
|
15
|
Game Development / Newbie & Debugging Questions / Re: Increasing cordinates of an image?
|
on: 2013-01-01 17:32:55
|
thanks this really helped understand. if i wasnt making a game is there really a reason to use a while statement over an if?
Instructions in a program are usually executed sequentially, one by one. A while loop is a loop. That means after the last instruction the processor jumps back to the start. so while(a < 100) { do something } means if(a >= 100) jump over the next 2 lines do something jump back by two lines
|
|
|
|
|
19
|
Discussions / General Discussions / Re: Scripting languages / DSLs?
|
on: 2012-09-14 20:09:25
|
There have been some updates to Janino recently, primarily to allow use of javax.tools / javac as a compiler (Janino is far more than just a compiler, it's also the infrastructure such as classloaders, expression wrappers, etc.). Unfortunately, I don't see the inbuilt compiler getting Java 1.5+ features, which means no generics / enhanced for loops, and no lambdas (which will be great for live coding); You could also use clojure as a scripting language. Clojure compiles source code to bytecode on the fly just like janino, but it has lambdas.
|
|
|
|
|
20
|
Discussions / General Discussions / Scripting languages / DSLs?
|
on: 2012-09-09 20:38:02
|
Hi, so I saw this talk recently that mentions how important scripting languages are in game development. http://www.infoq.com/presentations/Functional-Architecturefrom 7:30 to 19:20 If I understand that guy correctly it is usually better to just write a game engine in a language like C++ or Java and then write the game logic in a scripting language. So I was wondering, does that really speed up the development process? And what kind of scripting language is best for games? And what about creating your own language/DSL? If you could design your own language what do you think the syntax should be like to simplify writing of games? Maybe a more descriptive language than a procedural one? For example you could describe all the locations and the properties of objects in the game with an XML based language. XML seems like a good choice because it's easy to parse.
|
|
|
|
|
22
|
Discussions / Miscellaneous Topics / Re: What dafuq?
|
on: 2012-08-30 23:17:00
|
So each time I'm adding two int's I get a new Int(1 + 2) ?
Yes. 1 2 3
| int a = 3; int b = 4; int c = a+b; |
Using BigInteger 1 2 3
| BigInteger a = new BigInteger("3"); BigInteger b = new BigInteger("4"); BigInteger c = a.add(b); |
Both pieces of code behave the same way. If BigInteger was mutable you would get a very different behaviour. 1 2 3
| MutableBigInteger a = new MutableBigInteger ("3"); MutableBigInteger b = new MutableBigInteger ("4"); a.add(b); |
Now a has changed it's value to 7. Something that doesn't happen with immutable BigIntegers and it also doesn't happen with ints. 1 2 3
| int a = 3; int b = 4; a + b; |
Neither a nor b has changed it's value here.
|
|
|
|
|
23
|
Discussions / Miscellaneous Topics / Re: What dafuq?
|
on: 2012-08-30 22:28:04
|
But I still stay with writing things the "imerative" way. So I don't recreate a vector each time I add values to my given vector (I'm speaking of mathematical vectors (think of Vector2f...)) and I still use classes much more than anything else.
In my opinion mathematical entities (e.g. BigInteger, Vectors, Matrices, etc.) should always be immutable. It makes the code cleaner and less error prone and on the oracle JVM optimization is so good that there is no performance difference. Btw in case you want to say that primitive variables are mutable and you can do calculations with them just fine you should realize that primitive variables actually behave exactly like immutable objects, not like mutable objects, even though that may sound strange at first. e.g. A mutable int behaves like an immutable BigInteger object with it's reference stored in a mutable variable except for the lower precision of course.
|
|
|
|
|
24
|
Discussions / Miscellaneous Topics / Re: What dafuq?
|
on: 2012-08-30 18:13:44
|
|
Many eclipse plugins are very unstable. Or maybe it's eclipse itself that's unstable. I tried to install the playn plugin several times and always eneded up having to reinstall eclipse. Anyway, Scala is certainly a nicer language than Java no matter what kind of program you are writing.
|
|
|
|
|
25
|
Game Development / Newbie & Debugging Questions / Re: If and if and if and if...
|
on: 2012-08-29 18:30:44
|
That code is way too long. try this 1 2 3 4 5 6 7 8 9 10 11 12
| ArrayList<Tile> neighbours = new ArrayList<Tile>();
for(int dy = -1; dy <= 1; dy++) { for(int dx = -1; dx <= 1; dx++) { if(dx == 0 && dy == 0) continue; int nx = x+dx, ny = y+dy; if(nx >= 0 && nx < mapWidth && ny >= 0 && ny < mapHeight) { neighbours.add(map[nx][ny]); } } } |
|
|
|
|
|
26
|
Game Development / Newbie & Debugging Questions / Re: Matrix camera, movement concept
|
on: 2012-08-28 22:04:49
|
You should move the player and not the background. When drawing the objects to the screen (platforms, walls, enemies, etc.) you can translate the graphics context to move the camera to the right spot. e.g. The upper left corner of your world has the coordinates (0,0), your player is at (2000,500) and your game runs in a window with a size of 800*600. Every time you draw a frame you calculate the coordinates of the camera. If the player should always appear in the middle of the screen the camera might be at (1600,0). Then you simply translate the graphics object to those coordinates. and then you can draw the player and all the objects using their real coordinates.
|
|
|
|
|
27
|
Game Development / Game Mechanics / Re: Pause/Resume execution in method
|
on: 2012-08-25 21:14:49
|
... but I kind of like when games let the players cheat and completely break the game if they are clever enough, so at the very least I'm going to put in a way to disable or reduce the security, maybe as an unlockable.
Sounds like you are a Microsoft programmer.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|