Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (602)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 / Performance Tuning / Re: QuadTrees / Spatial Data Structures on: 2010-05-29 19:39:29
Ah that looks like an interesting document, I will give it a read.

Yes it did cross my mind that I could enclose the other existing tree in a bigger tree but getting the program to know how to do that might be tricky. How would it determine where the new extents of the tree would be?
2  Game Development / Performance Tuning / Re: QuadTrees / Spatial Data Structures on: 2010-05-28 12:05:06
Ah thanks for the great reply. Im glad you've told me that now, I think I will take your advice and avoid even considering them....
I would go with QuadTree but Im not sure how to make use of them in my situation. Is there any way of expanding a QuadTree effectivly without having to recreate the whole thing once you add something outside the initial extents of it?
3  Game Development / Performance Tuning / Re: QuadTrees on: 2010-05-26 07:18:54
Riven seriously, you might not like my method and you might think my code is messy but I don't sew any major problems in it. Maybe it's you who doesn't understand what I'm trying to do. If your just picking small faults with my code that's fair enough but it's not the purpose I'm here for.
My idea was based on the applet:
1. Does this node have points?
2. If not then it must have 4 more nodes
3. Run this same method on the 4 nodes
4. Eventually it would come to a point where there are no more children nodes where the points in the last node are checked.

Maybe it isn't the correct way and has fundimental problems but at least it was an attempt at a solution instead of just critising my lack of knowledge. If it's so easy why don't you tell me what the answer is?

Demonpants yes I see the best way would be to find the node first but isn't that pretty much what I'm doing above? I know it's over complex but I couldn't think of another way that would suit an arbiruary tree size and complexity. I don't know anything about quadTrees or their techniques which is why I'm here.

If there is a website that shows some useful techniques I could study then do tell me because I am a newbie and I think people like Riven forget that....

I'm actually building a simple terrain modelling tool, not a game itself and I think I haven't thought it through completely as I can't see how quadtrees could be effective in my needs now. I might start looking for another spacial data structure. I know of one program that uses triangulation for example. Any help or direction would be appriciated.

EDIT: I have been searching and I think I may have a better solution possibly, instead of using QuadTrees I could use R-Trees? Does anyone have experience with these:
4  Game Development / Performance Tuning / Re: QuadTrees on: 2010-05-25 23:53:07
Thanks for the advice, yes they where silly mistakes from rushing, I need to sort out the code once I've got it working.

I dont know what is wrong with my strategy though, I thought it would be a very scalable system.

I think I may have come up with a system to find the closest point but I havent checked or tested it yet.

public Point getClosestPoint(double x, double y){
        Point closestPoint = null;
        if(isInside(x,y)){              //is the point within the bounds of this node?
            if(contents.size() > 0){    //Does this node contain any points?
                double closestDistance = contents.get(0).distance2DSquared(x, y);
                for(Point p : contents){
                    double distance = p.distance2DSquared(x, y);
                    if(distance < closestDistance){
                        closestPoint = p;
                        closestDistance = distance;
            } else {
                if(children[0] != null && children[0].isInside(x,y))
                    closestPoint = children[0].getClosestPoint(x, y);
                if(children[1] != null && children[1].isInside(x,y))
                    closestPoint = children[1].getClosestPoint(x, y);
                if(children[2] != null && children[2].isInside(x,y))
                    closestPoint = children[2].getClosestPoint(x, y);
                if(children[3] != null && children[3].isInside(x,y))
                    closestPoint = children[3].getClosestPoint(x, y);

        return closestPoint;

    public boolean containsPoint(Point point){
            return true;
        if(children[0] != null && children[0].containsPoint(point))
            return true;
        if(children[1] != null && children[1].containsPoint(point))
            return true;
        if(children[2] != null && children[2].containsPoint(point))
            return true;
        if(children[3] != null && children[3].containsPoint(point))
            return true;
        return false;

If you have a better solution for me please do share as I am not exaclty an advanced programmer or specialist in this area but at least Im trying...
5  Game Development / Performance Tuning / Re: QuadTrees on: 2010-05-25 22:38:21
Sorry If Im too generic.
It can change completely at any time, either in number of points or size.
So I've started writing a class that allows for more flexibility, Ill show you what I have so far.

class QuadTreeNode{
      private QuadTreeNode[] children;
      private double x, y, width, height;
      private Set<Point>contents;
      private BoundingBox boundingBox;
      public QuadTreeNode(double x, double y, double width, double height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

        boundingBox = new BoundingBox(x,y,x + width, y + height);
        contents = new ArraySet<Point>();
        children = new QuadTreeNode[4];

    public int size(){
        return contents.size();
    public boolean isInside(double x, double y){
        return boundingBox.isInside(x, y);

    public void split(double x, double y){
        double halfW = width / 2;
        double halfH = height / 2;
        children[0] = new QuadTreeNode(x - halfW, y - halfH, width/2, height/2);
        children[1] = new QuadTreeNode(x - halfW, y, width/2, height/2);
        children[2] = new QuadTreeNode(x, y - halfH, width/2, height/2);
        children[3] = new QuadTreeNode(x, y, width/2, height/2);

    public void add(Point point){
        if(children[0].isInside(point.getX(), point.getY())
                && children[0] != null){
        } else
        if(children[1].isInside(point.getX(), point.getY())
                && children[1] != null){
        } else
        if(children[2].isInside(point.getX(), point.getY())
                && children[2] != null){
        } else
        if(children[3].isInside(point.getX(), point.getY())
                && children[3] != null){
        } else {

      private class BoundingBox{
          private double minX, minY;
          private double maxX, maxY;

          BoundingBox(double minX, double minY, double maxX, double maxY){
              this.minX = minX;
              this.minY = minY;
              this.maxX = maxX;
              this.maxY = maxY;

          public boolean isInside(double x, double y){
              if(x > minX && x < maxX)
                  if(y > minY && y < maxY)
                      return true;

              return false;

Its not finished yet but it should give you an idea. I need tio figure out how to get and alter the tree as data changes.
So now I have my tree if I have an x and y coordinate how would I go about using the tree to find the closest point in an efficient way?

EDIT: I have also come across another problem, I do not have set extents to the active area so if I setup a quadTree and then a point is added outside its extents Im abit stuffed...
6  Game Development / Performance Tuning / Re: QuadTrees on: 2010-05-25 19:29:14
Ok so I've started using the code you have gave me which is great but Im trying to taylor it to my needs more. first off I need to be thinking in imaginary units like metres instead of working in pixels because my graphics will be scalable. So although your 100 pixal threshold might work for an average game it would would not work in my case. The resolution of the QuadTree would need to adapt to the size of the area Im working with whether it be 100m or 100,000m.

Here is a better example of what Im after:

Thats a brilliant little applet!
7  Game Development / Performance Tuning / Re: QuadTrees on: 2010-05-25 13:03:42
Ah thanks for the great reply. I will look at this in more detail.

Well for the nature of what Im doing I might not have constant movement and the objects may be spread over a large area but your correct, I will just try to make something that works and then worry about more performance gains if its still not enough later.
8  Game Development / Performance Tuning / Re: QuadTrees on: 2010-05-25 12:21:23
So heres roughly how I think it would work just to show I've made an effort into thinking about it.

So I have a QuadTree class which has 4 subsequant collections (lets say sets for my purpose) which represent quadrants. So each of those sets have 4 more sets and each other those 4 sets which have 4 sets etc.
But of course they are only split up into 4 sets if its required.

What I cant quiite think of is how do I know when to split up a set into 4 quadrants and given a list of coordinates how would the quadTree class know where to put the points within its self...

Another thing I thought of is that I dont want the 4 quadrants to be split evenly because over a big area that could prove inefficient, but determining where the centre goes is yet another question.

The thing is Im sure I could figure something out but it needs to be 100% efficient because otherwise it defeats the point in the first place and it could make or break my project.


9  Game Development / Performance Tuning / QuadTrees / Spatial Data Structures on: 2010-05-25 10:50:30
It was suggested to me here in these very forums that QuadTrees would be very useful for my problems and I researched into them and the theory looks great but the problem is I dont even know how to start implementing them in Java....

At first I thought it might just be a new type of collection but now Im starting to think it will be more complex than that.

Has anyone used quadTrees before for anything?
For me its to stop the program from drawing/calculating graphics offscreen and also for calculating the closest object to the mouse cursor which I am currently doing by iterating over a list and checking each object which proves waaaaay too slow when you are talking about thousands of objects at once.


10  Java Game APIs & Engines / Java 2D / Re: Pan / Zoom Algorithm on: 2010-05-23 14:34:36
Omg man I think you have it! How the heck have I missed that....

I was considering if I could do it about the mouse X & Y also but I dont think it would be as easy as the centre of the screen.

Thanks for the help Karmington its been a big help!
11  Java Game APIs & Engines / Java 2D / Re: Pan / Zoom Algorithm on: 2010-05-23 14:16:46
What you say makes sense but Im just trying to think how to implement it.

Here is an example of what I mean:
I have a point at 1000,1000, of course this would be off screen if the screen is 500,500 lets say.
So as we pan towards the point the x & y origin value will increase (or decrease depending on hows it done) and for example f we pan 500 pixels to the right then the equation for relative x is:
x = 1000 + (- 500) = 500
so the point would be drawn at 500,1000.

Of course then you have the scale so currently I am doing this:
(1000 * 1) + (-500) + (500/2) = 750

I add half screen width because I want it based on the centre of the screen and not the bottom conrer.

You can test it in excel but right now my brain is fried as I've been trying to work this out for ages...
12  Java Game APIs & Engines / Java 2D / Re: Pan / Zoom Algorithm on: 2010-05-23 13:52:10
Hmm Im not quite sure what you mean. Im not drawing one pannable/scalable image although maybe I should be drawing to an image first.
Im drawing individual lines and points.
13  Java Game APIs & Engines / Java 2D / Re: Pan / Zoom Algorithm on: 2010-05-23 13:46:06
What do you mean?

Yes well If Im panning and then scroll with the mouse wheel I want the graphics to get larger from a base point of the middle of the screen, otherwise if its based on 0,0 then everything goes flying off the screen and I have to pan to find it again.....
14  Java Game APIs & Engines / Java 2D / Pan / Zoom Algorithm on: 2010-05-23 12:01:58

Im creating something that requires I pan and zoom some graphical data.
So below is the code I have come up with which almost works but Im really struggling to get my head around this whole thing.

So the idea of these methods is that I pop in some absolute coordinates and it gives me the position in which to draw the coordinate in the display (a JPanel).
Im sure you can guess what xScale and yScale are for and xOrigin and yOrigin are used to offset the drawing in relation to panning.

public double getRelativeX(double x){
        double relativeX = (x* xScale) + xOrigin +(getWidth()/2);
        return relativeX;

public double getRelativeY(double y){
        double relativeY = (y * -yScale) - yOrigin+(getHeight()/2);
        return relativeY;

At the moment it does the job but when I zoom it zooms about the absolute point 0,0 when I want it to do it about the centre of the screen, any ideas how I need to change it?

Also this is my zooming/panning code:

public void mousePressed(MouseEvent e) {
                    // capture starting point
                    lastOffsetX = e.getX();
                    lastOffsetY = e.getY();

            public void mouseDragged(MouseEvent e) {
                    // new x and y are defined by current mouse location subtracted
                    // by previously processed mouse location
                    int newX = e.getX() - lastOffsetX;
                    int newY = e.getY() - lastOffsetY;

                    // increment last offset to last processed by drag event.
                    lastOffsetX += newX;
                    lastOffsetY += newY;

                    // update the canvas locations
                    xOrigin += newX;
                    yOrigin -= newY;

                    // schedule a repaint.

public void mouseWheelMoved(MouseWheelEvent e) {
                if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {

                        xScale -= (mouseSensitivity * e.getWheelRotation());
                        yScale -= (mouseSensitivity * e.getWheelRotation());

                        xScale = Math.max(0.001, xScale);
                        yScale = Math.max(0.001, yScale);

One last important note, you may notice the X and Y methods are slightly different, thats because I want the coordinate system to start from the bottom left and not the top left corner and increase as you move upwards not downwards.

Thanks for any help,

15  Game Development / Shared Code / Re: 2D Map editor on: 2010-05-07 10:36:48
I wish I could make a tool as cool and well done as that Smiley
16  Game Development / Newbie & Debugging Questions / Re: Applets still viable? on: 2010-05-07 10:04:45
I have never deployed anything onto the web before so I am speaking strictly as a user and heres what I think.

I like the idea of wbestart for large or complicated apps/games because you know its going to be simple and you know you wil get all the uptodate stuff.

I dont like the idea of webstart for smaller games/apps because it annoys me downloading them and waiting for them to load, feels like Im downloading lots of rubbish, applets feel alot more intergrated and you dont notice the downloading etc. which is nice for adding simple things to websites and especially for games.

Straight downloads are ok I guess but I dont see the purpose of it for games...

And games that require installation (I have seen java application and even games like this) is a big no no, I will never give anything like that a try...

I would say unless the type of game calls for more, applets are best for games in my opinion as a user.
17  Game Development / Newbie & Debugging Questions / Re: Load in local file on: 2010-05-07 09:49:59
Thanks that brilliant.
18  Discussions / General Discussions / Re: JDialog on: 2010-05-06 13:26:59
Yes thats logical, why do people always refer to stacks then? lol
19  Discussions / General Discussions / Re: JDialog on: 2010-05-06 12:31:53
Ah I see I think I get the rough theory behind it. Its hard for a new programmer to learn these things when they happen behind the scenes, its not something you implement yourself.

So you have a list of events to occur and the pump goes down the stack one by one performing them...
20  Discussions / General Discussions / Re: JDialog on: 2010-05-06 11:53:08
Calm down  Undecided
I didnt say what he said was correct, I dont know or I wouldnt be here!
All I said was what he says makes sense...

I will take alook at your code but I dont think it will mean much to me, I dont even understand what you mean by event pump.
EDIT: Ah yes I roughly see what you've done, ok I believe you its the same thread.
21  Game Development / Newbie & Debugging Questions / Re: Load in local file on: 2010-05-06 10:25:15
Ah thank you so much that is the type of answer I was looking for. No my file system is as follows:


So both folders (gui and resources) are in the structures folder so I presume the following code would be correct then:

new File(System.getProperty("user.dir") + "/structures/resources/UBProperties.csv")

I thought the directory had to be relative to the class refering to it. So the System.getProperty("user.dir") will give the directory of my com folder? Even if its in a jar...

Ah yes I see what you mean, I dont work in the industry and Im a lone programmer so I either dont know or forget what good practices are for organising a project. Thanks for the advice its easy to change in netbeans at least, think I might need to rethink things abit...
22  Discussions / General Discussions / Re: JDialog on: 2010-05-06 10:15:00
What you say jezek2 makes perfect sense to me, it creates a new thread and pauses the main thread hence why my methods do nothing and why you cant do anything with any other GUI outside the dialog.

I changed my code to what I suggest and it worked anyway so Im happy  Smiley

Learn something new everyday...
23  Game Development / Newbie & Debugging Questions / Re: Load in local file on: 2010-05-05 15:42:51
No Im using Netbeans and as far it seems it appears automatically in the projects manager bit. It did just start working and Im still not sure if this new method did actually fix it or if it was something like you mentioned.

Now I have further problems with it, how do I get it to work in a Jar file, it says it cant find it...
24  Discussions / General Discussions / Re: JDialog on: 2010-05-05 15:08:03
Stupid question (as I cant test it right now) but what would happen if I did the following then?

if(currentBeam != null && bendingDiagram != null){
            CalcsDialog calcs = new CalcsDialog(this, true);
25  Discussions / General Discussions / Re: JDialog on: 2010-05-05 14:53:59
No problem!

Ill not give you the full classes, just the relavent bits.

The dialog:

public class CalcsDialog extends javax.swing.JDialog {

    /** Creates new form CalcsDialog */
    public CalcsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

    public void log(String text){

The class accessing the dialog (my main frame on the main thread):
When the button is pressed.
if(currentBeam != null && bendingDiagram != null){
            CalcsDialog calcs = new CalcsDialog(this, true);

Hopefully that gives you a clue how it works, it must be something fundimentally wrong as theres not much to it...
26  Discussions / General Discussions / Re: JDialog on: 2010-05-05 14:43:43
Thanks, I do find it hard to get my head around these threading concepts etc. but I roughly understand.

Well nothing it depends on happens after setVisible() eg create the text area, setup the gui etc. The only thing that happens is calls to the one method I have mentioned before.

From what you are saying does it mean that I should use the log method and append text to text area after I create the dialog but before I setVisible?
27  Discussions / General Discussions / Re: JDialog on: 2010-05-05 14:16:06
Oh really, well yes its modal so that mean it has its own event thread?
So technically I've got some multithreading going on here then. The main thread is using the public method log so does that mean I need to implement some form of protection and could this have something to do with my problem?
28  Discussions / General Discussions / JDialog on: 2010-05-05 12:58:34

I think my problem is caused by my lack of knowledge of extending JDialog, heres my situation:

I have a JDialog that is crated and setVisible(true) when you click a button. The dialog only has a JTextArea as its only component. I have created a public method called log(String text) which appends whatever text I enter to the text area within the dialog.

My problem is that when I use this method nothing happens, no text added to the area, no exceptions, I checked the strings and there fine. Is this due to the event dispatching thread or have I forgot something?


29  Game Development / Newbie & Debugging Questions / Re: Load in local file on: 2010-05-02 16:48:22
Thanks for the replys, I have used your method of using File.separator which is helpful but this didnt actually solve my problem.

I've decided to use a different method pointed out to me by someone because as you say the method seems correct but this new method using input streams seems to have worked.

Thanks anyway.
30  Game Development / Newbie & Debugging Questions / Load in local file on: 2010-05-02 12:04:16

I've posted here because I feel like Im being a newless clubie at the moment.

The class loading in the file is located at:

The file is located at:

So my code is:
try {
      File ubProperties = new File(this.getClass().getResource("../resources/UBProperties.csv").toString());
      fileScanner = new Scanner(ubProperties);
} catch (FileNotFoundException ex) {
    Logger.getLogger(UBTemplateBuilder.class.getName()).log(Level.SEVERE, null, ex);

and the output is:
02-May-2010 12:52:29 com.structures.elements.UBTemplateBuilder initTemplates
SEVERE: null file:\M:\Java%20Projects\BeamDesigner\build\classes\com\structures\resources\UBProperties.csv (The filename, directory name, or volume label syntax is incorrect)
        at Method)
        at java.util.Scanner.<init>(
        at com.structures.elements.UBTemplateBuilder.initTemplates(

Now the directory in the exception is correct but claims it isnt...
I may be missing something stupid here but I've put this code together based on abunch of other examples, not any one so if anyone knows the best way to do what Im trying to do I would appriciate it.

By the way Im on Windows XP but of course want OS independancy.


Pages: [1] 2
Jesse (14 views)
2015-07-29 04:35:27

Riven (36 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (29 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

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 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‑
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!