Collision Detection Problems

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Collision Detection Problems

Post by RandomDever »

Code: Select all

bool checkCollision( 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;
}
The collision code

Code: Select all

void Player::Move( SDL_Rect solidCollisionBox )
{
	//Move the Player left or right
	collisionBox.x += xVel;
	//If the Player went too far to the left or right
	if( ( collisionBox.x < 0 ) || ( collisionBox.x + PLR_Width > SCR_Width ) || (checkCollision( collisionBox, solidCollisionBox ) ) )
	{
		//move back
		collisionBox.x -= xVel;
	}
	//Move the Player up or down
	collisionBox.y += yVel;
	//If the Player went too far up or down
	if( ( collisionBox.y < 0 ) || ( collisionBox.y + PLR_Height > SCR_Height ) || (checkCollision( collisionBox, solidCollisionBox ) ) )
	{
		//move back
		collisionBox.y -= yVel;
	}
}
The player movement code

http://www.youtube.com/watch?v=CCVhRshlIMk[/youtube]

I don't get it. and BTW in the vid i stop pressing the keyboard when i move up and randomly press when i'm stuck on the top.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Collision Detection Problems

Post by dandymcgee »

To utilize the youtube tags only put the portion after v= (http://www.youtube.com/watch?v=[b]CCVhRshlIMk[/b]) between the tags.


Fix'd. ;)

Also to answer your question, you may want to check this thread out. If you still have questions post back here.

Edit:
Try changing these lines:

Code: Select all

//move back
collisionBox.x -= xVel;
collisionBox.y -= yVel;
To this:

Code: Select all

//move back
collisionBox.x -= 2 * xVel;
collisionBox.y -= 2 * yVel;
Just for the fun of it.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Collision Detection Problems

Post by MarauderIIC »

So... what's the problem, exactly?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Collision Detection Problems

Post by dandymcgee »

MarauderIIC wrote:So... what's the problem, exactly?
RandomDever wrote: I don't get it. and BTW in the vid i stop pressing the keyboard when i move up and randomly press when i'm stuck on the top.
That. He's not moving the character back far enough so that it isn't detected as a collision the next frame was my guess.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Collision Detection Problems

Post by RandomDever »

Well Actually the getting stuck on top thing was just me being a dumbass.
But I think I found an SDL library for collision so I'll see how that works. :)
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Collision Detection Problems

Post by ibly31 »

I don't know how you handle collision, but what I did was have a function that I sent the characters current position and where what direction they want to go.

like: void checkCol(PosX, PosY, AddX, AddY)

In my case, my character's size doesnt change so I don't need to have an entire SDL Rect. I then filtered through every collision rectangle and used lazyfoo's checkCollision function.

When handling the keys, I would do:

if(press left && checkCol(charX, charY, -1, 0)
if(press right && checkCol(charX, charY, 1, 0);

or whatever velocity your character moves.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
Post Reply