Little Longhorn – Part 3 – Collision, Detection and Selection (Help needed)

image 
As you might already know, I’m currently trying to get some of the basic stuff implemented for an “Tower defense”-genre (TD) game into Little Longhorn. I’m making progress, but have now run into a little problem. In this blog post I will try to explain which features that are coursing the trouble and hopefully, after I got some input/help from some of you guys out there, I can also post the solution for others to learn from and use.
Let me also make it clear that I’m not asking for complete code samples provided by one of you guys, I’m asking for a good dialog about this problem and different possible solutions for it. I will figure out the code myself, it’s the “thoughts behind” or what you want to call it I’m after here, some inspiration you could say. Hoping for some good input :)

WARNING: This post includes my own hand-made drawings! :)

Course of the problem(s)
Well, the main reason to this/these problems are that Little Longhorn is a little different than a normal TD. In normal TD games I see a lot of use of the mouse to interact with the game, Little Longhorn doesn’t use a mouse for anything. You are now thinking, “But how are you going to place towers, upgrade them etc?”. Instead of a mouse, you have an character you play in the game. This little guy is controlled by the arrow keys at the moment. This little player character will walk around, avoiding being hit by enemies, while he collect gold and build new towers, controlled by you of course. So you actually have to “walk” to where you want your tower and then build it there. It might sound weird to you, if you are use to play the standard online TD games, but believe me, this works and adds a hole different aspect to the game.
The above is what course the troubles I’m having here, but let me explain what I’m trying to archive and then you might see the problem.

The actual problem(s)
The following is what I need to have implemented in the game.

“Normal” collision
I need basic collision in the game. This is actually almost in place in the code and is actually working ok. My implementation is pretty similar to what Joel Neubeck describes in this article, I just have it all divided up in classes etc, but the basics are the same. This collision mechanism should be used to detect is the player is colliding with an enemy.

(Yes, I made this drawing myself :) Keep in mind that all graphics you see on screenshots, drawings etc, are all just “temp” graphics, so don’t get confused if an enemy looks like an alien in one image and a beetle in another one)

image

This is the most common collision (and maybe the mechanism that all the rest of the features can use? I’ll get back to that later). GameObject A intersect with GameObject B, and a “Collide()” method is called on both GameObject’s which they can act upon.

“Block” collision
At some point in the game, I need to be able to place objects that that player can’t go through, ex. a rock. This is essential the same collision as the “normal” one, I just can’t figure out how to implement it? All my “GameObjects” moves around using the normal “Vector/Velocity” approach (if you don’t know this approach Joel Neubeck describes this too in this article series). I have tried setting the Velocity equals to an “Zero” when the player hits a “rock” (actually i tried it on an enemy just for testing purpose), but it didn’t have the right effect :)

image

“Detection” of enemies
One of the basic features in a TD game, is the towers ability to detect enemies in a range and shoot at them. I have this implemented in the game right now actually, but it’s done by using the “normal” collision detection described above. This works “ok”, but is it the “right” way to do it? What I have done now is that I have given the towers a big “CollisionSphere”, which is actually a collision object, but act as a “range of sight” for the tower. Is this the right way to do it? I mean, at the moment, when the enemy is inside the towers “range” a collision is happening in every tick of the game. Is that wise or could this be done in another and better way?

image

“Selection” of elements
As stated earlier you don’t have a mouse in this game, all interaction is done through your player character. In the game you can only build towers on special elements that I (the level editor) put in there. This mean that when you want to build a tower to need to move your player character close to one of these “building” spots, hit “space” and a build-menu will show up. You choose the type of tower you want to build, and hit “space” again and your tower is build on that spot(this isn’t actually implemented in the game yet). The same approach is used when you want to delete or upgrade a tower. Move close to it and hit “space” and the options for this tower will appear.
The problem for me, at this point in time, is this “selection” mechanism. Basically, in my head that is, this is also a kind of “collision”, but at the same time it’s conceptually not, because nothing is actually colliding logically. 

image

 

Let’s sum it up
All the above can somehow to boiled down to being “collision” related. I just can’t figure out how I should implement these features. I have tried out different things but nothing seems to be “good”. In my mind all of the above should be able to be archived by using the same “intersection” algorithms that I use already and then adjust the behavior of the colliding objects properly, it’s just not that simple. I’m pretty empty on good idea for how to implement this, so all input are welcome. Please let me know if you need more information about something and I will gladly give it to you.

You can get the current source code of the game here. The game is not intended to be an open source game, but I have decided to share the code in this early stage, to give you guys the best possibility to help me out here. And then you can also see how I have build the game so far. Keep in mind that the source is very work-in-progress, which is why there are a lot stuff in there that isn’t used for anything.

Hope for great dialog and some good feedback/ideas.

Thanks in advance :)

  • Sergey

    1. You said in “Block” collision: …but it didn’t have the right effect :)
    But what effect it had??

    2. “Detection” of enemies

    I think you act quite right.

    3. “Selection” of elements: ..this is also a kind of “collision”, but at the same time it’s conceptually not, because nothing is actually colliding logically.

    What I think about it:

    – It’s doesn’t matter about your conceptual confusion if it works well :) . As you said before in answer to my post: "let the pattern be the solution to my problems, not a barrier", now replace the word "pattern" with "concept" :)
    – You can just find the closest center of your "building" spot to your character’s center because there may be more then one spots next to it and there may be trouble while using collision to detect it (which of two spots to shoose?). But anyway even in this case you can choose only the first one and skip the others.

  • Sergey

    P.S.

    1. Your site(blog) hangs up my Firefox all the time while loading :)

    2. Be careful with Joel Neubeck’ articles: there are enough logical errors. Also some of his code is too complicated, for example colliding bounging box and sphere. Here is how it looks in my code now:

    static public bool Check(BoundingBox b, BoundingSphere s)
    {
    Vector shiftCenter = AbsVector(b.Center – s.Center);
    double extX = b.Extent.X + s.Radius;
    double extY = b.Extent.Y + s.Radius;

    if (shiftCenter.X <= extX && shiftCenter.Y <= extY)
    {
    b.Hit = s.Hit = true;
    return true;
    }

    return false;
    }

    Best Regards :)

  • Hi Sergey
    First of all, thanks for taking your time an read the entire post, I was a long one, too long I have learned.

    I have created a post on the silverlight forum, linking to this post, hoping people could help me from there: http://betaforums.silverlight.net/forums/t/114354.aspx

    1. It had a "sliding" effect. I actually knew that setting the Velocity to 0 wasn’t what I needed, I just tried it out for fun. There are a post in the forum thread talking about how I can make this work. Haven’t had time to look at this "Block" collision yet.

    3. Well, I see what you mean, but for me the code need to make sense. I’m currently working on a solution about this "Detection" and "Selection" stuff, it doesn’t work right now, but I’m sure I’m on the right track, just need time to implement it fully. I got the idea for the implementation I’m currently working on by talking to Cameron Albert and reading the posts on the silverlight.net forum thread.

  • 1. I have tried it only once actually. I’m using FireFox normally and it doesn’t hang for me. Sadly I can’t reproduce the problem, so don’t know whats wrong and can’t fix it.

    2. I know, even though I’m refering to the Joel Neubeck articles pretty much, they are not THE right way and completely error free. Actually he had a pretty bad bug in the Vector struct, which is used pretty many places. Right now I’m using the collision code, pretty much as he provided it, because it works right now, and then later on I will go back and optimize it.

    PS: Damm, comments look like crap here on my blog, when there are smilies in them. I need to look into that. :)

  • Sergey

    It’s ok – expressive smiles must be large :)