Followers 0

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

1

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

The thread title does contain "ANSI C"

0

##### 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 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 on other sites
The problem is that you are saying
typedef struct HITBOX {...] HITBOX;
typedef struct {...} HITBOX;
0

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

## Create an account

Register a new account