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