Thursday, November 29, 2012

Implementing basic collision went almost as smooth as I had expected. Had some problems with keeping the objects separated from eachother when there is a collision. I used a radius-based collision system, which basicly means; when the distance between two objects is smaller than the radius of one, there is a collision.
When there is a collision, I keep the two objects from interpenetrating eachother by displacing object A (the player) in the direction away from object B by an amount just great enough to keep them apart. It feels really smooth and convincing to me, so that's the basic collision so far.

When I got that to work, I immediatly started implementing shooting for the Player. When the Player resses "space", the Player ill shoot. The problem with this was; where do I keep a list of the bullets? If I keep the list in the Player  the bullets will move along with the Player and will have difficulty with the collision detection. I needed to find a way to get those bullets to the GameObjectManager in a seperate Bullet list, but without the Player knowing of the existence of that GameObjectManager. What I did was filling up a list of bullets in the Player and after the update of the Player, the GameObjectManager placed all the contents of the bullet-list in the player to a list in the Manager itself. After that, it clears the list in the Player, to clean up the mess.


for(std::vector<Bullet>::iterator k = i->m_bullets.begin(); k != i->m_bullets.end(); ++k )
{
AddPlayerBullet(&(*k));
}

This is the piece of code that checks in the player("I", since it's looping through a vector of Players) for new bullets and adds it to m_PlayerBullets via the AddPlayerBullet function in the GameObjectManager. I don't really know if this is good practice or a terrible thing to do, but it works for me and the game still performs. I can shoot, the bullets draw and update and they clean up after 5 seconds.

I also made an improvement to the Texture-management. I loaded every Texture apart from eachother and over and over again. I fixed this by creating a TextureManager class with a singleton and a hashmap of textures. I can load textures with TextureManager::getInstance()->m_Textures["name_of_texture_here"]. Makes reusability of textures a lot better.

As usual, I've pushed the code to my GitHub for people to check out what I did exactly. Next up will probably be shooting some enemies or scene-loading. Not sure which one I will do first, since they're both very important, so that's a mystery until the next post appears!

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!