# SDL collision help

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

## Recommended Posts

Hi
I am completely new to programming but i am trying to learn SDL and C++. I have run into a problem , i have got two players who is going to collect points while a "monster" is bouncing around and they must avoid it. The number of monsters is going to increase by time.

The problem is that the collision between the players and monsters isn't working.
I have pasted the parts of the code that i think might be the problem. It is not the whole program.
Sorry for the spelling , English isn't my first language.

 globals.h extern SDL_Rect mob; globals.cpp SDL_Rect mob; functions.cpp bool check_collision( SDL_Rect A, SDL_Rect B ) { //The sides of the rectangles int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; //Calculate the sides of rect A leftA = A.x; rightA = A.x + A.w; topA = A.y; bottomA = A.y + A.h; //Calculate the sides of rect B leftB = B.x; rightB = B.x + B.w; topB = B.y; bottomB = B.y + B.h; //If any of the sides from A are outside of B if( bottomA <= topB ) { return false; } if( topA >= bottomB ) { return false; } if( rightA <= leftB ) { return false; } if( leftA >= rightB ) { return false; } //If none of the sides from A are outside B return true; } dot.h // The players private: SDL_Rect box; dot.cpp Dot::Dot(int x, int y) { box.x = x; box.y = y; box.w =DOT_WIDTH; box.h =DOT_HEIGHT; xVel = 0; yVel = 0; dot_score = 0; hp=3; } if (check_collision( box, mob)==true) { box.y =240; box.x =320; hp--; dot_score-=5; } enemy.h // The monsters class Enemy { private: SDL_Rect mob; } enemy.cpp Enemy::Enemy(int x, int y) { mob.x=x; mob.y=y; mob.w =DOT_WIDTH; mob.h =DOT_HEIGHT; xVel = 10; yVel = 10; } 

##### Share on other sites
Which are the values of DOT_WIDTH and DOT_HEIGHT?

##### Share on other sites
Your main problem is that you've got a block of code floating in your dot.cpp.

if (check_collision( box, mob)==true) { box.y =240; box.x =320; hp--; dot_score-=5; }

Is this really just sitting at the bottom of dot.cpp? If not, you've missed out some important code that we need to see. For instance, where it exists in code or the function it is contained within.

It should be being checked every frame or two.

If you wish, I'd be happy to look at your entire code for the game and comment areas that you might need to look into. Feel free to send me a private message.

##### Share on other sites
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

### DOT_WIDTH and DOT_HEIGHT has the value 20. TehOwn, sent you a Pm.[/font]

##### Share on other sites

DOT_WIDTH and DOT_HEIGHT has the value 20.
TehOwn, sent you a Pm.

Looking through your code, it seems you are having some trouble understanding the use of objects in object orientated programming. Also, you have several unnecessary #includes. I'll get round to the collision detection, but mostly the cause is due to your code being very difficult to understand.

Where have you been learning SDL and C++ programming?

Your problem with the actual mob / player collision is simple. You define the collision box for the "mob" as a global in Globals.h and Globals.cpp, then you redefine it under Enemy like so:
class Enemy { private: SDL_Rect mob; int xVel, yVel; public: Enemy(int x, int y); bool Status_left; void show(); void move(); };

A quick fix would be to simply remove "SDL_Rect mob;" from class Enemy and use the global instead. But this doesn't handle the inherent design issues with your style of coding and you'll likely run into problems again.

Hopefully, we can give you a good reference to study from and learn some of the object orientated concepts before you end up bashing your head against a brick wall trying to figure this out.

You're on the right PATH, you just need some better education before going any further.

##### Share on other sites
I have used LazyFoo´s tutorial for the SDL but I have pretty much studied C++ on my own. I began with programming in october last year so i have not been using it alot. I have just tried different ways to solve problems but this one i haven't figured out.
If i remove SDL_Rect mob then the two monsters i have created get stuck together and the velocity is doubled.

The reason I have choosed this kind of program is because of its a School work. We got to choose something to work with and the present it to the class. I am very interested in programming so i chose it, and then I wanted to make something that you can play.

I am going to clean up the code soon but I wanted to complete this bit first.

##### Share on other sites
If i remove SDL_Rect mob then the two monsters i have created get stuck together and the velocity is doubled.

That's because you used globals in the first place. The easiest way to fix this, without rewriting a lot of the code is to simply have a single enemy. Before you understand object orientated programming, you're not going to be able to process enemies the way you want to. This is all possible with procedural programming, but you're stuck half-way between the two, hence the confusion.

I'd love to write an example for you but unfortunately I'm already in the middle of a R&D project prototype as well as my full-time job. If I had more time, I'd help more!

Have you studied this? http://www.aaroncox....sdlclasses.html

If you wanted a quick fix to your problem. I'm afraid there isn't one. With the mix-and-match structure of your programming you're always going to run into problems until you go back to basics and actually learn the language. This may sound a bit harsh, but you need to understand the elements of the language before you can use them to solve problems.

##### Share on other sites
I am going to read the guide and then rewrite my whole program from scratch. I'm going to keep the current program with one enemy just in case I run out of time.
Anyway, thanks.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013448
×