Page 1 of 1

Plain Old hitTesting

Posted: Wed Mar 07, 2012 8:56 am
by VZdemon
does any one know a good hitTest if condition because the one i'm currently using is blocking the entire way down.
if you catch the bug please fix it for me because i can't seem to find it.

Code: Select all

bool hitTest(object obj1, object obj2){
        if((obj1.posx + obj1.width >= obj2.posx)&&
           (obj1.posx <= obj2.posx + obj2.width)&&
           (obj1.posy + obj1.height >= obj2.posy)&&
           (obj1.posx <= obj2.posy + obj2.height))
        return true;
     else
        return false; 
}
thanks :) .

Re: Plain Old hitTesting

Posted: Wed Mar 07, 2012 9:30 am
by Chris
What your are looking for is collision detection.
http://lmgtfy.com/?q=2d+collision+detection+tutorial

My advice would be to use a vector class so your can store your positions as one object.

Re: Plain Old hitTesting

Posted: Wed Mar 07, 2012 9:37 am
by VZdemon
i'm using plain C, it was made before the invention of Object Oriented Programming so there is no objects or classes in C it's procedural programming only.

Re: Plain Old hitTesting

Posted: Wed Mar 07, 2012 9:55 am
by Winawer
I'm not sure what your exact requirements are, but maybe something like this would work?

Code: Select all

bool hitTest( object obj1, object obj2 ) {
	int left1 = obj1.posx;
	int left2 = obj2.posx;
	int right1 = obj1.posx + obj1.width;
	int right2 = obj2.posx + obj2.width;
	int top1 = obj1.posy;
	int top2 = obj2.posy;
	int bottom1 = obj1.posy + obj1.height;
	int bottom2 = obj2.posy + obj2.height;

	if( bottom1 < top2 ) {
		return false;
	}
	if( top1 > bottom2 ) {
		return false;
	}
	if( right1 < left2 ) {
		return false;
	}
	if( left1 > right2 ) {
		return false;
	}
	return true;
}

Re: Plain Old hitTesting

Posted: Wed Mar 07, 2012 11:52 pm
by Jackolantern
Have you looked around for collision detection libraries? C is a very common language for game development, and collision detection is one of the most common libraries needed for game development.

There are some fairly easy methods you can use yourself, such as overlapping bounding-boxes, but pixel-perfect collision is complex and one really shouldn't spend the time to re-invent the wheel if pixel-perfect is what you need.

Re: Plain Old hitTesting

Posted: Thu Mar 08, 2012 10:12 pm
by VZdemon
thanks Winawer works perfectly.

btw normally downloading a physics library is the first thing i would do. but i found this great library called winBGIm for C and it has a function called putpixel(x,y,color), and so i'm trying to challenge myself and see what i can make using only that function (and some keyboard input functions).

Re: Plain Old hitTesting

Posted: Sun Mar 11, 2012 3:48 am
by Xaleph
You did something wrong and that`s the && usage. Your code will only validate if ALL conditions are met, but if the width of obj1 + offsetX is greater then the obj2 offset, you already have a collision. It`s just number shifting. You should check for different kinds of collisions, the X offset + width, the Y offset + height and the X offset ( left collision) or the Y offset (top/bottom collision).

That should check if a collision occured, if so, return true. Otherwise false, however, i`m going to guess you are creating a player so you should always have a collision on the bottom part of your player ( walking on the floor ).