Jump to content

  • Log In with Google      Sign In   
  • Create Account


Updating the Player in a Quad Tree


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 30 August 2012 - 11:54 PM

I'm writing my game in Java, but this seems pretty language independent.
I have a quad tree set up to organize all my game entities.
All game entities are children of an entity class, and that is what the quad tree sees all its data as.
Almost all entities simply get a list of nearby entities for their update function. The player, however, needs this in addition to the keyboard info and mouse info. (I think in typing this I've figured out the solution but I'll ask anyways.) How do I make the tree recognize that it is a Player object and know that it is ok to pass the keyboard and mouse states to it?

I've thought of two ways to solve the problem, I'm just wondering which is most effective (or if there's a better way all together).
1.) When I loop through each nodes data, check if each data object's class equals the player class. Create a new player object equal to this data object, update it, and then replace the original with the new one.
Example:
if(data[i].getClass() == new Player().getClass()){
    Player p = (Player)data[i];
    p.update(keyboard, mouse);
    data[i] = p;
}
2.) (And this is the way I think is best right now.) Create an update function in the Entity class that takes in the keyboard and mouse, which only its Player child class will ever call. In the Player class I just override it, and put in the player update code. Then all I have to do is check if the data's class is the same as Player (as I did above).

I'm still new at this, so I'm just looking for input.
If there's a better way, please let me know.
Thanks,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

Sponsor:

#2 rnlf   Members   -  Reputation: 1057

Like
2Likes
Like

Posted 31 August 2012 - 01:07 AM

I wouldn't recomment iterating through the quadtree for such things. Quadtrees are basically spatial datastructures which's biggest feature is finding what objects are near other objects or within a designated area (like the screen, for example).

Your solution 1 is one of the worst ways to solve this. You are basically relying on runtime type checks, you run this check for each and every entity you have (thus wasting a lot of time) and the compiler is able to do much better than this. Don't do it.

Your solution 2 is a bit better as you let the compiler and JVM do the selection of whether to use the keyboard and mouse information. The problem is, that you bloat up the interface of the base entity class. Every entity will get dependant on mouse and keyboars state. Does a rock on the ground really need keyboard input? I doubt so.

You have, in fact stumbled upon one of the good old known problems in game engine design. There is one quick solution, which will solve a lot of the above problems, but introduces its own new problems: why not create a class external to the game entities that receives all input. Then, you allow other objects to register callback functions for input. Now, every time your input system receives an event, it will deliver it to all registered objects. You may wish to read up on the observer pattern to find out more about how to implement in in Java. This will solve your problem in the short run, but it will still lead to increased interface sizes for the base class. Now that you have input factored out, there is a lot more that you will be tempted to put in the base class, because somehow most of your entities will need it, but not all of them. There may be collision detection, rendering, animations, AI, ... you name it. All of these need to be updated from somewhere and you will feel the need to put that stuff in the entity base class.

There are some nice ways to resolve that problem, but it will not be done in pure OOP. You may search the interwebs for Component Entity Systems.

Hope that helps.

my blog (German)


#3 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 31 August 2012 - 02:13 AM

Thank you, I looked up the Observer/Observable, and I won't actually have to change much to implement it.

As to your concern about putting too much into a base class, can't I just create children classes for each kind of entity I need? All I would have to do is put empty functions into the entity class so that its children could use them. (Entity already takes care of animation updates, rendering, and collision detection.)

Thanks again for the help!
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

#4 rnlf   Members   -  Reputation: 1057

Like
0Likes
Like

Posted 31 August 2012 - 02:34 AM

Yes, you can do that (and it will work okay for small-ish projects), but it is not very clean to do so and in bigger code bases, it will become a mess to maintain. It is also a minor performance drawback to call lots of empty functions. But I guess it is also the most common pattern found in hobby projects. It works okay for a lot of people, just be aware that it is neither the cleanest nor the most reusable way to do it.

my blog (German)


#5 destructivArts   Members   -  Reputation: 205

Like
1Likes
Like

Posted 31 August 2012 - 04:09 AM

Ok, I'll keep that in mind.

One other question:
I've implemented an ObservableInput object that calls this update function every frame:
    public void updateInput(MouseHandler m, KeyHandler k){
	    //Mouse
	    mousePos = m.getMousePos();
	    //Keyboard
	    keys = k.getKeyStates();
    }
For some reason it's not calling the update function for it's observers.

I don't know if this has anything to do with it but I'm setting the Entities as observers when they are placed in a data list of a node. I'm removing them when they have to be shifted, and adding them again when they are placed. I know the ObservableInput object sees them, so I don't know why it's not updating.

Just to be thorough, heres the entity update function.
public void update(Observable obs, Object obj){
}
And the players:
    @Override
    public void update(Observable o, Object obj){
	    System.out.println("1");
	    if(o instanceof ObservableInput){
		    ObservableInput obs = (ObservableInput)o;
		    buttonState = obs.getButtonState();
		    mousePos = obs.getMousePos();
		    keys = obs.getKeyState();
	    }
    }

I'll keep looking in the meantime,
Thanks again for all the help!
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

#6 slayemin   Members   -  Reputation: 2020

Like
1Likes
Like

Posted 31 August 2012 - 05:02 AM

I'd recommend putting a layer of abstraction between the input devices (mouse and keyboard in this case) and the game object. That way, the input devices are more interchangable and you don't have to worry about how the device is being used.

For example, suppose I have a space ship object. I want to give the play the ability to move the space ship object forward in the direction its facing, and to turn the direction of the space ship object.
W - Makes the space ship go forward
A - Turns the space ship to the left
D - Turns the space ship to the right
S - Slows the space ship down

If you bind the keys like this:
if(Key.Pressed(W))
{
   position += GetDirection() * speed;
}
if(Key.Pressed(A))
{
   Direction -= 1;
}
if(Key.Pressed(D))
{
   Direction += 1;
}
etc...

Instead, do this:
if(Key.Pressed(W))
{
    Spaceship.Thrust();
}
if(Key.Pressed(A))
{
    Spaceship.Turn(-1);
}
if(Key.Pressed(D))
{
    Spaceship.Turn(1);
}

By calling the methods for the objects to manipulate the objects instead of manipulating the objects directly, lets other entities control the object -- like an AI controller. So, both the player and an AI call Spaceship.Thrust(), and can be interchanged with very little code rewriting.

When you're using the quad tree to handle collision detection and any update code (AI), you shouldn't care about what is controlling the entity. All you care about is where the object is located spatially and how you want to update the object with respect to nearby objects.

Eric Nevala

Currently a self-employed indie game dev





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS