Plain Old hitTesting

C++, C#, Java, PHP, ect...
Post Reply
User avatar
VZdemon
Posts: 93
Joined: Thu Nov 25, 2010 1:55 pm

Plain Old hitTesting

Post 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 :) .
it's VZdaemon on xbox live
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Plain Old hitTesting

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
VZdemon
Posts: 93
Joined: Thu Nov 25, 2010 1:55 pm

Re: Plain Old hitTesting

Post 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.
it's VZdaemon on xbox live
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Plain Old hitTesting

Post 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;
}
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Plain Old hitTesting

Post 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.
The indelible lord of tl;dr
User avatar
VZdemon
Posts: 93
Joined: Thu Nov 25, 2010 1:55 pm

Re: Plain Old hitTesting

Post 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).
it's VZdaemon on xbox live
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Plain Old hitTesting

Post 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 ).
Post Reply

Return to “Coding”