Screen collision detection

Started by
5 comments, last by theOcelot 15 years, 8 months ago
I don't understand why this won't work. I want to be able to use my boolean collision detection function to check if something is still on screen and draw and move it if it is. I have a camera which I set to follow my player. I draw to the screen what is contained in that camera rect. Here is my camera function. //////////////////////////////////////////////////////////////////////////////// void Player::set_camera() { //Center the camera over the player camera.x = ( (int)playerbox.x + player_width/ 2 ) - SCREEN_WIDTH / 2; camera.y = ( (int)playerbox.y + player_height/ 2 ) - SCREEN_HEIGHT / 2; //Keep the camera in bounds. if( camera.x < 0 ) { camera.x = 0; } if( camera.y < 0 ) { camera.y = 0; } if( camera.x > LEVEL_WIDTH - camera.w ) { camera.x = LEVEL_WIDTH - camera.w; } if( camera.y > LEVEL_HEIGHT - camera.h ) { camera.y = LEVEL_HEIGHT - camera.h; } } //////////////////////////////////////////////////////////////////////////////// And here is my collision function ////////////////////////////////////////////////////////////////// // check collision 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; } //////////////////////////////////////////////////////////////////////////////// Now, when I try to do this. if ( shot1_alive == true && check_collision( playerfirebox, camera ) { if( bullet_direction == bullet_right ) { playerfirebox.x += shotVel; playerfirebox.y -= cameraDY; } } I am not sure what's going on but my bullet will not move. I start at the left hand side of the level. When I push my fire button, the bullet is drawn. But it doesn't move. When I move my player right until camera.x is no longer equal to 0( refer to set camera function ) and the camera starts to move, the bullet moves with the camera. It stays on the same spot on the screen. Why does this happen? What do I need to do to get the result I want? ( to check if something is still on screen and draw and move it if it is ) Thank you.
Advertisement
I'm not sure what you're whole problem is, but why not just create a rect that represents the screen, collide it with the game objects, and only draw if the function returns true? The only other thing is to make sure that the drawing code doesn't change the actual position of things like bullets. Speaking of which, you have made sure that your world coordinates are completely independent of screen/drawing coordinates, right?

If that doesn't help, you're probably going to have to post a little more code.
http://gamedev.pastebin.com/m50d2558d

Well here is all of it....If you wanna look.....
All 2428 lines of newbish mess...

"but why not just create a rect that represents the screen"
I thought that was what my camera was.
Anyone have any ideas??

I'd really appreciate them. :)
PM'ed you. Hope it went through.
It didn't go through. Go to your sent folder, see if it's there. If it is just copy/paste it here. :)
Sent folder is empty. I didn't think it was that lost.

The important thing was that you were trying to use the bullet's collision box to draw it with. My suggestions follow:

1. make a class that represents a bullet, containing an SDL_Rect, state, etc. Don't do things like bullet1_box, bullet2_box. That's nasty. Make an array of these bullet objects in your PlayerWeapon class.

2. In your showbullets function:
a. create an SDL_Rect as a local variable
b. for each bullet object:
i.check it's collision box against the screen (and also if it's alive, I guess)
ii.if the bullet is on screen, fill the SDL_Rect you created with coordinates that are corrected for the camera's distance from the origin of your coordinate system
iii. draw using the corrected rect.

When you get that straightened out, go through your entire design and change it to remove duplicated code. That will help make your design clearer and easier to debug. Why do you have classes Controller and Controller2? I don't know what they're for, but they can probably be combined into one class. Same for anything else that you have to tack a number on the end of.

PS. The state numbers in the player class should be static and const. Stuff like that shouldn't have to be filled in the constructor.

Hope that helps

This topic is closed to new replies.

Advertisement