• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NicholasLopez

ANSI C - pixel perfect collision

12 posts in this topic

Hello,

 

I making this topic because someone told me in the other threads to just make it a new one. First I am going to post my code and then talk.

typedef struct HITBOX
{
	/* This is for the hitbox's dimensions */
	int x, y;
	int w, h;
	/* This is for pixel perfect collision */
	int surface_n;
	int surface_e;
	int surface_s;
	int surface_w;
} HITBOX;
int collision( HITBOX A, HITBOX B ) {
	int a_left, a_right, a_top, a_bottom;
	a_left = A.x;
	a_right = A.x + A.w;
	a_top = A.y;
	a_bottom = A.y + A.h;

	int b_left, b_right, b_top, b_bottom;
	b_left = B.x;
	b_right = B.x + B.w;
	b_top = B.y;
	b_bottom = B.y + B.h;

	if( a_bottom <= b_top || a_top >= b_bottom || a_right <= b_left || a_left >= b_right ) {
		return false;
	} else {
		return true;
	}
}
int t = 0;
HITBOX TILE;
int world_collision( HITBOX A ) {
	for( t = 0; t < total_tiles; t++ ) {
		TILE.x = levelbox[t].x;
		TILE.y = levelbox[t].y;
		TILE.w = levelbox[t].w;
		TILE.h = levelbox[t].h;
		if( collision( A, TILE ) == true ) {
			A.surface_n = TILE.y;
			A.surface_e = TILE.x + TILE.w;
			A.surface_s = TILE.y + TILE.h;
			A.surface_w = TILE.x;
			return true;
		}
	}
	return false;
}
int collision_lua( lua_State *L ) {
	if( strcmp(lua_tostring(L, 1), "world") == 0 )
		return world_collision( player.hitbox );
	return 0;
}

My collisions technically are pixel perfect, but because the game object inside the lua file moves at a float (although I have done the rounding method to prevent that, I still get the player hovering or inside by 1/2 pixels time to time). I came up with the surface variables inside HITBOX so that when there is a collision (with the level), the hitbox will touch that surface, and i won't see any floating/sinking. The problem is I can't figure out how to return those surface variables easily. I was asking some people, and they told me to do

int world_collision( HITBOX *A ) {
	for( t = 0; t < total_tiles; t++ ) {
		TILE.x = levelbox[t].x;
		TILE.y = levelbox[t].y;
		TILE.w = levelbox[t].w;
		TILE.h = levelbox[t].h;
		if( collision( A, TILE ) == true ) {
			A->surface_n = TILE.y;
			A->surface_e = TILE.x + TILE.w;
			A->surface_s = TILE.y + TILE.h;
			A->surface_w = TILE.x;
			return true;
		}
	}
	return false;
}

I will get the errors:

 

actor.c: In function 'world_collision':
actor.c:72:3: error: incompatible type for argument 1 of 'collision'
actor.c:45:5: note: expected 'HITBOX' but argument is of type 'struct HITBOX *'
actor.c: In function 'collision_lua':
actor.c:104:3: error: incompatible type for argument 1 of 'world_collision'
actor.c:66:5: note: expected 'struct HITBOX *' but argument is of type 'HITBOX'

although if I change one line to:

if( collision( *A, TILE ) == true ) {

I just get the errors in function 'collision_lua'. If I do:

return world_collision( *player.hitbox );

I will get the issue:

 

actor.c: In function 'collision_lua':
actor.c:104:27: error: invalid type argument of unary '*' (have 'HITBOX')

Does anybody have any ideas what thing needs changed, and willing to show me? Thanks

0

Share this post


Link to post
Share on other sites
The correct code to pass a pointer to the player's hitbox would be:
world_collision( &player.hitbox );
I would also note that "t" and TILE" could be local variables in "world_collision":
int world_collision( HITBOX *hitbox ) {
	for(int t = 0; t < total_tiles; t++ ) {
                HITBOX tile;
		tile.x = levelbox[t].x;
		tile.y = levelbox[t].y;
		tile.w = levelbox[t].w;
		tile.h = levelbox[t].h;
		if( collision( *hitbox , tile) == true ) {
			hitbox->surface_n = tile.y;
			hitbox->surface_e = tile.x + tile.w;
			hitbox->surface_s = tile.y + tile.h;
			hitbox->surface_w = tile.x;
			return true;
		}
	}
	return false;
}
Code that uses global variables is generally harder to understand and change than code with local variables, so this would be preferred. Here, it is obvious that no other code depends on the values of "t" and "TILE" being set by this function.

I would also note that you don't seem to have a consistent naming scheme for variables. In some cases you use lowercase, such as a_left, others you use TILE and A. While the exact scheme is not important, being consistent makes code easier to understand.
0

Share this post


Link to post
Share on other sites

ANSI C does not allow you to initialize a variable inside a for statement, or at the very least my GCC compiler won't. Will try defining TILE inside it though, doesn't that make a bunch of them or are they cleared from memory?

 

EDIT:

thanks, it works

Edited by DrNicholas
0

Share this post


Link to post
Share on other sites

Also, just for the record:  What you are doing is NOT pixel perfect collision, it is bounding box collision.  Unless every object in your world is a rectangle.

0

Share this post


Link to post
Share on other sites

ANSI C does not allow you to initialize a variable inside a for statement, or at the very least my GCC compiler won't.

It should if you specify -std=c99. If you don't want to do this, you can declare it just before the loop:
int world_collision( HITBOX *hitbox ) {
	int t;
	for(t = 0; t < total_tiles; t++ ) {
                HITBOX tile;
		tile.x = levelbox[t].x;
		tile.y = levelbox[t].y;
		tile.w = levelbox[t].w;
		tile.h = levelbox[t].h;
		if( collision( *hitbox , tile) == true ) {
			hitbox->surface_n = tile.y;
			hitbox->surface_e = tile.x + tile.w;
			hitbox->surface_s = tile.y + tile.h;
			hitbox->surface_w = tile.x;
			return true;
		}
	}
	return false;
}

Will try defining TILE inside it though, doesn't that make a bunch of them or are they cleared from memory?

The necessary memory will be allocate when the function is called and deallocated when it returns, so no it doesn't have any drawbacks. It is only manual memory allocation (such as malloc/free or external library calls) that you have to worry about.
2

Share this post


Link to post
Share on other sites


It should if you specify -std=c99.

 

That's not ANSI C anymore though (admittedly GCC's default setting isn't either, but some gnu89 variant).

0

Share this post


Link to post
Share on other sites

 


It should if you specify -std=c99.

 

That's not ANSI C anymore though (admittedly GCC's default setting isn't either, but some gnu89 variant).

 

 

Why on earth would anyone be using strictly ANSI C in 2014? blink.png  Stuck with Turbo C 2.0 or something?

1

Share this post


Link to post
Share on other sites

 

 


It should if you specify -std=c99.

 

That's not ANSI C anymore though (admittedly GCC's default setting isn't either, but some gnu89 variant).

 

 

Why on earth would anyone be using strictly ANSI C in 2014? blink.png  Stuck with Turbo C 2.0 or something?

 

 

The thread title does contain "ANSI C" wink.png

0

Share this post


Link to post
Share on other sites


That's not ANSI C anymore though (admittedly GCC's default setting isn't either, but some gnu89 variant).

Well, given the phrase "ANSI C" has no distinct meaning, that's a pretty vague statement.

 

GCC is pretty strict about adhering to the various standards when you explicitly set them.  Using -std=c89 you get ISO C 89, using -std=c99 you get ISO C 99, using -std=c11 you get ISO C 2011.  If you want the gnu89 variant, you need to specify -std=gnu89.

0

Share this post


Link to post
Share on other sites

Not sure what your point is, Bregma, ANSI C can be a couple of things but it certainly isn't C99. And I did say "default", and by default GCC does currently adhere to gnu90 (not gnu89, my mistake) as pointed out in the documentation. Obviously if you ask for a specific standard or dialect you'll get that. Anyway let's not derail the thread further, was just pointing out that the OP might want to stick to ANSI C one way or the other as per the thread title.

Edited by Bacterius
0

Share this post


Link to post
Share on other sites
The problem is that you are saying
typedef struct HITBOX {...] HITBOX;
Instead, you should be saying
typedef struct {...} HITBOX;
0

Share this post


Link to post
Share on other sites


http://clc-wiki.net/wiki/Portability_and_ANSI_C_Compliance

 

And as usual, I'm left scratching my head. Sure, portable code is great, and all those arguments are fine, but what has Ansi C to do with it?

Wouldn't my code be just as portable, and I have all those advantages, if I simply wrote it for any of the other standards that have free compilers for pretty much anything?

Like... C++11 or something? :)

 

Sorry for the OT, and I'm not bashing your choice of language, I just... don't understand I guess...

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0