• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
destructivArts

Updating the Player in a Quad Tree

5 posts in this topic

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:
[CODE]
if(data[i].getClass() == new Player().getClass()){
Player p = (Player)data[i];
p.update(keyboard, mouse);
data[i] = p;
}
[/CODE]
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
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Ok, I'll keep that in mind.

One other question:
I've implemented an ObservableInput object that calls this update function every frame:
[CODE]
public void updateInput(MouseHandler m, KeyHandler k){
//Mouse
mousePos = m.getMousePos();
//Keyboard
keys = k.getKeyStates();
}
[/CODE]
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.
[CODE]
public void update(Observable obs, Object obj){
}
[/CODE]
And the players:
[CODE]
@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();
}
}
[/CODE]

I'll keep looking in the meantime,
Thanks again for all the help!
Peter
1

Share this post


Link to post
Share on other sites
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:
[code]
if(Key.Pressed(W))
{
position += GetDirection() * speed;
}
if(Key.Pressed(A))
{
Direction -= 1;
}
if(Key.Pressed(D))
{
Direction += 1;
}
[/code]
etc...

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

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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0