# Some basic questions from beginner

This topic is 1761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

First of all, hello everyone.

I have already made some super-basic 2D games like TicTacToe, snake and pong. I think I am starting to understand how simple games work. Collision detection in pong is simple, because there is only one ball and 2 objects that it can hit. But when I think about some simple 2D shooter game i feel confused.. Let's say I have my character, some enemies and some platforms, on which I can jump.. How am I supposed to check the collision  of every object ( me, all bullets flying, all enemies and all platforms ). When I have 1 enemy and many bullets I can store the bullets in the array and then use loop to check the collision between the enemy and every element of the array. When I have many enemies and many bullets I can use double loop.. this is the way I see it, I have no idea if it is correct. But what if I have many elements of different types? Enemies, trees, fences, cars etc.. This topic really confuses me.. I hope everything is clear.

Second question, programming side:

In my poorly written code I am mostly using numbers instead of const variables..

// this is my code
enemy array[10];

// this is the experienced programmers code
const int MAX_ENEMIES=10;
enemy array[MAX_ENEMIES];


what is the point of doing it?

##### Share on other sites

You only need to check for collision with surrounding entities. If entityA is only 50 units wide, and entityB is only 100 units wide, then if their distance between each other is more than 100, you don't need to check anything else.

Also, you can partition up your loaded area into sections (think of each section as a 'bucket' of entities), if myEntity is in section7, it only needs to check for collisions against other entities in section7, and maybe a few of the sections next to section7 (incase myEntity is on the edge of both sections).

Further, look into std::vector for your entities and bullets. Twenty thousand bullets or more won't be a problem, but if you have them as a local variable of some function, they very well could be. std::vector stores its memory internally as non-local (dynamic) memory, "on the heap", whereas local variables are stored "on the stack", and the stack has limited memory.

Furtherly also, treat your bullets as a point (x,y) instead of a rect (x,y,width,height), and it'll be easier to check collision for.

Rect to rect collision is simple, but if you are hurting for performance, point to rect collision is even simpler. And when you multiply that collision by twenty thousand bullets, once per frame, you might save quite a bit.

Alsoly further, who says you need to check every bullet every frame?

If it's really a problem, and you have literally over half a million bullets, you could always update half one frame, and half the next frame, or divide the work up in some other method.

Finally, don't worry about performance until your program starts to slow down. Pre-mature optimization slows down your development time, even if it speeds up your program's execution time.

When speed really is noticeably a problem, then use a profiler, and it'll tell you exact what parts of your code are running too slow. People are often surprised at what the real slowdown in their code is - humans are fairly bad at guessing where the slow parts are, but a computer will tell you exactly where they are.

Your focus shouldn't be on fast code, but on clean, easy to read, easy to expand code, in my opinion.

##### Share on other sites

Thanks a lot for the fast response, it seems clear now. I know about the Vector but I never had to use it. I made this topic because when something bothers me I can't stop thinking about it :D I am coding the Pong game while thinking about hundreds of other things.

Now I see that games are much more complicated than I thought..

Again, thanks a lot :)

##### Share on other sites

You had another question in there I missed:

But what if I have many elements of different types? Enemies, trees, fences, cars etc.. This topic really confuses me.. I hope everything is clear.

What is a tree? What is an enemy? What is a fence? What is a car?

I see:

• A rectangle (includes position and size)
• Possibly a velocity (for continuing movement)
• An image
• Logic

All four (tree, enemy, fence, car) have the exact same 'pieces'. The only thing that changes is what image, and what logic.

Trees and fences have a velocity of zero.

So create your 'Entity' as one single type of entity, and initialize them with different values:

Entity = {rectangle{pos,size}, image, velocity, typeOfEntity};

Entity myCar(car_size, "CarImage.png", TYPE_CAR);
Entity myTree(tree_size, "TreeImage.png", TYPE_TREE);
Entity myFence(fence_size, "FenceImage.png", TYPE_FENCE);
Entity myEnemy(enemy_size, "EnemyImage.png", TYPE_ENEMY);

...add all of them to your single array (std::vector) of entities...

Figure out what's the same between the objects, and add it to your Entity class. Anything that's not the same, separate it out.

If you're using C++, you can use polymorphism here for handling the logic, but that might be a bit advanced.

Instead, how about having an 'UpdateEntity()' function, which checks what TYPE of an entity it is, and then calls separate more-specializes functions like "UpdateCar()" and "UpdateEnemy()"?

There are many other methods or techniques (polymorphism, function pointers, components), but I don't want to throw too much at you at one time, so I'm trying to use language features that I'm guessing you already know about. If you have any questions, keep on asking.

I'm not sure whether you're using 'C', C++, or some other language, but the concepts are the same for most languages, even if the syntax looks different.

##### Share on other sites

Yep, I am using C++, but I still don't know much about OO part of this language. In my programs I use just simple classes, constructors, destructors and friend classes. I am reading a C++ book, so eventually I will learn the more advanced techniques.

I have one more question, this time about the game loop..

I know it should look like this:

while(gameRunning)
{
handle_events();
logics();
rendering();
}


handling events is just taking the player input?

rendering.. this is obvious

but the logic part bothers me.. I see it as a main part of the loop, where most things happen. Calculating collision, velocities, tons of other stuff and at the end changing the coordinates of the objects. But finally I come to the point, where I have no idea where to move the objects and where to calculate stuff.