Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 02 Sep 2012
Offline Last Active Apr 27 2013 04:03 AM

#5039212 Tips for starting development of a Java 3D Game

Posted by on 04 March 2013 - 04:23 PM

Literally every question you posted can be found on these forums. I don't mean to be rude, but use the search feature so somebody else doesn't have to for you. These threads come about very often and have been answered countless times before.

#4975925 Handling many objects

Posted by on 02 September 2012 - 08:57 PM

After some research I found that HashTable is included in a Java package "Java.util.Dictionary", but it is legacy now. Using a HashMap seems to be the way to go, so I'll try that for now.

I looked into some spacial data structures and I think I'd like to avoid them for now if possible. R-Trees and Quadtrees are the two I looked at and to be honest they seem pretty daunting to me at a glance. I think I'd be better off using conditionals to check the coordinates of an object on the game map.

#4975777 Handling many objects

Posted by on 02 September 2012 - 11:27 AM

I think you need something like this. While an array might not be the best choice, you need a place where all you game objects are put in so you can do things with all of them like your move() or a general gamelogicupdate() or a render() etc.

I prefer to have a hashtable to store my game objects in. This comes with some handy features:
- you can still iterate over all your objects
- every object you create can get a unique id and you can store it in the hastable by that unique id. With an array you could use the index for that, but as the game continues, you get higher and higher ids while old objects get removed and you get holes in your array. OR you could put new objects at the places of removed objects, but then, your index-ids aren't unique anymore.
- each object having a unique id allows objects to reference each other without tricky sideeffects. For eaxmple imagine a unit A chase another unit B. You could add a reference of B in A and that way A can check B's location in the gamelogicupdate() and knows where to move. However, what if B gets destroyed? It get removed from the scene, but it keeps existing because A has a reference to it. If A only knew B's id, it can do the same as above by checking the hashtable of B and get B's position. When B gets destroyed, A will notice because the hashtable lookup will fail. Also note that references/pointers are only valid locally. When you add network play, you absolutely need some kind of unique ids.

Instead of a hashtable one could use a map. Besides that, you probably want a spatial datastructure as well to put your game objects in so you can do queries like "give me alle objects within a radius of 5m of point p.". One might be able to combine these datastructures somehow, I don't know thought. I always used those two next to each other.

This is what I was looking for. I've never dealt with hashtables or maps in Java so I'm gonna go ahead and look at some references on those now. I appreciate the help!

Edit: At a glance, I notice that hashtables are similar to associative arrays in other languages. I take it that I'd still have to iterate through the hashtable and call GameObject.Update() on each iteration. Do you think this is better than an object recursively controlling itself? I'm trying to choose the best method to handle objects before I expand and cause a jumbled mess for myself.

#4975733 Handling many objects

Posted by on 02 September 2012 - 08:33 AM

Acc to the current situation that goes without any doubt that you code will get more and more complex with the increase in the number of the units there will be more loops more array and so on insted of using loops for ur job why dont u give a try to a time class makin and independet method for each class or making a sinlge one and adjusting all the elements acc to that single timer results way easier then that for loops

Are you saying I should make each object update on its own (either by making it runnable or using a while loop)? I guess I'm not understanding how this would work where the object would still be able to interact with everything else that is updating on a different cycle.

#4975712 Handling many objects

Posted by on 02 September 2012 - 06:52 AM

I'm currently making a 2d RPG in Java and its engine is coming along quite nicely.. however I am a bit confused on how I should handle a certain situation. I have a class Unit which is any game character (subclasses for NPCs, Player, etc). The problem is there are always going to be several instances of this class and each one will need to update independently each time the canvas is rendered.

What's the best way to manage this? Currently each time gameUpdate() runs (optimally 60 times per second), it loops through an ArrayList that contains each Unit. Is there any way to forego using an array to handle each object? It seems like using an array like this will come back to haunt me when the game is more complex.

This is what I have now and I'd like to improve it or scrap it altogether.
[source lang="java"]package engine;import java.util.*;public class ObjectHandler extends ArrayList<GameObject> { private static final long serialVersionUID = -8598610051757901108L; public ObjectHandler() { System.out.println("ObjectHandler loaded."); } public static ObjectHandler getObjectHandler() { if (ref == null) ref = new ObjectHandler(); return ref; } // This method is ran every time // the game is updated. gameUpdate() public void update() { for (int i = 0; i < size(); i++) { get(i).move(); } } // This method is ran every time // the game is updated. gameRender() public void drawList() { String name; for (int i = 0; i < size(); i++) { name = null; name = get(i).name; } } /* OBJECTS ADD THEMSELVES IN THEIR CONSTRUCTOR * public void add(String unitName) { Unit unit = new Unit(unitName); //System.out.println(unit.name); add(unit); }*/ private static ObjectHandler ref;}[/source]