• Create Account

## ANSI C - pixel perfect collision

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1DrNicholas  Members

130
Like
0Likes
Like

Posted 07 June 2014 - 02:11 PM

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

### #2rip-off  Moderators

10730
Like
0Likes
Like

Posted 07 June 2014 - 04:04 PM

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.

### #3DrNicholas  Members

130
Like
0Likes
Like

Posted 07 June 2014 - 06:05 PM

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, 07 June 2014 - 06:20 PM.

### #4LennyLen  Members

5414
Like
0Likes
Like

Posted 07 June 2014 - 08:46 PM

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.

### #5rip-off  Moderators

10730
Like
2Likes
Like

Posted 08 June 2014 - 05:21 AM

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.

### #6Bacterius  Members

13102
Like
0Likes
Like

Posted 08 June 2014 - 05:24 AM

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

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #7Chris_F  Members

3018
Like
1Likes
Like

Posted 08 June 2014 - 05:37 AM

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?   Stuck with Turbo C 2.0 or something?

### #8Bacterius  Members

13102
Like
0Likes
Like

Posted 08 June 2014 - 05:41 AM

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?   Stuck with Turbo C 2.0 or something?

The thread title does contain "ANSI C"

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #9Bregma  Members

8320
Like
0Likes
Like

Posted 08 June 2014 - 05:43 AM

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.

Stephen M. Webb
Professional Free Software Developer

### #10Bacterius  Members

13102
Like
0Likes
Like

Posted 08 June 2014 - 05:50 AM

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, 08 June 2014 - 05:51 AM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #11DrNicholas  Members

130
Like
0Likes
Like

Posted 08 June 2014 - 11:13 PM

I use ANSI C because the C Programming Language only covers ANSI C, and http://clc-wiki.net/wiki/Portability_and_ANSI_C_Compliance

So I may be cut out of some nicer features, but the program still get's done nontheless.

### #12Álvaro  Members

20266
Like
0Likes
Like

Posted 08 June 2014 - 11:40 PM

The problem is that you are saying
typedef struct HITBOX {...] HITBOX;
typedef struct {...} HITBOX;

### #13Olof Hedman  Members

5703
Like
0Likes
Like

Posted 09 June 2014 - 04:03 AM

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...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.